Wordpress - 如何仅显示子类别

时间:2017-07-17 09:13:26

标签: wordpress

我有代码显示每个类别的帖子: 但是,如果仅显示每个类别(不是帖子)的子类别。怎么做? ***示例型号: 主页

  • -Category 1
  • ---子类别1
  • ----- Post 1
  • ----- Post 2
  • ---子类别2
  • ----- Post 1
  • ----- Post 2



<div class="box_inner cat_box list_posts_box first_post_left posts-v1" >
  <div class="news_box">
    <h3 class="news_box_title2">
		<a href="<?php echo get_category_link($GLOBALS['bd_cat_id']); ?> ">
			<?php echo get_cat_name($GLOBALS['bd_cat_id']);?>
		</a>
    </h3>
    <ul>
      <?php query_posts(array('showposts' => 1, 'cat' => $GLOBALS['bd_cat_id']  )); ?>
      <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
      <li class="first_news">

      <div <?php post_class(); ?>>
        <div class="inner_post">
          <div class="first-post-thumbnail">

            <a href="<?php the_permalink();?>" title="<?php printf( esc_attr__( '%s', 'bd' ), the_title_attribute( 'echo=0' ) ); ?>" rel="author">
	            <?php $timthumb = bdayh_get_option('timthumb'); if($timthumb == true) { ?>
	            	<img src="<?php echo BD_URI ?>/timthumb.php?src=<?php echo bd_post_image('large'); ?>&amp;h=150&amp;w=317&amp;zc=1" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
	            <?php } else { ?>
	            <?php

	                $thumb = bd_post_image('large');
	                $ntImage = aq_resize( $thumb, 317, 150, true );
					if($ntImage == '')
						{
							$ntImage = BD_IMG .'/default_thumb.png';
						}
	                ?>

		            <?php if (strpos(bd_post_image(), 'youtube')) { ?>
		            	<img src="<?php echo bd_post_image('large'); ?>" width="317" height="150" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
		            <?php } elseif (strpos(bd_post_image(), 'vimeo')) { ?>
		            	<img src="<?php echo bd_post_image('large'); ?>" width="317" height="150" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
		            <?php } elseif (strpos(bd_post_image(), 'dailymotion')) {?>
		            	<img src="<?php echo bd_post_image('large'); ?>" width="317" height="150" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
		            <?php } else { ?>
		            	<img src="<?php echo $ntImage; ?>" width="317" height="150"  alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
		            <?php } ?>
	            <?php } ?>
	            <span class="article-icon"><img src="<?php echo BD_IMG; ?>/spacer.gif" alt="" /></span>
            </a>
          </div><!--//post_thumbnail-->
          <h2>
            <a href="<?php the_permalink();?>" title="<?php printf( esc_attr__( '%s', 'bd' ), the_title_attribute( 'echo=0' ) ); ?>" rel="author">
            	<?php the_title();?>
            </a>

          </h2>

          <div class="post_meta">
            <a class="author_link" href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) )?>" title="<?php sprintf( esc_attr__( 'View all posts by %s', 'bd' ), get_the_author() ) ?>">
            	<?php echo get_the_author() ?>
            </a>
            <a class="date">
            	<?php the_time(get_option('date_format')); ?>
            </a>
              <span class="widget post-rat"><?php echo bd_wp_post_rate(); ?></span>
          </div>
          <p>
          <p><?php bd_excerpt_home() ?></p>
          </p>
        </div>
        </div>

      </li>
      <?php endwhile; endif;?>
      <?php wp_reset_query(); ?>

      <?php query_posts(array('showposts' => 4,'offset'=>1, 'cat' => $GLOBALS['bd_cat_id']  )); ?>
      <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
      <li class="posts-list-small">
        <div class="inner_post">
            <?php
            $img_w      = 55;
            $img_h      = 55;
            $thumb      = bd_post_image('full');
            $image      = aq_resize( $thumb, $img_w, $img_h, true );
            $alt        = get_the_title();
            $link       = get_permalink();
            if (strpos(bd_post_image(), 'youtube'))
            {
                echo '<div class="post_thumbnail"><a href="'. $link .'" title="'. $alt .'"><img src="'. bd_post_image('full') .'" width="'. $img_w .'" height="'. $img_h .'" alt="'. $alt .'" /></a></div><!-- .post-image/-->' ."\n";
            }
            elseif (strpos(bd_post_image(), 'vimeo'))
            {
                echo '<div class="post_thumbnail"><a href="'. $link .'" title="'. $alt .'"><img src="'. bd_post_image('full') .'" width="'. $img_w .'" height="'. $img_h .'" alt="'. $alt .'" /></a></div><!-- .post-image/-->' ."\n";
            }
            elseif (strpos(bd_post_image(), 'dailymotion'))
            {
                echo '<div class="post_thumbnail"><a href="'. $link .'" title="'. $alt .'"><img src="'. bd_post_image('full') .'" width="'. $img_w .'" height="'. $img_h .'" alt="'. $alt .'" /></a></div><!-- .post-image/-->' ."\n";
            }
            else
            {
                if($image) :
                    echo '<div class="post_thumbnail"><a href="'. $link .'" title="'. $alt .'"><img src="'. $image .'" width="'. $img_w .'" height="'. $img_h .'" alt="'. $alt .'" /></a></div><!-- .post-image/-->' ."\n";
                endif;
            }
            ?>
          <h2>
            <a href="<?php the_permalink();?>" title="<?php printf( esc_attr__( '%s', 'bd' ), the_title_attribute( 'echo=0' ) ); ?>" rel="author">
            <?php the_title();?>
            </a>
          </h2>
          <div class="post_meta">
            <a class="date">
            <?php the_time(get_option('date_format')); ?>
            </a>
              <span class="widget post-rat"><?php echo bd_wp_post_rate(); ?></span>
          </div>
        </div>
      </li>
      <?php endwhile; endif;?>
      <?php wp_reset_query(); ?>
    </ul>
  </div>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

