短代码出现在顶部

时间:2013-12-09 20:35:48

标签: wordpress shortcode

拥有一个调用自定义帖子类型的短代码,以显示在标准循环模板中的某个区域。 我遇到的问题是输出显示在页面中的内容之上。

有人可以帮忙吗?

代码是:

function JDD_display_stores() {

    ob_start();

    $args = array(
        'post_type' => 'stores',
        'tax_query' => array(
            array(
                'taxonomy' => 'store',
                'field' => 'slug'
            )
        )
    );

    $success = new WP_Query( $args );

    if( $success->have_posts() ) {

        while( $success->have_posts() ) {

            $success->the_post();

            ?>
                <h1><?php the_title() ?></h1>

                <div class='content'>

                    <?php the_content() ?>

                </div>

            <?php
            return $success;
        }
    }
    else {
        echo 'No stores have been added!';
    }
}

add_shortcode('display_stores', 'JDD_display_stores');

1 个答案:

答案 0 :(得分:1)

您需要返回标题和内容,而不是回应它 (the_content()the_title()执行)

像:

$output = '';

while( $success->have_posts() ) {
    $success->the_post();

    $output .= sprintf("<h1>%s</h1>", get_the_title()); 
    $output .= sprintf('<div class="content">%s</div>', get_the_content());

}

//reset the orignal main query
//see http://codex.wordpress.org/Function_Reference/wp_reset_query
wp_reset_query();

return $output;

..你需要获取 _the_title()和获取 _the_content()。

另外请注意,您将return 置于之外的while循环中,否则您将在第一次迭代中退出该函数...

http://codex.wordpress.org/Function_Reference/get_the_content
&安培; http://codex.wordpress.org/Function_Reference/get_the_title