Display Post In WordPress

In this post I’m going to tell you how to display all post in a wordpress, or display posts by categories in wordpress. There are two ways  to display posts or display posts by category.

// the query
$query = new WP_Query(array(‘post_type’=>’post’, ‘post_status’=>’publish’, ‘posts_per_page’=>-1)); ?>
<?php if ( $query ->have_posts() ) : ?>
<!– the loop –>
<?php while ( $query >have_posts() ) : $query ->the_post(); ?>
<li><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></li>
<?php endwhile; ?>
<!– end of the loop –>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( ‘Sorry, no posts matched your criteria.’ ); ?></p>
<?php endif; ?>

post_type=>post if you are using custom post type then change post to your custom post name
posts_per_page=>-1 show unlimited post on this page if you want limited post then set limit replace -1 by limit(10,20) as your requirement

Display Posts By Category by Coding

To display all posts in a wordpress page , post or widget we’ll be using coding so if you want to install wordpress  then follow this tutorial first how to install wordpress  make function in functions.php file with the below code.

$query = new WP_Query(array(‘post_type’=>’post’,’cat’=>'{category ID}’, ‘post_status’=>’publish’, ‘posts_per_page’=>-1));

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – we are here to solve your problems.

I, Dayanand Saini,the founder and chief editor of www.webtechsource.com,have been working as a software engineer from last 4+ years. It's my hobby to learn new things and implement on live environment.

Leave a Reply

Your email address will not be published. Required fields are marked *

36 + = 41

%d bloggers like this: