1

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

3

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();
3

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;
4
  • What is the difference with my answer? Why do you repost my answer? Commented Apr 9, 2012 at 17:44
  • we were typing at the same time..?
    – jessica
    Commented Apr 9, 2012 at 17:48
  • looks like .... Commented Apr 9, 2012 at 17:50
  • 1
    I'm a sucker for whoever answers first (sorry jess). However, I like that you did more than a copy/paste for your answer so you get a +1
    – Jacksonkr
    Commented Apr 9, 2012 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.