How to view the list of posts by WordPress category

How to view the list of posts by WordPress category


Since the creation of this site, I’ve been regularly resorting to your questions to write posts… and here’s one I’ve received several times: How to list posts by category in WordPress ?

In this article, I’ll show you the method you can use to create a list of posts sorted by category without digging too much into the code… and also how to list the posts associated with a specific label in WordPress.

Step 1 – Install an extension that allows using PHP code

The code that makes it possibleView a list of posts by category in WordPress is written in PHP language. For example, to easily display it in an article, you can use the free Insert PHP Code Snippet extension. It allows you to memorize the pieces of code of your choice and turn them into shortcode to paste where you want (in an article, on a page, etc.).

Simply install the plugin via the “Extensions” > “Add” menu in WordPress and activate it. A new menu item called “XYZ PHP Code” will be created in the WordPress admin.

Install an extension to manage PHP code
Install an extension to manage PHP code

To add a new code, go to the PHP Code Snippets submenu and Click on “Add new PHP code snippet”.

Create a new shortcode

Step 2 – The code to display the list of articles by category

The code I’m going to provide you with now allows this to be doneView a list of articles on your site by category. The category title is highlighted with an h2 tag, they are displayed in alphabetical order, and the posts are then listed as a list from newest to oldest, like so:

List posts by category in WordPress
List posts by category in WordPress

The code to use

In PHP code snippets, name your code, for example “postsparcat”… and paste the following PHP code:

$args = array(
        'orderby' => 'name',
        'order' => 'ASC',
        'taxonomy' => 'category'
    );

    $categories = get_categories( $args );

    foreach( $categories as $category ) {
        $args = array(
            'post_type' => 'post',
            'posts_per_page' => -1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'category',
                    'field' => 'name',
                    'terms' => $category->name
                )
            )
        );
        $posts = get_posts( $args );
        echo '<h2>' . $category->name. '</h2>';
        echo '<ul>';
        foreach( $posts as $post ) {
            echo '<li><a href="' . get_permalink( $post->ID ) . '">' . $post->post_title . '</a></li>';
        }
        echo '</ul>';
    }

Once the code is saved, PHP Code Snippets will automatically generate a shortcode. [xyz-ips snippet="postsparcat"] In this particular case it is possible paste into a post or page for the list to appear on your blog.

What does this code say? It asks WordPress to get the list of categories in alphabetical order using the “get_categories()” function and the “orderby” and “order” parameters (which define that the categories are retrieved by name, using them in ascending order – ASC ) are classified. .

For each category (foreach( $categories as $category )) we will then list the related items.

Some notions of personalization

By default, only 5 items are displayed for each category. If you want to show more, you need to add the “posts_per_page” parameter. to this part of the code:

$categories = get_categories( $args );

    foreach( $categories as $category ) {
        $args = array(
            'post_type' => 'post',
            'posts_per_page' => 10,
            'tax_query' => array(
                array(
                    'taxonomy' => 'category',
                    'field' => 'name',
                    'terms' => $category->name

In the example above, I’m displaying 10 items. If you want to list all, replace “10” with “-1”.

You can also Change the order in which categories are retrievedby playing with the “orderby” parameter. In the example I gave you, “orderby” was set to “name” (so we retrieved categories based on their name)… but we can use the “term_id” parameter instead if you want to retrieve them in order her creation.

If you want to change the order in which the items themselves are listed, you need this time Add an “orderby” parameter to control the order in this part of the code:

$categories = get_categories( $args );

    foreach( $categories as $category ) {
        $args = array(
            'post_type' => 'post',
	    'orderby' => 'title',
	    'order' => 'ASC',
            'tax_query' => array(
                array(
                    'taxonomy' => 'category',
                    'field' => 'name',
                    'terms' => $category->name

You can choose from the sorting parameters provided by WordPressFor example :

  • “Title” to sort the articles by title (which I show in my example);
  • “ID” to classify categories based on their unique identifier;
  • “Date” to sort them by date;
  • “Modified” to sort them by last modified date;
  • “rand”, for random ranking.
  • comment_count to rank articles by the number of comments received.

Likewise, the order can be ascending (“ASC”) or descending (“DESC”).

For example, here is a sort of posts by alphabetical order :

Classification of articles in alphabetical order
Classification of articles in alphabetical order

What if it doesn’t work?

Of course I tested the provided code before sharing it with you. However, if you encounter a problem, make sure you haven’t forgotten to copy a punctuation mark. Everything counts in PHP! Omitting a comma, a parenthesis, not closing a quotation mark leads to a blank page.

It can also be important if there are upper and lower case letters in certain places!

Note that you are free to test your customizations Copy the shortcode into a draft post or page, and use WordPress’ “preview” mode to see if your changes work…instead of publishing the content directly. So in case of a display problem you don’t take any risks, only you see the error!

PHP code
PHP code

Can we list articles by tags in WordPress?

It’s kind of the logical continuation of the first part of this tutorial: and if this time you need to create a listing by tag (“label”), until Get the list of WordPress posts for a specific tag ?

That too is entirely possible. The methodology is the same, with creating a new shortcode for PHP code snippets. For example, this time you can name it “postspartag”… and copy this code:

$tags = get_tags();
foreach ( $tags as $tag ) {
    echo '<h2>' . $tag->name . '</h2>';
    $args = array(
        'tag_id' => $tag->term_id,
        'posts_per_page' => -1
    );
    $posts = get_posts( $args );
        echo '<ul>';
        foreach( $posts as $post ) {
            echo '<li><a href="' . get_permalink( $post->ID ) . '">' . $post->post_title . '</a></li>';
        }
        echo '</ul>';
    wp_reset_postdata();
}

This time we use the get_tags function to get the tags. We then display the title of each tag surrounded by h2 tags. After this title, we use the get_posts function to retrieve and list the articles associated with each tag.

As in the previous example, you can change the posts_per_page line to show more or fewer posts.

I hope this tutorial will help you easily use these features on your website!

Marlene Viancinmarlene

Hello ! Me, it’s Marlene. As an SEO manager in life, I’ll give you advice on how to create a website and become more visible on the web, thanks in particular to natural referencing. If after reading this article you have a question or want to react, don’t hesitate to leave a comment!



Source link

Related Posts

Leave a Reply

Open chat
Scan the code
Hello 👋
Can we help you?