Grabbing specific content

Right now I’m doing this (pseudo code)

posts = content_type("special_stuff");
$i = 0;
$n = 5;
$while(have_posts) {
    if($i == $n) {
        // print the content
    }
    ++$i;
}

This is what I’m doing to always get the nth item. I’m sure there is a better way, but I’m kind of a WP noob.

Related:

  1. (this question) How do I grab the nth element of a content type? eg. always getting the 1st or 5th most recent element from the db.
  2. List item

How do I do #1 based on content id?

2 Answers
2

First of all learn WP_Query class.

Answering on questions:

(this question) How do I grab the nth element of a content type? eg. always getting the 1st or 5th most recent element from the db.

$query = new WP_Query( 'post_type=special_stuff&posts_per_page=1&paged=5' );

List item

// The Query
$the_query = new WP_Query( $args );

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
    echo '<li>';
    the_title();
    echo '</li>';
endwhile;

// Reset Post Data
wp_reset_postdata();

Leave a Comment