【问题标题】:Shortcode showing single post only仅显示单个帖子的简码
【发布时间】:2014-08-20 01:45:22
【问题描述】:

我正在创建一个推荐插件。我注册了一个查询自定义推荐帖子的简码,但简码只加载一个帖子,我需要加载 10 个帖子。那么短代码没有加载所有自定义帖子的问题在哪里?

代码在这里:

function testimonial_text_shortcode(){
    global $post;
    $q = new WP_Query(
        array('posts_per_page' => 10, 'post_type' => 'rwpt_custom_post')
        );      
    while($q->have_posts()) : $q->the_post();
        $list = '<li><p>'.get_the_content().'</p></li>';        
    endwhile;
    wp_reset_query();
    return $list;
}

add_shortcode('test_text', 'testimonial_text_shortcode');

【问题讨论】:

    标签: wordpress shortcode


    【解决方案1】:

    您将在每次循环迭代中覆盖 $list 的值,因此最终它只会保留最后一个值。

    修改您的代码以附加到变量:

    function testimonial_text_shortcode(){
        global $post;
        $q = new WP_Query(
            array('posts_per_page' => 10, 'post_type' => 'rwpt_custom_post')
            );
        $list = '<ul>';
        while($q->have_posts()) : $q->the_post();
            $list .= '<li><p>'.get_the_content().'</p></li>';        
        endwhile;
        $list .= '</ul>';
        wp_reset_query();
        return $list;
    }
    
    add_shortcode('test_text', 'testimonial_text_shortcode');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多