Support » Fixing WordPress » The loop question

  • Resolved mattisn

    (@mattisn)


    I am using this loop right now, however I need to modify it to just show the post’s from one catagory. I also found a piece code which lets you show only one catagory, but where do I place the code in the loop?

    Here’s the loop I’m using right now:

    <?php if (have_posts()) : ?>

    <?php while (have_posts()) : the_post(); ?>

    <div class=”post” id=”post-<?php the_ID(); ?>”>
    <h2>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”><?php the_title(); ?></h2>
    <small><?php the_time(‘j. F Y’) ?> <!– by <?php the_author() ?> –></small>

    <div class=”entry”>
    <?php the_content(‘Read the rest of this entry »’); ?>
    </div>

    <p class=”postmetadata”>Posted in <?php the_category(‘, ‘) ?> | <?php edit_post_link(‘Edit’, ”, ‘ | ‘); ?> <?php comments_popup_link(‘No Comments »’, ‘1 Comment »’, ‘% Comments »’); ?>
    </div>

    <?php endwhile; ?>

    <div class=”navigation”>
    <div class=”alignleft”><?php next_posts_link(‘« Previous Entries’) ?></div>
    <div class=”alignright”><?php previous_posts_link(‘Next Entries »’) ?></div>
    </div>

    <?php else : ?>

    <h2 class=”center”>Not Found</h2>
    <p class=”center”>Sorry, but you are looking for something that isn’t here.
    <?php include (TEMPLATEPATH . “/searchform.php”); ?>

    <?php endif; ?>

    And here is the code to show one catagory:

    <?php $my_query = new WP_Query(‘category_name=student&showposts=1’);
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID; ?>
    <!– Do stuff… –>
    <?php endwhile; ?>

    Where do I put this code in the loop?

Viewing 5 replies - 1 through 5 (of 5 total)
  • It depends on where you want it to appear. I don’t know if the code is right, but if you want it “connected” to the individual post, it has to go after the <div class=”entry”>

    the code is correct because it’s working, but now it’s showing all the posts in every category. I want it to show the posts in just one selected category

    thanks for your help

    So, when you click on a category link, you want a new page to load with just the posts in that category? I don’t think you need a special code for that. If you don’t have a separate archive.php, I think that is exactly the type of thing that wordpress defaults to. Otherwise, the appropriate place for that code isn’t the index.php file, it would be the archive.php files.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    mattisn: That code you’re trying to put into the loop is way overcomplicated. It seems like you’re just wanting the query_posts functionality.

    See here: http://codex.wordpress.org/Template_Tags/query_posts

    Essentially, you’re going to add something like this just before the first have_posts() reference:
    <?php if (is_home()) {query_posts("cat=3");} ?>

    Where 3 is the category you want to display. The “is_home” checks and makes sure that this only happens on the home page.

    Found the answer: just paste this code before your loop:

    <?php query_posts(“category_name=yourcatgory&showposts=8”); ?>

    and use the name of the catagory you want instead of “yourcategory”

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘The loop question’ is closed to new replies.