从Foreach循环中排除类别

时间:2014-09-08 作者:jojo79

当我每年列出自定义类型的项目时,如何从循环中排除一个类别?

<?php foreach(posts_by_year() as $year => $posts) : ?>
  <h3><?php echo $year; ?></h3>

  <ul>
    <?php foreach($posts as $post) : setup_postdata($post); ?>
      <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
      </li>
    <?php endforeach; ?>
    <div class="et_pb_column et_pb_column_4_4"><hr class="et_pb_space et_pb_divider"style="border-color: #f7f7f9;"></div>
  </ul>
<?php endforeach; ?>
更新,功能:

function posts_by_year() {
  // array to use for results
  $years = array();

  // get posts from WP
  $posts = get_posts(array(
    \'numberposts\' => -1,
    \'orderby\' => \'post_date\',
    \'order\' => \'ASC\',
    \'post_type\' => \'project\',
    \'post_status\' => \'publish\',
    \'category\' => -27
  ));

  // loop through posts, populating $years arrays
  foreach($posts as $post) {
    $years[date(\'Y\', strtotime($post->post_date))][] = $post;
  }

  // reverse sort by year
  krsort($years);
  return $years;
}

1 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

我相信您正在使用以下代码this pastebin link

function posts_by_year() {
  // array to use for results
  $years = array();
 
  // get posts from WP
  $posts = get_posts(array(
    \'numberposts\' => -1,
    \'orderby\' => \'post_date\',
    \'order\' => \'ASC\',
    \'post_type\' => \'my-custom-post-type\',
    \'post_status\' => \'publish\'
  ));
 
  // loop through posts, populating $years arrays
  foreach($posts as $post) {
    $years[date(\'Y\', strtotime($post->post_date))][] = $post;
  }
 
  // reverse sort by year
  krsort($years);
  return $years;
}
此函数使用get_posts 返回帖子列表。要排除类别,可以传递category 参数。只有一个音符

Note: category参数需要是类别的ID,而不是类别名称。

Note: category参数可以是以逗号分隔的类别列表,如get_posts() 函数将“category”参数直接传递到WP_Querycat.

EDIT

请注意,要排除类别,需要使用减号(-) 在类别ID前签名。类似的操作即可。只要换一下-13-ID 其中ID是要排除的类别的ID

function posts_by_year() {
  // array to use for results
  $years = array();
 
  // get posts from WP
  $posts = get_posts(array(
    \'numberposts\' => -1,
    \'orderby\' => \'post_date\',
    \'order\' => \'ASC\',
    \'post_type\' => \'my-custom-post-type\',
    \'post_status\' => \'publish\',
    \'category\' => -13
  ));
 
  // loop through posts, populating $years arrays
  foreach($posts as $post) {
    $years[date(\'Y\', strtotime($post->post_date))][] = $post;
  }
 
  // reverse sort by year
  krsort($years);
  return $years;
}

EDIT 2

按原样复制并粘贴此代码。你有一个语法错误,这就是为什么你得到了空白页

function posts_by_year() {
  // array to use for results
  $years = array();

  // get posts from WP
  $posts = get_posts(array(
    \'numberposts\' => -1,
    \'orderby\' => \'post_date\',
    \'order\' => \'ASC\',
    \'post_type\' => \'project\', 
    \'post_status\' => \'publish\',
    \'category\' => -27
  ));

  // loop through posts, populating $years arrays
  foreach($posts as $post) {
    $years[date(\'Y\', strtotime($post->post_date))][] = $post;
  }

  // reverse sort by year
  krsort($years);
  return $years;
}

EDIT 3

似乎您正在使用自定义分类法。如果是这种情况,请使用tax_query 有关可用参数,请参见WP_Query. 您只需在taxonomy 参数

function posts_by_year() {
  // array to use for results
  $years = array();

  // get posts from WP
$posts = get_posts(array(
    \'numberposts\' => -1,
    \'orderby\' => \'post_date\',
    \'order\' => \'ASC\',
    \'post_type\' => \'project\', 
    \'post_status\' => \'publish\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'NAME OF YOUR CUSTOM TAXONOMY\',
            \'field\'    => \'term_id\',
            \'terms\'    => \'27\',
            \'operator\' => \'NOT IN\'
        ),
    ),
  ));

  // loop through posts, populating $years arrays
  foreach($posts as $post) {
    $years[date(\'Y\', strtotime($post->post_date))][] = $post;
  }

  // reverse sort by year
  krsort($years);
  return $years;
}

结束

相关推荐

Child theme functions.php

我不知道如何改变我的孩子主题的功能。php。在woothemes文档中,它说“子主题中的functions.php应该是空的,并且不包括父主题functions.php中的任何内容。”我需要使用此功能来不显示产品类别,但我不确定如何在我的子主题中执行此操作。也许有人能给我提供一些指示?add_action( \'pre_get_posts\', \'custom_pre_get_posts_query\' ); function custom_pre_get_posts_query( $