Recently Added

A'dan Z'ye Call to Action

Sunduğunuz ürün veya hizmetlerin...

Programatik Reklamcılık

Programatik reklamcılık, geleneksel dijital...

Söz Uçar, Helvetica Kalır

Bir çay ocağını kafe yapmanın en kestirme...

Displaying Posts of Specific Categories in WordPress

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. 

Volkan Levent Soylu / Jr. Frontend Developer