Add Snippet To Project
Navigating your website should be a seamless experience for your visitors, and one effective way to ensure that is by listing posts by category in your navigation. Such an organized system allows users to find the content they’re interested in with minimal fuss, enhancing user experience significantly. Whether you’re a seasoned WordPress user or just starting, you’ll find this guide meaningful. We will walk you through the process of seamlessly listing posts by category in your WordPress website’s navigation in this article.
function wpturbo_list_posts_by_category() {
$categories = get_categories();
foreach ($categories as $category) {
echo '<li><a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a></li>';
$args = array(
'category' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => -1
);
$posts_in_category = get_posts($args);
if(!empty($posts_in_category)) {
echo '<ul>';
foreach ($posts_in_category as $post) {
echo '<li><a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a></li>';
}
echo '</ul>';
}
}
}
add_action('wp_nav_menu_items', 'wpturbo_list_posts_by_category');
Let’s decode this PHP code snippet line by line.
The code starts by defining a function named wpturbo_list_posts_by_category()
, and as the name suggests, this function’s job is to list posts by category. Within this function, we are applying various WordPress functions to retrieve relevant data and iterate over them.
Firstly, we make a call to get_categories()
function. This function is a built-in WordPress function and retrieves all of your WordPress categories as an array of category objects.
$categories = get_categories();
We then loop through each category using a foreach
loop.
foreach ($categories as $category) {
Within this loop, we generate an HTML list item (<li>
) and hyperlink (<a>
) for each category and output that using the echo
function. The category URL is generated using another built-in WordPress function get_category_link()
, and the visible text of the hyperlink is the category’s name.
echo '<li><a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a></li>';
Up next, we generate a query in the form of an associative array to fetch all posts from the current category in the loop.
$args = array(
'category' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => -1
);
We then pass these arguments to the get_posts()
function which retrieves the posts.
$posts_in_category = get_posts($args);
Next, we check if there are any posts in the category using ! empty()
function.
If there are posts, an unordered list (<ul>
) is started.
if(!empty($posts_in_category)) {
echo '<ul>';
We loop through each post in the category, and for each post, we generate an HTML list item (<li>
) and hyperlink (<a>
). The post URL is generated using get_permalink()
function, and the visible text of the hyperlink is the post’s title.
foreach ($posts_in_category as $post) {
echo '<li><a href="' . get_permalink($post->ID) . '">' . $post->post_title . '</a></li>';
}
Once all posts in the category have been listed, we close the unordered list (</ul>
).
echo '</ul>';
Finally, outside the categories foreach loop but still within the function, we hook the wpturbo_list_posts_by_category()
function to the wp_nav_menu_items
action. This ensures that our custom function is executed, and posts are listed by category in the navigation.
add_action('wp_nav_menu_items', 'wpturbo_list_posts_by_category');
With this code, every time your WordPress navigation is created, an item will be added for each category, and within that, a nested list of all posts within that category.