不确定您希望在哪个部分代码中添加该列表,但我找到了一种方法来显示子类别下的帖子列表。

<ul>
<?php  $childCats = get_categories('child_of=6'); 

foreach ($childCats as $childCat) :
  $this_category = get_category($childCat);
  $args = array(
    'category__in' => array($childCat->term_id)
  );
  $childCatPosts = new WP_Query($args); 
  if ($childCatPosts->have_posts()) : ?>

  <li><?php echo $childCat->name; ?>

    <?php if ($this_category->category_parent != 0) { ?>

      <ul>
        <?php while ($childCatPosts->have_posts()) : $childCatPosts->the_post(); ?>

          <li><?php the_title(); ?></li>

        <?php endwhile; ?>
      </ul>

    <?php } ?>
  </li>

  <?php              
  endif; 
endforeach;     
?>
</ul>

请注意,'child_of = 6'代表父ID,这将检索类别对象列表。 然后,您需要遍历这些类别并根据子类别ID执行查询。

希望这会有所帮助:)

答案 1 :(得分:0)

在WordPress中显示具有子类别的帖子

在主页中添加以下代码

$category_args = array(
    'type'                     => 'post',
    'child_of'                 => 0,
    'parent'                   => 0,
    'orderby'                  => 'id',
    'order'                    => 'ASC',
    'hide_empty'               => 0,
    'hierarchical'             => 1,
    'exclude'                  => '',
    'include'                  => '',
    'number'                   => '',
    'taxonomy'                 => 'category',
    'pad_counts'               => false 
); 
$categories = get_categories( $category_args );

if(count($categories)>0)
{

    $categories = get_categories( $args );  
    foreach ( $categories as $category ) {
        ?>
        <h3 class="news_box_title2">
            <a href="<?php echo get_category_link( $category->term_id );  ?> ">
                <?php echo $category->name;?>
            </a>
        </h3>
        <?php
        $subcategories_args = array('parent'=> $category->term_id,'orderby'=> 'id','order'=> 'ASC','hide_empty'=> 0,'taxonomy'=> 'category');
        if(count($subcategories_args)>0)
        {
            ?>
            <ul>
            <?php
            foreach ( $subcategories_args as $subcategory ) {
                ?>
                    <li><a href="<?php echo get_category_link( $subcategory->term_id );  ?> ">
                        <?php echo $subcategory->name;?>
                    </a></li>
                    <?php
                    $post_category_taxonomy     = $subcategory->taxonomy;
                    $post_category_term_id      = $subcategory->term_id;

                    $listing_post_args=array(
                        'type'                     => 'post',
                        'post_status'              => 'publish',
                        'posts_per_page'           => 2,
                        'caller_get_posts'         => -1,
                        'child_of'                 => 0,
                        'parent'                   => 0,
                        'orderby'                  => 'name',
                        'order'                    => 'DESC',
                        'hide_empty'               => 0,
                        'hierarchical'             => 1,
                        'exclude'                  => '',
                        'include'                  => '',
                        'number'                   => '',
                        'tax_query'                => array(
                                                            array(
                                                                'taxonomy' => $post_category_taxonomy,
                                                                'field' => 'id',
                                                                'terms' => $post_category_term_id
                                                            )
                                                        ),
                        'pad_counts'               => false, 
                    );
                    $listing_post_my_query = null;
                    $listing_post_my_query = new WP_Query($listing_post_args);


                    if( $listing_post_my_query->have_posts() ) 
                    {
                        while ($listing_post_my_query->have_posts()) : $listing_post_my_query->the_post(); 
                        ?>
                        <a href="<?php echo get_permalink($post->ID); ?>"><?php echo get_the_title( $post->ID );?></a>
                        <?php
                         endwhile;
                    }
                    ?>
                <?php
            }
            ?>
            </ul>
            <?php
        }
    }
}
?>