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
iFrame, first of all, needs to be clarified for this question. iFrame is an HTML tag that allows us to embed and display a different web page within another web page. By using the iFrame element, we can seamlessly integrate another website into our own website or display a specific page from a different website […]
Read More
Senior Level Web/App AnalyticsThere are various ways to obtain backlinks to your website or to bring backlinks. While some of these methods are recommended by Google, others are not and can be harmful. What matters here is your strategy. No matter what strategy you follow, don’t compromise on naturalness. Now let’s take a look at 12 different methods […]
Read More
Mid Level SEOBacklink is a link that one website provides to another website. Backlinks should be obtained from target keywords and high-quality pages, as they are believed to be beneficial after on-page SEO is completed. Sometimes, surpassing or catching up with our competitors may not be possible by solely focusing on on-page SEO or off-page SEO. Therefore, […]
Read More
Beginner Level SEOWhat are the Learning Stages? In the Facebook advertising system, as in other advertising platforms, there is machine learning. With machine learning, the optimization of the ads provided is more accurately ensured. After the ads are published, factors that change the efficiency of the ad, such as target audience, ad space, and broadcast time, […]
Read More
Beginner Level Performance Marketing