treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] The wordpress loop

  • <?php query_posts("posts_per_page=2"); ?>

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <h1>News</h1>

    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

    <div class="date">posted by <?php the_author();?> on <?php the_date(); ?></div>
    <img class="blogphoto" src="<?php $thumb = get_post_custom_values('post-image'); echo $thumb[0]; ?> " alt="<?php the_title(); ?>" />

    <div id="seemore">
    <?php the_content('See more...'); ?>
    </div><!-- END Seemore -->

    <?php endwhile;?>

    <?php else : ?>

    <p>Searching and there isn't there!</p>
    <?php endif; ?>

    <div id="moreprev">

    <?php next_posts_link('More...'); ?>

    <?php previous_posts_link('Previous...'); ?>

    </div><!-- END morePrev -->



    Well, the question is about how to point the query post to a category, for example i want this query to post all news category and it subcategory? I kinda new into php so..i've hade a headache to try to make this work hehe..please,correct me if i'm wrong! And i want to have a duplicate of this to point to events category only.

    //Splendor
  • The first thing you will need to do is find out the ID's of your categories which you can do by going into your categories admin panel and simply hovering over the edit link or category title, once you have the ID's simply comma separate them like the below example where 1 and 2 would become your category ID's.

    query_posts('cat=1,2&posts_per_page=2');
  • oki, thank you very much for your answer, that helps alot, i did find some dokument about it on wordpress codex, but i wasn't really sure about it so, thanks :) I was thinking about one more thing, do i need to wp_reset each part, that's is the best thing to do i suppose..

    //Felipe
  • As a side-bar, there's an excellent plugin called reveal-ids-for-wp-admin. Does exactly what it says: shows IDs for posts, pages, categories, etc. Can be very, very helpful if you're building a lot of custom loops.
  • No problem, you may want to have a read of the following article as well which explains the correct way to use pagination with custom post queries.

    http://scribu.net/wordpress/wp-pagenavi/right-way-to-use-query_posts.html
  • Thank you SgtLegen, that was very helpful.:) ccc630, nice a didn't know about that plugin, i'll check that out. :P
  • <?php $args = array(<br />							'numberposts'     => 4,
    'offset' => 0,
    'category' => 4,
    'orderby' => 'post_date',
    'order' => 'ASC',
    'post_type' => 'post',
    'post_status' => 'publish' );
    ?>
    <?php $issuu = get_posts($args); ?>
    <?php echo 'Blog'; ?>
    <?php foreach($issuu as $post) : setup_postdata($post); ?>


    <?php the_time('l j F Y'); ?>
    <?php echo $post->post_content; ?>
    <?php echo get_post_meta($post->ID,'Author_name',true); ?>
    <?php echo get_post_meta($post->ID,'Post_Issuu',true); ?>

    <?php endforeach; ?>