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
What is Site Speed? Site speed refers to how quickly a web page loads. (Site speed has multiple factors. The most important of these are initial load time and load speed.) This speed directly affects the visitor experience. A slow-loading site can cause users to leave the site and choose other pages. Additionally, search engines […]
Read More
Mid Level SEOWhat is Structured Data? Structured data is a coding system used to help search engines better understand the content of a website. Implemented using formats like JSON-LD and Microdata, it enables the presentation of detailed information such as products, events, and business details in a comprehensible way. This is especially advantageous for e-commerce websites, as […]
Read More
Mid Level SEOIn the digital marketing world, user-generated content (UGC) is becoming increasingly important. UGC includes videos where users share their own experiences, opinions, and creativity in promoting brands and products. So, why are UGC videos so significant in digital marketing strategies? 1. Trustworthiness and Authenticity User-generated content creates a more trustworthy perception for consumers compared to […]
Read More
Performance MarketingWith the transition from Universal Analytics to Google Analytics 4, there may be some issues you need to resolve. One of these issues is “unassigned” traffic. Dimensions appearing as “unassigned” / (not set) in reports negatively impact your ability to analyze and optimize. We will discuss the causes of “unassigned” traffic in your GA4 reports […]
Read More
Beginner Level Web/App Analytics