Wordpress Category And Posts [closed]
We would like to know the Wordpress more about Wordpress Category print and then print Post in selected category.
Like in this style
<h2>Categories Name 1</h2>
<ul>
<li>Post1</li>
<li>Post2</li>
<li>Post3</li>
<li>P开发者_运维问答ost4</li>
</ul>
<h2>Category Name 2</h2>
<ul>
<li>Post1</li>
<li>Post2</li>
<li>Post3</li>
<li>Post4</li>
</ul>
Wordpress provides a function to return all categories (get_categories()
) and a function to list all posts of a category (WP Query
object).
With the combination of the two, you can create the output you would like to do. The following is some example code that can be extended / changed with the parameters you need:
$categories = get_categories();
foreach($categories as $category)
{
printf('<h2>%s</h2><ul>', $category->cat_name);
$posts = new WP_Query('cat='.$category->cat_ID);
while($posts->have_posts())
{
$posts->the_post();
echo '<li>', the_title(), '</li>';
}
print '</ul>';
}
精彩评论