Category in WordPress – In this article, I will talk about how to display new posts by Category in WordPress, this topic has probably been written by many Websites already but mostly using plugins. For developers who work a lot with WordPress, this is probably a pretty simple problem, but for those of you who are new to learning and coding the function yourself, it is quite important for the beginning.Let’s get started!Show new posts by Category in WordPressShow new posts by Category in WordPress– First, you need to learn about Loop (Loop) to get the list of posts in WordPress<?php while (have_posts()) : the_post(); ?>
// informative articles
<?php endwhile ; wp_reset_query() ;?>WordPress supports you to get articles easily with the While loop as above, With this loop by default, you will output all posts in Post_type as Post (posts in the POST section on the admin page). value) in my Website– So we need to add some conditions for this loop to be able to get articles by Category<?php
$vnkings = new WP_Query(array(
‘post_type’=>’post’,
‘post_status’=>’publish’,
‘cat’ => 1,
‘orderby’ => ‘ID’,
‘order’ => ‘DESC’,
‘posts_per_page’=> 4));
?>
<?php while ($vnkings->have_posts()) : $vnkings->the_post(); ?>
// các thông tin bà i viết
<?php endwhile ; wp_reset_query() ;?>Explain :new WP_Query() : Create new Query with inner condition‘post_type’=>’post’ : Display posts in Post type as Post‘post_status’=>’publish’ : Display posts with status as published‘cat’ => 1: Display posts in Category with ID 1 (this is the crux of this post)‘order by => ‘ID’,’order’ => ‘DESC’ : Display posts from new to old‘posts_per_page’=> 4 : Display post count as 4$vnkings->have_posts() : Apply the above query to the WordPress loop lặpDetailed example:We will make a box to display posts by Advanced WordPress Category like Vnkings.com–Create a section displaying the category name and Link:<div class="title_category"><a href="<?php echo get_category_link( id_của_category ); ?>"><?php echo get_cat_name( id_của_category);?></a></div>— Create a Loop that displays the articles of this Category:<?php
$vnkings = new WP_Query(array(
'post_type'=>'post',
'post_status'=>'publish',
'cat' => id_cá»§a_category,
//thay id_của_category bằng id danh mục bạn muốn hiển thị nhé
'orderby' => 'ID',
'order' => 'DESC',
'posts_per_page'=> 5));
?>
<?php $i=1; while ($vnkings->have_posts()) : $vnkings->the_post(); ?>
<?php if($i==1){ ?>
<div class="bai_dau_tien">
<a href="<?php the_permalink() ;?>" class="anh_bai_viet">
<?php the_post_thumbnail("thumbnail",array( "title" => get_the_title(),"alt" => get_the_title() ));?>
</a>
<a href="<?php the_permalink() ;?>" class="tieu_de_bai_viet"><?php the_title() ;?></a>
<p class="trich_dan">
<?php the_excerpt() ;?>
</p>
</div>
<?php } else { ?>
<div class="cac_bai_con_lai"><a href="<?php the_permalink() ;?>"><?php the_title() ;?></a> </div>
<?php } ?>
<?php $i++; endwhile ; wp_reset_query() ;?>Explanation:for the purpose of displaying the first post differently from the rest in the loop, I have assigned a variable i=1, then check if i == 1it will be displayed div text, otherwise, the next posts will be displayed as default. This is pretty basic knowledge in PHP, if you don’t understand it, you can leave a comment with your question– A little advanced– if you want to get the post in the category in a Post type other than the default WordPress Post, then how?You just need to change the paragraph'tax_query' => array(
array(
'taxonomy' => 'post_type_taxonomy_của bạn',
'field' => 'id',
'terms' => 'ID_product_portfolio
)
),You have a Post type of Product, and a Taxonomy containing a category of product_cat,
then you will have the following code<?php
$vnkings = new WP_Query(array(
'post_type'=>'product',
'post_status'=>'publish',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => 'ID_Của_Danh_Mục_Sản_Phẩm'
)
),
'orderby' => 'ID',
'order' => 'DESC',
'posts_per_page'=> '4'));
?>
<?php while ($vnkings->have_posts()) : $vnkings->the_post(); ?>
// Nội dung từng sản phẩm
<?php $i++; endwhile ; wp_reset_query() ;?>After displaying new articles by category, apply your CSS knowledge to beautify this section. Good luck!