It’s a new year, time for a new improved custom WordPress loop!
Here’s a little code snippet that you can use to place a loop almost anywhere inside a theme, plugin or a framework like Thesis.
First function does the hard work and return an two dimensional array with each post, custom post etc.. easily accessible.
And designers will like this, the code is separate from the styling side, the second function calls the first and it is there that you can handle each post and add styles and other HTML.
function dmaCustomLoop( $args ){
global $post;
$output = ""; // empty the string
// The Query
$the_query = new WP_Query( $args );
$the_result = array();
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
$permalink = get_permalink();
$title = get_the_title();
$theexcerpt = get_the_excerpt();
$thecontent = get_the_content();
$brisbane_multi = get_post_meta($post->ID, 'brisbane_multi', true);
array_push($the_result, array("permalink" => $permalink, "title" => $title, "theexcerpt" =>$theexcerpt , "thecontent" => $thecontent) );
endwhile;
wp_reset_postdata();
return $the_result;
}
So place the above in your theme functions.php or inside custom_functions if you’re using Thesis. Then were you would like to call the function use this.
$args = array(
//'posts_per_page' => 2,
//'cat' => 11,
'post_type' => 'positiontype',
'orderby' => 'asc'
);
$result = dmaCustomLoop( $args );
// loop through the results
foreach($result as $value){
print "my perma is " . $value['permalink'];
print "my title is " . $value['title'];
print "my excerpt is " . $value['theexcerpt'];
print "my content is " . $value['thecontent'];
}
unset($value);
For more information on the arguments you can send to WP_Query check out this page on the WordPress Codex.


Recent Comments