WordPress子类别显示所有帖子

时间:2013-07-28 作者:BDGapps

我正在创建一个Wordpress页面,其中团队子类别中的所有帖子都显示在网格中。我用下面的代码得到了这个。格式和总体布局很好,但它只显示子类别中最近的5篇文章,而不是所有文章。在我的Wordpress帐户中,我有7篇应该显示的帖子。我应该如何检索帖子,使其全部显示出来。

                <ul class="faces">

 <?php
$categories = get_categories( \'child_of=2\' );  
foreach  ( $categories as $category ) {

    echo \'<div class="grid-row"><h2>\'.$category->name.\'</h2></div>\';

    $cat_posts = get_posts( \'cat=\'.$category->term_id );
    $end = count( $cat_posts ) - 1;
    $i = 0;
    foreach ( $cat_posts as $post ) {
        setup_postdata( $post );
        $face = get_field( \'face\' );
        $name = get_field( \'fullname\' );

        if ( $i % 6 === 0 ) {
            echo \'<div class="grid-row">\';
        }
      echo \'<div class="obj">\';

        echo wp_get_attachment_image($face)
            . \'<div class="name">\'.$name.\'</div>\';

        echo \'</div>\';
                if ( $i % 6 === 5 ) {
            echo \'</div>\';
        }
               $i++;
    }
}?>
    </ul>
//css

div.grid-row {
    width: 100%;

   height: 100%;
    position: relative;
    overflow: hidden;
}
div.obj{
float: left;
    position: relative;
    padding-right: 10px;
}
.faces{
    width: 1000px;

}
enter image description here

enter image description here

enter image description here

修订如下:enter image description here

以下职位版次:

<?php
$categories = get_categories( \'child_of=2\' );  
foreach  ( $categories as $category ) {

    echo \'<div class="grid-row"><h2>\'.$category->name.\'</h2></div>\';
    $args1 = array( \'posts_per_page\' => -1, \'cat=\'.$category->term_id );
    $cat_posts = get_posts( $args1 );
    $end = count( $cat_posts ) - 1;
    $i = 0;
    foreach ( $cat_posts as $post ) {
    $post_category = get_the_category($post->ID);
    if($post_category->cat_name == $category->name){
        setup_postdata( $post );
        $face = get_field( \'face\' );
        $name = get_field( \'fullname\' );

        if ( $i % 6 === 0 ) {
            echo \'<div class="grid-row">\';
        }
      echo \'<div class="obj">\';

       echo \'<div class="faceThumb">\';
       echo wp_get_attachment_image($face);
       echo \'</div>\';
          echo \'<div class="name">\';
          echo $name;
          echo \'</div>\';

        echo \'</div>\';
                if ( $i % 6 === 5 ) {
            echo \'</div>\';
        }
        }
               $i++;

    }
}?>

2 个回复
SO网友:lonehorseend

可以提供的获取\\u posts的参数之一是posts\\u per\\u page。如果您不包括此项,则默认为您的页面在阅读页面上显示最多设置您的设置。http://wordpress.org/support/topic/get_posts-not-pull-all-posts-unless-numberposts-in-query. 是的,我知道论坛的帖子已经有两年了,但它证实了我在阅读后的怀疑http://codex.wordpress.org/Template_Tags/get_posts 以及该页面底部列出的源文件。

SO网友:gmazzap

可以简单得多。我不知道是什么get_field 是的,我想是你的自定义函数。。。但为什么不使用标准自定义字段和后期缩略图呢?无论如何

$categories = get_categories( \'child_of=3\' ); 

foreach  ( $categories as $category ) {
  $i = -1;
  echo \'<div class="grid-row"><h2>\' . $category->name . \'</h2></div>\';
  $args = array( \'posts_per_page\' => -1, \'cat\' => $category->term_id );
  $cat_posts = new WP_Query($args);
  if ( $cat_posts->have_posts() ) : while ( $cat_posts->have_posts() ) :
    $i++;
    $cat_posts->the_post();
    $face = get_field( \'face\' );
    $name = get_field( \'fullname\' );
    if ( $i % 6 == 0 ) echo \'<div class="grid-row">\';
    echo \'<div class="obj">\';
    echo \'<div class="faceThumb">\';
    echo wp_get_attachment_image($face);
    echo \'</div><div class="name">\' . $name . \'</div></div>\';
    if ( ($i % 6 == 5) || $i == ($cat_posts->post_count - 1) ) echo \'</div>\';
  endwhile; endif;
}
wp_reset_postdata();

结束

相关推荐

Inserting random posts

我想在滑块中添加两个自定义帖子类型,一个是事件,另一个是核心值,但我想添加所有事件,并随机插入7个核心值中的2或3个。我已经设置了这个查询,它显然只是要插入所有事件和所有核心值。我有点困惑于如何让它的随机部分发生。我不希望使用post-ID来选择随机值,因为post-ID在将来可能会改变。有什么想法吗?? $the_query = new WP_Query( array( \'post_type\' => array( \'core-value\', \'event\' )) );