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
With 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 AnalyticsEarlier this year, Google Cloud announced that BigQuery data warehouse supports automatic data transfer from Facebook Ads. In its preview stage, this feature offers an alternative way to enhance analysis and insights by allowing the scheduling of data loading tasks. With this integration, the need for third-party tools or manual code execution is eliminated. This […]
Read More
Mid Level Web/App AnalyticsBy combining the app and web data in Google Analytics 4, app analysis has become more easily trackable. Firebase Analytics allows you to easily track your iOS or Android app with GA4. With numerous different tools available for mobile app tracking, being able to see both web and app data within the same property is […]
Read More
Mid Level Web/App AnalyticsBusinesses want to use as much data as possible from analytics and marketing cookies. However, in order to collect and use this data, they need to comply with laws such as KVKK/GDPR. By enabling Cookie Mode, Google helps to use cookies according to the level of consent. In other words, with Cookie Mode, users’ privacy […]
Read More
Mid Level Web/App Analytics