【问题标题】:WordPress bug appears with a custom shortcode functionWordPress 错误与自定义简码功能一起出现
【发布时间】:2018-12-29 09:20:42
【问题描述】:

我创建了一个简单的自定义插件,可以将帖子分组:

function custom_category_loop()
{
    // Grab all the categories from the database that have posts.
    $categories = get_terms( 'category', 'orderby=name&order=ASC');
    // Loop through categories
    foreach ( $categories as $category )
    {
        // Display category name
        echo '<h2 class="post-title">' . $category->name . '</h2>';
        echo '<div class="post-list">';
        // WP_Query arguments
        $args = array(
            'cat' => $category->term_id,
            'orderby' => 'term_order',
        );
        // The Query
        $query = new WP_Query( $args );
        // The Loop
        if ( $query->have_posts() )
        {
            while ( $query->have_posts() )
            {
                $query->the_post();
                ?>
                <div><a href="<?php the_permalink();?>"><?php the_title(); ?></a></div>
                <?php
            } // End while
        } // End if
        echo '</div>';
        // Restore original Post Data
        wp_reset_postdata();
    } // End foreach
}
add_shortcode( 'my_posts_grouped', 'custom_category_loop' );

然后我创建了一个页面并添加了单行

[my_posts_grouped]

我的 WordPress 安装是多站点安装,所有页面都使用相同的主题,因为我有相同的站点但使用不同的语言(和 URL)。

我的问题是,在我所有的网站上,除了一个网站,简码都能完美运行。仅在一个站点上,代码在页眉之前和正文页内输出。

任何想法为什么以及如何解决这个问题?

【问题讨论】:

  • 那个网站的代码/标记有什么不同?

标签: php wordpress shortcode


【解决方案1】:

WordPress 短代码必须返回 数据(而不是使用echo 或类似方法直接打印到屏幕上)。

来自documentation

请注意,短代码调用的函数不应产生任何类型的输出。 短代码函数应返回用于替换短代码的文本直接产生输出会导致意想不到的结果。这类似于过滤器函数的行为方式,因为它们不应从调用中产生预期的副作用,因为您无法控制它们的时间和位置调用自。

您的代码仅在一个网站上出现问题,而不是在所有网站上都出现问题,我只能靠运气了。

使用return 而不是多个echo 调用的代码的更新版本应该可以解决您的问题:

function custom_category_loop()
{
    // String to return
    $html = '';

    // Grab all the categories from the database that have posts.
    $categories = get_terms( 'category', 'orderby=name&order=ASC');
    // Loop through categories
    foreach ( $categories as $category )
    {
        // Display category name
        $html .= '<h2 class="post-title">' . $category->name . '</h2>';
        $html .= '<div class="post-list">';
        // WP_Query arguments
        $args = array(
            'cat' => $category->term_id,
            'orderby' => 'term_order',
        );
        // The Query
        $query = new WP_Query( $args );
        // The Loop
        if ( $query->have_posts() )
        {
            while ( $query->have_posts() )
            {
                $query->the_post();
                $html .= '<div><a href="' . get_permalink() . '">' . get_the_title() . '</a></div>';
            } // End while
        } // End if
        $html .= '</div>';
        // Restore original Post Data
        wp_reset_postdata();

        // Return the HTML string to be shown on screen
        return $html;
    } // End foreach
}
add_shortcode( 'my_posts_grouped', 'custom_category_loop' );

【讨论】:

    猜你喜欢
    • 2021-11-02
    • 2016-07-17
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多