个性化阅读
专注于IT技术分析

如何遍历WordPress分类

我是Wordpress的新手, 一直在努力创建类别循环。该循环应该:

  1. 遍历所有类别
  2. 回显类别名称(带有指向
  3. 回显该类别中的最后5个帖子(带有永久链接)

每个HTML将是

<div class="cat_wrap"> <div class="cat_name"> <a href="<?php get_category_link( $category_id ); ?>">Cat Name</a> </div> <ul class="cat_items"> <li class="cat_item"> <a href="permalink">cat item 1</a> </li> <li class="cat_item"> <a href="permalink">cat item 2</a> </li> <li class="cat_item"> <a href="permalink">cat item 3</a> </li> <li class="cat_item"> <a href="permalink">cat item 4</a> </li> <li class="cat_item"> <a href="permalink">cat item 5</a> </li> </ul> </div> 

请帮忙


#1


糟糕, 错过了你想要5个帖子

<?php //for each category, show 5 posts $cat_args=array( 'orderby' => 'name', 'order' => 'ASC' ); $categories=get_categories($cat_args); foreach($categories as $category) { $args=array( 'showposts' => 5, 'category__in' => array($category->term_id), 'caller_get_posts'=>1 ); $posts=get_posts($args); if ($posts) { echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> '; foreach($posts as $post) { setup_postdata($post); ?> <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> <?php } // foreach($posts } // if ($posts } // foreach($categories ?> 

#2


在这里保持简单是解决问题的方法

<?php wp_list_categories('show_count=1&title_li=<h2>Categories</h2>'); ?> 

#3


我做了这段代码来循环浏览嵌套的类别。分享。

 //Start on the category of your choice ShowCategories(0); function ShowCategories($parent_category) { $categories = get_categories(array('parent' => $parent_category, 'hide_empty' => 0)); foreach ($categories as $category) { ?><ul><li><?=$category->cat_name;?><? ShowCategories($category->cat_ID); ?></li></ul><? } } 

#4


看一下另一个Stackoverflow线程:

https://wordpress.stackexchange.com/questions/346/loop-through-custom-taxonomies-and-display-posts/233948#233948

我发布了一个我在生产中使用的答案, 就像一个魅力。

只需记住调整参数以仅显示5个帖子, 而不是全部。

$args = array( 'showposts' => 5 ); 

在循环遍历每个类别的帖子的循环中, 将’showposts’=> 5添加到当前的参数数组中。

赞(0)
未经允许不得转载:srcmini » 如何遍历WordPress分类

评论 抢沙发

评论前必须登录!