27.09.2022
If you are working on a project in WordPress, you should know about the components such as thumbnail, product, and category, and, how to patch up with PHP becomes crucial.
In this article, I want to show you one of the simplest ways of how to display posts from multiple categories with using PHP.
WordPress is based on a structure to create, categorize, and publish posts. By default, every single post is under the main category. It won’t cause any issue if you prefer to work on products only in the main category, instead of multiple categories. However, things might get complicated when you have more than one category.
In this example, there are three categories (category-1, category-2, and category-3). And I want to display all of them, by creating an array for each one of them.
'orderby' => 'date' ,
'order' => 'DESC' ,
'posts_per_page' => 3,
'paged' => get_query_var('paged'),
'post_parent' => $parent
); ?>
As this example shows clearly, in this array, it is possible to create arguments such as arranging the posts by date, also not including more than three posts.
When we want to call posts from specific categories, the only thing we should be doing is to add a new argument, as ‘category_name‘.
'category_name' => 'category-1',
Since we used this name when creating the category from the panel before, PHP won’t have a problem defining this argument.
Using multiple arrays allows me to call different categories. To do that, all I have to do is to create a new array and type the category name I want as an argument.
<?php $args = array(
'post_type' => 'post' ,
'orderby' => 'date' ,
'order' => 'DESC' ,
'posts_per_page' => 3,
'category_name' => 'category-1',
'paged' => get_query_var('paged'),
'post_parent' => $parent
); ?>
<?php query_posts($args); ?>
Using multiple arrays allow me to call different categories. To do that, all I have to do is create a new array and type the category name I want to call as an argument.
In the example below, arrays which I’ve created for each of the three categories are visible. You can then use the have_post() function on each to specify the area where the posts come from. Then you can define them as you want them to appear on HTML.
<!-- calling category-1-->
<?php $args = array(
'post_type' => 'post' ,
'orderby' => 'date' ,
'order' => 'DESC' ,
'posts_per_page' => 3,
'category_name' => 'category-1',
'paged' => get_query_var('paged'),
'post_parent' => $parent
); ?>
<?php query_posts($args); ?>
<!-- calling posts from category-1 -->
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="category-1">
---
</div>
<?php endwhile; ?>
<?php endif; ?>
<!-- calling category-2 -->
<?php $args = array(
'post_type' => 'post' ,
'orderby' => 'date' ,
'order' => 'DESC' ,
'posts_per_page' => 3,
'category_name' => 'category-2',
'paged' => get_query_var('paged'),
'post_parent' => $parent
); ?>
<?php query_posts($args); ?>
<!-- calling posts from category-2 -->
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="category-2">
---
</div>
<?php endwhile; ?>
<?php endif; ?>
<!-- calling category-3 -->
<?php $args = array(
'post_type' => 'post' ,
'orderby' => 'date' ,
'order' => 'DESC' ,
'posts_per_page' => 3,
'category_name' => 'category-3',
'paged' => get_query_var('paged'),
'post_parent' => $parent
); ?>
<?php query_posts($args); ?>
<!-- calling posts from category-3 -->
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="category-3">
---
</div>
<?php endwhile; ?>
<?php endif; ?>
There are many ways to display categories such as using category id or for each. Category_name is one of the easiest and most comprehensible ways.
Perfist Blog
Similar Articles
In this guide, we will explain what a session is in Google Analytics 4 (GA4), why session duration matters, which session metrics are available, and how to configure these settings. What is a Session in GA4? A session represents the group of interactions a user has with your website or mobile app within a given […]
Read More
Beginner Level Web/App AnalyticsWhat is GEO (Generative Engine Optimization)? GEO is a new generation optimization method that ensures content stands out in AI-supported search systems. It aims to produce content that can provide quick and clear answers to user questions. In addition to traditional SEO, a simple and understandable language that appeals to artificial intelligence is used. Why […]
Read More
Mid Level SEOGEO is an optimization method developed to ensure that content is better understood and recommended by AI-powered search engines. Previously, when writing content, the goal was solely to rank high on Google. Now, however, AI systems read these contents and present summaries to users. This is exactly where GEO steps in. It ensures that content […]
Read More
Mid Level Content MarketingUnderstanding how visitors reach your website is crucial for measuring the success of your marketing strategies. Google Analytics 4 (GA4) provides the Channel Grouping feature to analyze traffic sources. With channel grouping, you can categorize visitor traffic into specific segments and make more informed marketing decisions. What is Channel Grouping in GA4? GA4’s channel […]
Read More
Beginner Level Web/App Analytics