Nat95 Nat95 - 1 month ago 18
PHP Question

Show only a certain category of posts

In my

page-home.php
the above section gets all the latest's posts.

elseif($key == 'latest_post_block'){ ?>
<?php if(of_get_option('latest_post_block_visibility') != '0'): ?>
<section id="blog" class="clearfix">
<div class="ak-container">
<?php if(of_get_option('latest_post_title') || of_get_option('latest_post_desc')): ?>
<div class="section-title-wrap">
<?php if(of_get_option('latest_post_title')): ?>
<h1 class="main-title"><?php echo esc_attr(of_get_option('latest_post_title'));?></h1>
<?php endif; ?>

<?php if(of_get_option('latest_post_desc')): ?>
<div class="sub-desc">
<?php echo esc_textarea(of_get_option('latest_post_desc'));?>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
</div>

<?php
if(of_get_option('latest_post_count') > 0) :
$args = array(
'posts_per_page' => of_get_option('latest_post_count')
);
$query = new WP_Query($args);
if($query->have_posts()):
?>
<div class="blog-block-wrapper clearfix">
<div class="ak-container">
<div class="block-block-wrap">
<?php
$count = 0;
while($query->have_posts()):
$query->the_post();
$count++;
$class = ($count % 2 == 0) ? "right-blog" : "left-blog";
?>
<div class="blog-block <?php echo $class; ?>">
<div class="blog-image">
<?php if(has_post_thumbnail()):
$big_image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'blog-thumbnail' );
?>
<a class="blog-img-wrap" href="<?php the_permalink(); ?>">
<img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>">
</a>
<div class="blog-overlay">
<div class="blog-anchor-wrap">
<a class="search fancybox-gallery" data-lightbox-gallery="gallery" href="<?php echo $big_image[0]; ?>"> <i class="fa fa-search"></i></a>
<a class="link" href="<?php the_permalink(); ?>"> <i class="fa fa-link"> </i> </a>
</div>
</div>
<?php
endif; ?>
</div>

<div class="blog-content-wrapper clearfix">
<div class="blog-date-wrap">
<div class="blog-date">
<?php echo get_the_date('d'); ?><br/>
<?php echo get_the_date('M'); ?>
</div>
</div>
<div class="blog-content">
<h3 class="blog-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a></h3>
<div class="blog-desc"><?php echo accesspress_letter_count(get_the_content(),'200'); ?></div>
</div>
<div class="clearfix"> </div>
<div class="blog-comments-wrap clearfix">
<div class="blog-comments">
<span><a class="author" href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>"> <i class="fa fa-user"> </i><?php echo get_the_author(); ?></a></span>
<span><a class="comment" href="<?php echo get_comments_link( $post->ID ); ?>"> <i class="fa fa-comments"> </i><?php echo get_comments_number(); ?></a></span>
<?php if(has_category()): ?>
<span>
<i class="fa fa-folder"></i><?php echo get_the_category_list(', '); ?>
</span>
<?php endif; ?>
<?php if(has_tag()): ?>
<span>
<i class="fa fa-tags"></i><?php echo get_the_tag_list('' , ', '); ?>
</span>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php
if($count % 2 == 0){
echo '<div class="clearfix"></div>';
}
?>
<?php
endwhile; ?>
</div>
</div>
</div> <!-- blog-block-wrapper end -->
<?php
endif;
wp_reset_postdata();
endif; ?>
</section> <!-- blog -->
<?php endif; ?>


However, I want only a certain category of posts, to be shown and not all of them. Can this happen?

Answer

Yes, you can modify the $args array like so:

$args = array(
    'posts_per_page' => of_get_option('latest_post_count'),
    'cat' => 4 // Category ID
);

Or, to query by category slug:

$args = array(
    'posts_per_page' => of_get_option('latest_post_count'),
    'category_name' => 'cat' // Category slug
);