WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display posts from one category only (9 posts)

  1. herringsman
    Member
    Posted 2 years ago #

    I have a website based on the TwentyTen theme.

    http://www.fcyrugby.com

    It uses a static front page and there are posts in five categories on five separate pages, each page showing posts from one category only... except for my "main blog page" which for some reason shows the posts from all categories, despite trying various modifications to the code which are posted in the coda and on this forum.

    Might it be something to do with it not being the front page? Anyhow here is the code that is in my template present:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    I have also tried

    <?php query_posts($query_string . '&cat=1'); ?>

    This is my first site in WordPress, so apologies if any of the terms are incorrect and thankyou for any help you can give.

    Polite Request: I have searched the coda and tried various solutions posted elsewhere in the forum so please do no reply by just posting links to these.

  2. MichaelH
    Member
    Posted 2 years ago #

    Maybe look at a plugin that provides a shortcode so you don't have to mess with your page templates.

    http://wordpress.org/extend/plugins/list-category-posts/

  3. herringsman
    Member
    Posted 2 years ago #

    Thanks - I had a look at this, but it seems to just list the categories and the titles of the posts within rather than displaying posts from just one specified category on the page. Maybe I'm not explaining this too well.
    I'd also prefer to hard code it in than use a plug (if it's actually possible!).

  4. MichaelH
    Member
    Posted 2 years ago #

    If category 1 is what you want then should be able to do:

    <?php query_posts('cat=1'); ?>

    If you don't know the category id, something like this example from http://codex.wordpress.org/Function_Reference/query_posts

    query_posts( array ( 'category_name' => 'The Category Name', 'posts_per_page' => -1 ) );
  5. herringsman
    Member
    Posted 2 years ago #

    I tried your suggestion but I still have posts from all categories on the page.

    http://www.fcyrugby.com/actualites/

    The code as it stands now is:

    <?php query_posts('cat=1'); ?>
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    				<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    					<?php if ( is_front_page() ) { ?>
    						<h2 class="entry-title"><?php the_title(); ?></h2>
    					<?php } else { ?>
    						<h1 class="entry-title"><?php the_title(); ?></h1>
    					<?php } ?>

    Any further suggestions anybody?

  6. MichaelH
    Member
    Posted 2 years ago #

    Review Template Hierarchy and make sure you are using the correct category ID. Also see Stepping Into Templates

    Please note WordPress will naturely provide category archives without special coding.

  7. herringsman
    Member
    Posted 2 years ago #

    Thanks. I'll investigate this.

  8. herringsman
    Member
    Posted 2 years ago #

    Yes the problem is here. If I select another template in the WP admin, nothing changes so the page is using something else from the hierarchy. Would this be index.php? If so do I modify it in my child theme?

    Sorry if my use of terms is wayward!

  9. MichaelH
    Member
    Posted 2 years ago #

    Yes, you would need to create the proper template (as defined in Template Hierarchy) in your child theme folder.

Topic Closed

This topic has been closed to new replies.

About this Topic