从特定的父分类获取 Wordpress 子分类

5
我正在构建一个小的缩略图画廊,其中包含类别ID为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>';
   }

你在这里表达得不够清楚。你想要所有带有分类406的文章,即使它还有其他分类,还是只想要单独属于分类406的文章,或者是那些有分类且其子分类属于406的文章? - sven
我想获取所有来自406类别的文章,但有些文章也属于其他类别。而且我不知道如何检索406类别的子级。 - Ciprian
2个回答

11

所以现在我正在获取一个分类及其子分类列表,然后我获取已分配到该分类的父级分类的文章,获取文章数组后,我循环遍历它并获取分配给文章的所有分类,如果除了必需的分类及其子分类之外还有其他附加分类,则跳过该文章,否则执行我想做的任何操作。

$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();
上面的答案根据您的解释而言是正确的,但我认为您想要的是,比如您有一个艺术家类别,并且有具有特定名称的子类别。因此,对于艺术家画廊,您想要显示所有艺术家的列表及其图片。

谢谢帮忙。请检查我的编辑。我觉得我做错了。现在所有的文章都显示相同的类别。 - Ciprian
我不知道如何更好地解释它。我认为它非常清楚。我有一个 ID 为 406 的父类别。在这个父类别中有子类别。每个子类别中都有帖子。其中每篇帖子都位于 406 子类别以及其他类别中。我想检索这些帖子以及 406 子类别。没有其他类别。 - Ciprian
是的,但如果这些帖子也在另一个类别中怎么办?我刚刚编辑了我的问题。也许如果我向您展示我的整个代码,就会清楚了。我需要显示缩略图的类别名称和slug。 - Ciprian
请检查这是否适用于您,我已排除所有其他帖子,仅处理类别不是类别406或其子类别之一的帖子。 - sven

0

我也需要获取所选父类别的子类别列表及其URL,

我发现以下代码非常有用

<?php

$categories = get_categories( array(
  'orderby' => 'name',
  'parent'  => 5
) );

foreach ( $categories as $category ) {

   printf( '<li><a  class="btn btn-light btn-light-custom mt-2" href="%1$s">%2$s</a></li>',
      esc_url( get_category_link( $category->term_id ) ),
      esc_html( $category->name )
  );
}

?>

请将“5”替换为您选择的父类别ID


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接