【问题标题】:Shortcode appearing at top短代码出现在顶部
【发布时间】:2013-12-09 20:35:48
【问题描述】:

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

谁能帮忙?

代码是:

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');

【问题讨论】:

    标签: wordpress shortcode


    【解决方案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;
    

    ..为此,您需要 get_the_title() 和 get_the_content()。

    还要注意,将return 放在 while 循环之外,否则您将在第一次迭代中退出函数...

    http://codex.wordpress.org/Function_Reference/get_the_content
    &http://codex.wordpress.org/Function_Reference/get_the_title

    【讨论】:

    • return the_title();??
    • 是的,但你需要 get_the_title() 。更新了我的答案
    • 已经尝试了代码,但输出的只是:/h1> /div> in my h1 style
    猜你喜欢
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    相关资源
    最近更新 更多