WordPress Development
General Post Query in WordPress
Post query is a common and important part in WordPress. If you will do a post query , every post gets same style from this query. The common post query code given below:
<?php if(have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> // Content goes here <?php endwhile; ?> <?php endif; ?>
On the content goes here position you need to style for your post what are you want to show. I am taking a post query and it is describe below:
<?php if(have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div class="single_post"> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <div class="post_info"> Posted In: <?php the_category(', '); ?> | Posted on: <?php the_time('M d, Y') ?> <?php comments_popup_link('No Comment', '1 Comment', '% Comments'); ?> </div> <div class="post_content"> <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('post-image', array('class' => 'post-thumb')); ?></a> <?php the_content(); ?> </div> </div> <?php endwhile; ?> <?php endif; ?>
In this query <?php if(have_posts()) : ?><?php while (have_posts()) : the_post(); ?> come from post query.
<div class="single_post">
This div class taking for style single post. By using .single_post class you can style easily.
<h2><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></h2> Here h2 is heading tag, <?php the_title(); ?> this code use for title, you may understand that every title come as h2 title <a> tag use for linking heading and href=”<?php the_permalink(); ?>” used for get permalink on that post when click on heading.
<div class=”post_info”> this div and class taking for styling post info like which post, when post, category and comment.
Posted by: <?php the_author(); ?> | Posted In: <?php the_category(', '); ?> | Posted on: <?php the_time('M d, Y') ?> <?php comments_popup_link('No Comment', '1 Comment', '% Comments'); ?>
<?php the_author(); ?> this code showing post author which post this, <?php the_category(); ?> this code for showing category, <?php the_time(‘M d, Y’) ?> this code for showing posted time and <?php comments_popup_link(‘No Comment’, ‘1 Comment’, ‘% Comments’); ?> this code for showing comment.
<div class="post_content">
Using this div and class taking for style main content. By using .post_content you can easily style your content.
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('post-image', array('class' => 'post-thumb')); ?></a>
In this code <?php the_post_thumbnail(‘post-image’, array(‘class’ => ‘post-thumb’)); ?> used for post thumbnail and by using post-thumb class you can style thumbnail easily (Note: Thumbnail support need to given in function.php ) <a> used for linking the post thumbnail. And href=”<?php the_permalink(); ?> used for this thumbnail link with post permalink.
<?php endwhile; ?> <?php endif; ?> This code use for end query which given in general post query.
Comments are closed