Forums

Really basic "loop" PHP question... (5 posts)

  1. nickaster
    Member
    Posted 4 months ago #

    Hi there.. I'm just starting to learn the php that runs wordpress and have a very basic php question.

    Say I have a typical front page. But I want the first 5 posts to show *only* headlines. Then the rest of the posts show normally (headline+post+etc...)

    I can make a standard loop with this code:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

    1) Where in that code can I specify how many posts to choose?
    2) Where in the second code would I say... show posts not including the first 5?
    3) What would I do about pagination? ie - I only want those 5 headlines to be on page one?

    THANKS!

  2. I haven't goofed around with it in a while but I'm pretty sure it's all in the query string.

    1. Before your loop, try adding query_posts( 'posts_per_page=5' ); above it.

    2. and 3. See http://codex.wordpress.org/Function_Reference/query_posts and this too http://codex.wordpress.org/Class_Reference/WP_Query

    Play around with that, you'll have lots of fun. :)

  3. alchymyth
    The Sweeper
    Posted 4 months ago #

    possibly use a conditional statement, checking the value of $wp_query->current_post;

    example:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php if( $wp_query->current_post < 5 ) {
    //output only headlines
    } else {
    //output normal
    } ?>
    <?php endwhile; else: ?>
  4. nickaster
    Member
    Posted 4 months ago #

    @alchymyth ... brilliant, thanks! Works like a charm.

    Do you know hoe to add things like "only category 5" or "only posts tagged 'whatever'" ?

    Is that some part of the $wp_query thing?

  5. alchymyth
    The Sweeper
    Posted 4 months ago #

    Do you know hoe to add things like "only category 5" or "only posts tagged 'whatever'" ?

    for the whole loop?
    see
    http://codex.wordpress.org/Function_Reference/query_posts
    http://codex.wordpress.org/Class_Reference/WP_Query
    http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

    or for individual posts in the loop?
    see
    http://codex.wordpress.org/Conditional_Tags

Reply

You must log in to post.

About this Topic

Tags

No tags yet.