WordPress Loop for customizing Theme,plugins .

As we all know wordpress is the light weight web CMS.It is easy to customize to meet your customers need.
The wordpress loop(wp loop) play the role to fetch the required posts from db and show it.If you previously worked with the WP.you may be familiar with it.If not,you can figure it out just going to wordpress themes folder(wp-content/themes) and open the index.php file.The codes are :

<?php if (have_posts('')) :
while (have_posts()) : the_post();  ?>

Here have_posts(),the_posts() are both the functions that access the property method of Wp_query() class.Wp_query() locates at “wp-incldes/query.php”

In some of the themes “query_posts()” is used but for customization tasks,its better to create an instantiate the own “Wp_Query()” for custom loop.
Lets start to customize a loop.Just remove the previous loop, and create a new instantiate of “Wp_Query()”.And declare the category of the posts you want to display through the loop.

<?php
$my_query = new WP_Query('p=85,111');  //retrieving only post no 85 and 111 
have_posts()) :
	while ($my_query->have_posts()) :
		$my_query->the_post();
        ?>

You can customize the loop using different kinds of parameters in based of posts,category etc.
For having more query specific result you can pass specific arugements as form of array.Code example will be like below:

<?php 
$args = array('post_type'=>'post','post__in'=>array('1','4','6'));	
$my_query = new WP_Query($args); 		
if ($my_query->have_posts()) : 		
while ($my_query->have_posts()) :		
$my_query->the_post(); 	?>

You can use these kinds of customized loop for having different posts and pages.
For details about Wp_Query,Click Here

For customizing any plugin related to post display(Ex. gallery plugins)you can use custom loop.Also in differenet conditional posts can be displayed using custom loop.

For having further details about loop,Click Here

 

Advertisements

2 comments

  1. মাসপি cste5th · February 5, 2011

    আচ্ছা ভাইয়া আপনি যে php কোড গুলো লিখলেন তার পাশে মাউসের কার্সর রাখলে “view source” “print” “about” “copy to click board” এর বাটন গুলো আসে, তা কিভাবে আসে?

    • mushfiqsimple · February 5, 2011

      #maspi actually this is the feature of “SyntaxHighlighter”,I just declare a special tag(which enables syntax highlighter) for writing source code in the blog post.And when we move the mouse to the right side JavaScript display those features “view source” etc
      For details about Syntax Highlighter you can follow this link

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s