【问题标题】:Get Wordpress child category from specific parent从特定父级获取 Wordpress 子类别
【发布时间】:2013-10-21 17:14:09
【问题描述】:

我正在构建一个小型缩略图库,其中包含来自 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;

 }

*编辑**

$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 的子项?
  • 我想要所有来自 cat 406 的帖子,但有些帖子也属于其他类别。而且我不知道如何检索 406 类别子项。

标签: wordpress


【解决方案1】:

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

$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();

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

【讨论】:

  • 感谢您的帮助。请检查我的编辑。我不认为我这样做是正确的。现在它为所有帖子显示相同的类别。
  • 我不知道如何更好地解释它。我认为这很清楚。我有一个 id 为 406 的父类别。在这个父类别中有子类别。每个子类别中都有帖子。这些帖子中的每一个都属于 406 子类别以及其他类别。我想检索这些帖子以及 406 子类别。不是任何其他类别。
  • 是的,但是如果这些帖子也属于另一个类别怎么办?我刚刚编辑了我的问题。如果我向您展示我的整个代码,也许会很清楚。我需要类别名称和 slug 来显示缩略图。
  • 检查这是否适合您,我已排除所有其他帖子,仅处理类别为 406 或其子类别之一的帖子
【解决方案2】:

我还需要通过 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-09
    • 2022-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多