How many type we can get post and custom posts types
WordPress provides many ways to get posts and custom posts type. All methods described below one by one:-
1) The normal loop:-
WordPress provide normally a loop to get a post and page content and you can also found this snippet of code in your page.php and index.php of your theme directory.
while (have_posts()) : the_post(); the_title(); the_content(); endwhile;
2) By query post method:- we use query_post() method to modify normal post loop. You could use this WordPress to shows both normal posts and posts inside portfolio type. You have to pass all the necessary parameters to run this query.
query_posts(array( 'post_type' = 'portfolio', 'showposts' = 10 ) ); while (have_posts()) : the_post(); the_title(); the_content(); endwhile;
3) In the Third method, we have to create a new object of wp_query class and pass the necessary parameter.
$args = array( 'post_type' ='product', 'posts_per_page' '10', 'cat'=10 ) $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); the_title(); the_title(); endwhile;
This loop is the same as query_posts. the difference is in this way can store all parameters in a variable. Below is code example:-
$args = array( 'numberposts' =10, 'post_type' ='portfolio' ); $loop = get_posts( $args );
If you have any questions please comment and also share these posts to others.