Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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();
share|improve this answer

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;
share|improve this answer
    
What is the difference with my answer? Why do you repost my answer? –  Eugene Manuilov Apr 9 '12 at 17:44
    
we were typing at the same time..? –  jessica Apr 9 '12 at 17:48
    
looks like .... –  Eugene Manuilov Apr 9 '12 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 –  Jackson Apr 9 '12 at 19:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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