从特定父级获取 Wordpress 子类别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19500735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:23:50  来源:igfitidea点击:

Get Wordpress child category from specific parent

wordpress

提问by Ciprian

I'm building a small thumbnail gallery with posts from a category that has the ID 406.

我正在构建一个小型缩略图库,其中包含来自 ID 为 406 的类别的帖子。

Some of the posts are in multiple categories and I'm not sure how to grab the category name that is a child of 406. $post_cat[0]->name;returns a category, but I only need it to return children of 406.

有些帖子属于多个类别,我不确定如何获取属于 406 的子类别的类别名称。$post_cat[0]->name;返回一个类别,但我只需要它来返回 406的子类别。

$thumbnails = get_posts('posts_per_page=10&cat=406');

foreach ($thumbnails as $thumbnail) {

   $args = array(
     'type' => 'post',
     'child_of' => 0,
     'parent' => 406,
   );
   $categories = get_categories ( $args );

   foreach ( $categories as $category) {
      $cat_name = $category->name;
      $cat_slug = $category->slug;
   }

    echo $cat_name;
    echo $cat_slug;

 }

*EDIT**

*编辑**

$thumbnails = get_posts('posts_per_page=10&cat=406');
foreach ($thumbnails as $thumbnail) {

  $post_cat = get_the_category( $thumbnail->ID );
  echo '<li><a class="side-thumb" href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';

  if ( has_post_thumbnail($thumbnail->ID)) {

   echo get_the_post_thumbnail($thumbnail->ID, 'thumbnail');
   $upload_dir = wp_upload_dir();
   $art_image = ''.$upload_dir['basedir'].'/icons/'.$post_cat[0]->slug.'.png';

   if (file_exists($art_image)) {

     echo '<p class="artist-latest"><img src="http://site.com/wp-content/uploads/icons/'.$post_cat[0]->slug.'.png" alt="'.$post_cat[0]->slug.'"/>'.$post_cat[0]->name.'</p>';

    } else{
        echo '<p class="artist-latest">'.$post_cat[0]->name.'</p>';
    }

    } else {

    }
    echo '</a></li>';
   }

回答by sven

So now I'm fetching a list of category and its child, then I fetch posts having parent category assigned to it, after I get posts array, I loop into it and get all the categories assigned to the post, If there are additional categories other than the required category and its child I skip the post, otherwise do whatever I want to do.

所以现在我正在获取一个类别列表及其子项,然后我获取分配给它的父类别的帖子,在我获得帖子数组后,我循环进入它并获取分配给帖子的所有类别,如果还有其他类别除了所需的类别及其子项之外,我跳过该帖子,否则做我想做的任何事情。

$category = get_category_by_slug( 'category-name' );

$args = array(
'type'                     => 'post',
'child_of'                 => $category->term_id,
'orderby'                  => 'name',
'order'                    => 'ASC',
'hide_empty'               => FALSE,
'hierarchical'             => 1,
'taxonomy'                 => 'category',
); 
$child_categories = get_categories($args );

$category_list = array();
$category_list[] = $category->term_id;

if ( !empty ( $child_categories ) ){
    foreach ( $child_categories as $child_category ){
        $category_list[] = $child_category->term_id;
    }
}

$posts_args = array(
'posts_per_page'   => 10,
'cat'  => $category->term_id,
'post_type'        => 'post',
'post_status'      => 'publish',
'suppress_filters' => true 
);

$posts = new WP_Query ( $posts_args );
if ( $posts->have_posts() ){

  while ( $posts->have_posts() ){
    $posts->the_post(); 
    $category_array = array();
    $post_categories = get_the_category ( get_the_ID() );
    if ( !empty ( $post_categories ) ){
       foreach ( $post_categories as $post_category ) {
          $category_array[] = $post_category->term_id;
        }
    }
    //Checks if post has an additional category
    $result = array_diff( $category_array,  $category_list   ); 

    if ( empty ( $result ) ) { ?>
       <li>
         <a href="<?php the_permalink(); ?>" class="side-thumb" title="<?php the_title(); ?>"> dfdf<?php 
             if ( has_post_thumbnail() ){
                 echo get_the_post_thumbnail();
             } 
             //Put your default icon code here 
             ?>
         </a> 
        </li> <?php
     }
   }
  }
  wp_reset_postdata();

Above answer is as per your explanation, but I think what you want is, like you have a category Artists and there are subcategories each having a specific name, So for Artist gallery you want to display list of all the artists with their pictures.

上面的答案是根据您的解释,但我认为您想要的是,就像您有一个艺术家类别,并且每个子类别都有一个特定的名称,因此对于艺术家画廊,您希望显示所有艺术家及其图片的列表。