Skip to content

Instantly share code, notes, and snippets.

@junaidtk
Created October 23, 2018 16:46
Show Gist options
  • Save junaidtk/f6b12c98c6f902320cd2b286f4042b5f to your computer and use it in GitHub Desktop.
Save junaidtk/f6b12c98c6f902320cd2b286f4042b5f to your computer and use it in GitHub Desktop.
Wordpress Post Query for displaying post details in front end
To query a post type in the wordpress template or in wordpress fornt end. Please use the following code snippets.
$args = array(
'post_type' => 'post',
'post_per_page' => 5,
'order' => 'ASC',
);
$the_query = new WP_Query($args);
if($the_query->have_posts()){
while($the_query->have_posts()){
$the_query1->the_posts();
//echo the title of the posts
the_title();
//echo the content
the_content();
//echo the excerpt content
the_excerpt();
//echo the post link\
the_permalink();
//check for thumbnail
if(has_post_thumbnail()){
//echo the featured image
the_post_thumbnail();
}
//Displpay the some content of the post
echo wpautop( substr( get_the_content(), 0, 100 ) );
//get the post thumbnailo url
get_the_post_thumbnail_url();
//For resizing featured image using BFI thumb
$size_parameter = array('width' => 512, 'height' => 512);
$thumb_images = get_the_post_thumbnail_url();
$url_img = bfi_thumb($thumb_images, $size_parameter);
echo '<img href="'. $url_img .'" alt=""/>';
//Get the date of the posts
echo get_the_date('F d,Y');
//show the comment details
comments_popup_link('0', '1', '%');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment