【问题标题】:List categories / child categories / grandchild categories / post titles列出类别/子类别/孙类别/帖子标题
【发布时间】:2015-02-17 09:35:38
【问题描述】:

我找到了这段代码:

<?php 
$args = array(
  'orderby' => 'id',
  'hide_empty'=> 0,
  'child_of' => 2,
'depth' => 5,
);
$categories = get_categories($args);
 foreach ($categories as $cat) {
    echo '<div>';
    echo '<h1>'.$cat->name.'<img src="'.$cat->term_icon.'" alt=""  class="alignleft"/>'.'<br />'.'<span class="solutions">'.$cat->description.'</span>'.'</h1>';
    //echo '<br />';     
 $args3= array("orderby"=>'name', "category" => $cat->cat_ID, 'depth' => 5); // Get Post from each Sub-Category
    $posts_in_category = get_posts($args3);
    foreach($posts_in_category as $current_post) {
        echo '<span>';
        ?>
        <li><h1><a href="<?=$current_post->guid;?>"><?=$current_post->post_title;?></a></li></h1>
        <?php
        echo '</span>';
    }
    echo '</div>';
}
?>

这列出了所有类别和类别中的帖子。但我想要它来自所有类别。但是当我填写'child_of' =&gt; 2 时,它会列出所有内容,但格式不正确。孙子与子孙具有相同的等级地位。

例如我想要的:

  • 父母
    • 孩子 1
      • 孙子 1
        • 发布 1
      • 孙子 2
        • 发布 2
    • 孩子 2
      • 发布 3

所以:所有的猫都应该能够处理帖子,如果只有孙辈的帖子,只列出那些......谢谢! -edit- 实际上它应该与 wp_list_categories 相同,只有我应该能够分别编辑父母、孩子、孙子和帖子标题。 (例如,我必须能够从子类别中删除 href,但不能从孙子类别中删除 ..

【问题讨论】:

    标签: wordpress categories


    【解决方案1】:

    试试这个:

       <!-- Category Archive Start -->
        <ul class="catArchive">
        <?php
        $catQuery = $wpdb->get_results("SELECT * FROM $wpdb->terms AS wterms INNER JOIN $wpdb->term_taxonomy AS wtaxonomy ON ( wterms.term_id = wtaxonomy.term_id ) WHERE wtaxonomy.taxonomy = 'category' AND wtaxonomy.parent = 0 AND wtaxonomy.count > 0");
    
        $catCounter = 0;
    
        foreach ($catQuery as $category) {
    
            $catCounter++;
    
            $catStyle = '';
            if (is_int($catCounter / 2)) $catStyle = ' class="catAlt"';
    
            $catLink = get_category_link($category->term_id);
    
            echo '<li'.$catStyle.'><h3><a href="'.$catLink.'" title="'.$category->name.'">'.$category->name.'</a></h3>';
                echo '<ul style="margin-left:15px;">';
    
                query_posts('category__in='.$category->term_id.'&showposts=5');?>
    
                <?php while (have_posts()) : the_post(); ?>
                    <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
                <?php endwhile; ?>
    
                    <li><a href="<?php echo $catLink; ?>" title="<?php echo $category->name; ?>">More <strong><?php echo $category->name; ?></strong></a></li>
             <li> <?php
             $sub_cat_id = $category->term_id;
             $get_sub_args = array('child_of' =>$sub_cat_id);
                     $categories_arr =  get_categories($get_sub_args);  
                     //print_r ($categories_arr);
            foreach  ($categories_arr as $sacategory) {
                    //Display the sub category information using $category values like $category->cat_name
                    echo '<h2>'.$sacategory->name.'</h2>';
                    echo '<ul style="margin-left:15px;">';
    
                    foreach (get_posts('cat='.$sacategory->term_id) as $post) {
                        setup_postdata( $post );
                        echo '<li><a href="'.get_permalink($post->ID).'">'.get_the_title().'</a></li>';   
                    }  
                    echo '</ul>';
                }
    
             ?></li>
    
    
    
                </ul>
            </li>
            <?php } ?>
        </ul>
        <!-- Category Archive End -->
            </div>
    

    【讨论】:

    • 很有魅力!一个小通知:当顶部类别中没有帖子时,它现在会消失。你能实现一个$args 这样我就可以填写'hide_empty' =&gt; 0, 吗?非常感谢!
    • 完成!你能告诉我如何添加 hide_empty => 0 吗?真的需要那个!谢谢!!
    • 如果您在第一次查询所选类别是否有帖子后检查“wp_post”表? $test=get_posts('category_name=test'); if ($test) { //找到的帖子 }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    相关资源
    最近更新 更多