• Resolved Tom Ewer

    (@tomewer)


    Hello,

    I’m trying to create a valid category.php for my blog.

    Here’s a link to a category: http://www.beginnerblogging.net/category/wordpress-basics/.

    Here’s a link to my existing category.php code: http://pastebin.com/GWhTmfZV.

    At the moment the page is displaying correctly, except it is listing all posts, rather than only the posts in the given category.

    What do I need to change to limit the posts listed to the relevant category? I tried adding ‘category’ => ID but that didn’t do the trick.

    Thanks for your help!

    Tom

Viewing 4 replies - 1 through 4 (of 4 total)
  • You can use the argument 'cat' => ID or 'category_name' => slug.

    However, is there a particular reason that you generate an entirely new query for the category archives page? It’s possible to use the same query across all template files.

    Thread Starter Tom Ewer

    (@tomewer)

    ‘category_name’ worked great when putting in the name of a particular category slug, but what query do I use to find the slug relevant to the current page? (Perhaps something like ‘category_name’ => category_slug() or something?)

    To answer your question, I’m not sure to be honest…I didn’t create the PHP for the query. It seemed a little unusual to me. Is there a standard query for all template files that I should be following?

    I’m not PHP savvy, in case you were wondering!

    Thanks for your help 🙂

    It’s a bit of a chicken-egg situation to get the slug of the displayed category before you run the query to generate the displayed category, so that was a mistake on my part.

    But the good news is it isn’t technically necessary to make separate queries across your templates: your blog index, your monthly archives, your tag-based archives, etc. You can call The Loop like this:

    <?php if ( have_posts() ) : ?>
        <?php while ( have_posts() ) : the_post(); ?>
            ...code to display posts...
        <?php endwhile; ?>
    <?php endif; ?>

    And WP is smart enough to know what to display based on what template is being called.

    Thread Starter Tom Ewer

    (@tomewer)

    That’s worked perfectly, thank you! No idea what all the superfluous code was for…

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Custom Theme] Creating a category.php file’ is closed to new replies.