当POSTS_PER_PAGE设置为多个时,WP_QUERY仅打印出一个帖子

时间:2018-03-12 作者:Limpuls

我制作了自己的自定义帖子类型和输出自定义帖子类型的快捷码。到目前为止一切都很好,但一旦我尝试添加更多帖子,我仍然只能在页面上看到一篇,而不是更多。posts_per_page 设置为3或更多,但仍然为零。

// Custom Post Type for Use Cases page slider

function create_post_type() {
  register_post_type( \'use_cases\',
    array(
      \'labels\' => array(
        \'name\' => __( \'Use Cases\' ),
        \'singular_name\' => __( \'Use_Case\' )
      ),
      \'public\' => true,
      \'has_archive\' => true,
    )
  );
}
add_action( \'init\', \'create_post_type\' );

// Shortcode for Use Cases caption

add_shortcode(\'Use_Case\', \'use_case_shortcode_query\');
function use_case_shortcode_query($atts, $content){
    echo "working";
  extract(shortcode_atts(array( // a few default values
   \'posts_per_page\' => \'3\',
   \'post_type\' => \'use_cases\',
        )
   , $atts));

  global $post;

  $posts = new WP_Query(array( // a few default values
   \'posts_per_page\' => \'3\',
   \'post_type\' => \'use_cases\'
        ));
    //print_r($posts);
  $output = \'\';
    if ($posts->have_posts()):
        while ($posts->have_posts()):
                    //echo var_dump($posts->the_post());
            $posts->the_post();
            $out = \'<div class="use_case_boxes">
                <h4>Name: <a href="\'.get_permalink().\'" title="\' . get_the_title() . \'">\'.get_the_title() .\'</a></h4>
                <p class="Film_desc">\'.get_the_content().\'</p>\';
                // add here more...
            $out .=\'</div>\';
    /* these arguments will be available from inside $content
        get_permalink()
        get_the_content()
        get_the_category_list(\', \')
        get_the_title()
        and custom fields
        get_post_meta($post->ID, \'field_name\', true);
    */
    endwhile;
  else:
    return; // no posts found
endif;
  wp_reset_query();
  return html_entity_decode($out);
}

1 个回复
最合适的回答,由SO网友:Visakh B Sujathan 整理而成

您正在初始化out变量,这就是为什么它只显示一个产品

// Custom Post Type for Use Cases page slider

function create_post_type() {
  register_post_type( \'use_cases\',
    array(
      \'labels\' => array(
        \'name\' => __( \'Use Cases\' ),
        \'singular_name\' => __( \'Use_Case\' )
      ),
      \'public\' => true,
      \'has_archive\' => true,
    )
  );
}
add_action( \'init\', \'create_post_type\' );

// Shortcode for Use Cases caption

add_shortcode(\'Use_Case\', \'use_case_shortcode_query\');
function use_case_shortcode_query($atts, $content){
    echo "working";
  extract(shortcode_atts(array( // a few default values
   \'posts_per_page\' => \'3\',
   \'post_type\' => \'use_cases\',
        )
   , $atts));

  global $post;

  $posts = new WP_Query(array( // a few default values
   \'posts_per_page\' => \'3\',
   \'post_type\' => \'use_cases\'
        ));
    //print_r($posts);
  $output = \'\';
$out = \'\';
    if ($posts->have_posts()):
        while ($posts->have_posts()):
                    //echo var_dump($posts->the_post());
            $posts->the_post();
            $out .= \'<div class="use_case_boxes">
                <h4>Name: <a href="\'.get_permalink().\'" title="\' . get_the_title() . \'">\'.get_the_title() .\'</a></h4>
                <p class="Film_desc">\'.get_the_content().\'</p>\';
                // add here more...
            $out .=\'</div>\';
    /* these arguments will be available from inside $content
        get_permalink()
        get_the_content()
        get_the_category_list(\', \')
        get_the_title()
        and custom fields
        get_post_meta($post->ID, \'field_name\', true);
    */
    endwhile;
  else:
    return; // no posts found
endif;
  wp_reset_query();
  return html_entity_decode($out);
}

结束

相关推荐

内容中内置的响应图像--我需要在unctions.php中添加任何内容吗?

我正在从头开始制作一个WP主题,但我似乎没有得到响应的图像。即使添加了srcset和size标记,以500px插入的图像仍将保持在500px。据我所知,这幅图像应该在手机上以300px的速度自动显示?<img class=\"alignleft size-full wp-image-446\" src=\"couple_group_session.jpg\" alt=\"\" width=\"500\" height=\"333\" srcset=\"couple_group_s