In this tutorial we’ll see how to get the Custom Post Type (CPT) in your custom page of WordPress (WP).
See the below example code:
/*Custom post name*/ $post_type = 'custom_post'; /** * By default WP gets the get_option( 'posts_per_page' ) from database which is mentioned in the admin backend * we can use our custom numbers as 12, 24 whatever is required * we can also use -1 to show unlimited posts */ $posts_per_page = get_option( 'posts_per_page' ); /** * 'publish' - a published post or page * 'pending' - post is pending review * 'draft' - a post in draft status * 'auto-draft' - a newly created post, with no content * 'future' - a post to publish in the future * 'private' - not visible to users who are not logged in * 'inherit' - a revision. see get_children. * 'trash' - post is in trashbin. added with Version 2.9. */ $post_status = 'publish'; $args = [ 'post_type' => $post_type, 'posts_per_page' => $posts_per_page, 'post_status' => $post_status ]; $query = new WP_Query( $args ); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); /*Get post title by using the below varible*/ $post_title = get_the_title(); /*Get post link by using the below varible*/ $post_link = get_the_permalink(); /*Get post thumbnail src by using the below varible*/ $post_thumbnail = get_the_post_thumbnail( null, 'post-thumbnail', '' ); /*Get post thumbnail url which you can put this in your own img tag*/ $post_thumbnail_url = get_the_post_thumbnail_url( null, 'post-thumbnail' ); /*Get post thumbnail caption (if available) by using the below varible*/ $post_thumbnail_caption = get_the_post_thumbnail_caption( null ); } } /*This is optional to use, unless you want to do another query in the same page*/ wp_reset_query();
Karthikesavan
Write a Reply or Comment
You must be logged in to post a comment.