【问题标题】:WordPress Shortcode based on other shortcode基于其他简码的 WordPress 简码
【发布时间】:2017-09-21 07:36:59
【问题描述】:

我正在处理 2 个 WordPress 短代码:

  1. 一章。 [章节名称=“开始”]...内容...[/chapter]
  2. 目录 [toc][/toc]。目录需要显示一个简单的章节列表。

规格:

  • 一个帖子可以有多个章节。
  • 帖子中可以有一个、两个或没有 toc 短代码。
  • 目录可以在章节之前或之后,也可以在章节之前和之后。这取决于帖子作者,所以我事先不知道。
  • 我不能使用嵌套的短代码,因为编写者很难使用它们。

我想过使用静态 toc 数组在每个章节标签处添加章节,然后将其输出到 toc 短代码中。唉,toc 短代码可以出现在章节之前,然后数组将为空。

以下帖子 html 不会显示目录:

[toc][/toc]
Here is some content
[chapter name="hello"]xxx[/chapter]
In between content may come here
[chapter name="world"]yyy[/chapter]
Some more stuff

这是我的起始代码(嵌入到一个类中):

public static function register_chapter_shortcode($atts, $content=null){
    $atts = shortcode_atts (
        array (
            'name'       => '',
        ), $atts );

    $name = $atts["name"];
    if (self::$toc == null){
        self::$toc = array ($name);
    } else {
        array_push(self::$toc, $name);
    }
    return '
        <h2>'.$atts["name"].'</h2>
        <div>'.do_shortcode($content).'</div>'
    ;
}

public static function register_toc_shortcode($atts, $content=null){
    $items = "";
    foreach (self::$toc as $item){
        $items = $items. '<li><a href="#">'.$item.'</a></li>';
    }
    return '
        <ul>'.$items.'</ul>            
    ';
}

【问题讨论】:

  • 你打算从register_chapter_shortcode显示什么输出?
  • 这并不重要。你可以假设:return do_shortcode($content);
  • 所以你确实打算输出一些东西?如果你不这样做,你可以将[toc] 移到最后。 register_toc_shortcoderegister_chapter_shortcode 设置self::$toc 之前运行,所以它没有任何显示。问题在于函数运行的顺序,因此无论哪种方式,您都必须将[toc] 移动到末尾。如果您需要在[chapter] 中显示某些内容,我看到的唯一方法是(1)将 all 内容保存到register_chapter_shortcode 中的self::$toc 并在之后输出register_toc_shortcode 中的所有内容[chapter]s 或 (2) 使用 jQuery 将 toc 插入 DOM
  • 是的。我正在输出东西。章节名称等等。 TOC 可以在顶部或底部(我听说有些用户想要两次)。这就是让它变得复杂的原因:用户。大声笑
  • 您应该确保从一开始就在您的问题中有所有您的要求。在短代码之间包含文本需要完全不同的方法。我浪费了很多时间来准备下面的答案以满足您的初始要求,而且我现在没有更多的时间来帮助您。希望别人会。我还认为您正试图通过短代码实现太多目标,它们并不适用于任何复杂的东西 - 您正试图使用​​它们来处理来自编辑器的内容。有没有其他方法可以使用,例如自定义字段?

标签: php wordpress shortcode


【解决方案1】:

问题是函数运行的顺序:register_toc_shortcoderegister_chapter_shortcode 设置self::$toc 之前运行,所以它没有任何显示。这意味着 [toc] 必须在 [chapter] 短代码之后。这样做的问题是您需要在列出章节之前显示目录。

正如我在评论中提到的,我可以看到两种解决方法 - 一种是使用 javascript 将 TOC 插入 DOM,但如果可能的话我会避免这样做。

另一种方法是使用register_chapter_shortcode一切 存储在self::$toc 中,并且本身不输出任何内容。然后register_toc_shortcode就可以显示所有内容了。

这个没有经过测试,但其背后的基本逻辑应该是正确的:

public static function register_chapter_shortcode($atts, $content=null){
    $atts = shortcode_atts (
        array (
            'name'       => '',
        ), $atts );

    if (self::$toc == null)
        self::$toc = array ();

    /* add ALL info to self::$toc in an array */
    array_push(self::$toc, array("name" => $atts["name"], "content" => $content));
}

public static function register_toc_shortcode($atts, $content=null){
    /* add the ability to pass in the position of the toc, as required in your comment */
    $atts = shortcode_atts (
        array (
            'position' => 'top', /* e.g. "top" (default), "bottom", "both" */
        ), $atts );

    /* generate the HTML for the TOC */
    $toc_html = "<ul>";
    foreach (self::$toc as $item){
        $toc_html .= '<li><a href="#">'.$item["name"].'</a></li>';
    }
    $toc_html .= "</ul>";

    /* generate the HTML for the chapters */
    $chapters = "";
    foreach (self::$toc as $item){
        $chapters .= '<h2>'.$item["name"].'</h2>
        <div>'.do_shortcode($item["content").'</div>';
    }

    /* generate the output, putting the TOC at the top or bottom as required */
    $html = "";
    if ($position == "top" || $position == "both") $html .= $toc_html;
    $html .= $chapters;
    if ($position == "bottom" || $position == "both") $html .= $toc_html;

    return $html;
}

那么你可以如下使用它:

[chapter name="hello"]xxx[/chapter]
[chapter name="world"]yyy[/chapter]
[toc position="top"][/toc]

注意:[toc]总是必须跟在chapters 之后,以便为[toc] 设置它们以显示详细信息。

【讨论】:

  • 谢谢。但是: 1. 用户可能想要 TOC 之前或之后或两者兼而有之。 2. 这假设帖子上除了短代码之外没有其他内容(从来没有)。
  • @checklist 1. 不,我的代码允许在顶部、底部或两个位置添加 TOC,具体取决于短代码中指定的位置 - 您是否检查了 register_toc_shortcode 中的代码? 2.我只能使用您问题中包含的信息-这就是我在评论中特别问的原因!如果你不告诉我们,我不知道你还想做什么。如果您有其他内容,您可以轻松地将其包含在 register_chapter_shortcode 的数组中。
猜你喜欢
  • 1970-01-01
  • 2011-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多