WordPress uses “the Loop” to create content it gets from the database. It’s used all over the place, be that within the standard theme files such as index.php, single.php, archive.php etc etc etc. Often as coders we use the loop to get information using WP_Query(); take the below example which is taken from a Widget that gets the latest 5 posts, excluding the current post (if there is one); $id = false; // get current post id if a single (post or page) if(is_single()){ global $post; $post->ID; $id = $post->ID; } // setup basic loop arguments $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => '5' ); // add the id as an exclude to the args if there is one if($id){ $args['post__not_in'] = array($id); } // create the loop query $loop = new WP_Query($args); // check if has a good result if ($loop->have_posts()) : // begin the unordered list ?><ul><?php while ($loop->have_posts()) : $loop->the_post() ; // do the things that are shown in the loop ?><li> <a href='<?php the_permalink(); ?>'><?php the_title(); ?></a> </li><?php endwhile; // end the ?></ul><?php
Share This