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.

Read More

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?

Related posts

Leave a Reply

2 comments

  1. 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();
    
  2. There are a few ways to do this, my first thought is to do:

    $query = new WP_Query(array(
        'posts_per_page' => 1, 
        'paged' => 5, 
        'post_type' => '[your_content_type]',
        ));
    while ($query->have_posts()) : $query->the_post();
    // loop stuff
    endwhile;