logo

$10
Show only one category of post on Home & Archive pages

Hi all,

Question regarding controlling what categories show up as visible:

I only want my "news" stories to be visible to my site visitors (this is the post category called "News", ID=1.) I want to hide all other categories of post on my home page and in my archive list of posts.

Can I hardcode my php to permanently look for only for posts categorized as "news" and ignore all other posts?

(This may seem like a strange request, but I am using different post categories to associate mp3s with different podcast feeds and I don't want these to show up on home or in the archives. Those post categories are only to feed the podcasts)

When you reply, could you let me know what code to look for in my home and archives pages and let me what to replace? I'm not much of a developer.

Thanks in advance!!

Kelli

Kelli Anderson | 05/10/10 at 3:28pm | Edit


(4) Possible Answers Submitted...

  • avatar
    Last edited:
    05/10/10
    4:12pm
    Oleg Butuzov says:

    Code can be inserted before while_posts running.

    if you have home.php or archive.php you need to edit it and insert this code before...
    if you havn't this files - use index.php


    <?php
    if ( is_home() || is_archive()) {
    $YOURNEWSCATEGORYID = 1;
    query_posts('cat='.$YOURNEWSCATEGORYID);
    }
    ?>



    this how code shoudl looks at least in idea...

    <?php
    if ( is_home() || is_archive()) {
    $YOURNEWSCATEGORYID = 1;
    query_posts('cat='.$YOURNEWSCATEGORYID);
    }
    ?>

    <?php while (have_posts()) : the_post(); ?>
    <!-- your loop code -->
    <?php endwhile; ?>



    Previous versions of this answer: 05/10/10 at 3:33pm | 05/10/10 at 3:35pm | 05/10/10 at 3:35pm | 05/10/10 at 3:37pm | 05/10/10 at 3:39pm | 05/10/10 at 3:57pm | 05/10/10 at 3:57pm | 05/10/10 at 4:08pm | 05/10/10 at 4:10pm | 05/10/10 at 4:12pm

  • avatar
    Last edited:
    05/10/10
    3:40pm
    Maor Barazany says:

    Hi Kelli,

    Yes, it is possible and it's quite easy to do.

    Just add a query_posts before the loop.
    The loop starts with the code something like -

    if (have_posts()) : while (have_posts()) : the_post(); ?>


    So a line before the loop starts just add -

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


    cat = 1 means that it will show only category number 1.
    If you write instead cat=-1, so all categories except number 1 will be shown.
    You may have more than one category, just add cats separated with commas - cat=1,2,x,y,z

    showposts=4 - this means that only 4 posts will be shown, you may use -1 and it will show all posts, or any other number you like.

    Good luck!

    Maor

    Previous versions of this answer: 05/10/10 at 3:39pm | 05/10/10 at 3:40pm

  • avatar
    Last edited:
    05/10/10
    3:38pm
    Erez S says:

    <?php
    $category=get_the_category();
    echo $category[0]->cat_name;
    ?>
    Replace the following code on your index.php and archive.php files with the code above:
    the_category();
    (Something like that,it changes from theme to theme)

  • avatar
    Last edited:
    05/10/10
    4:45pm
    Will Anderson says:

    The problem with using query_posts is that is overrides any other query variables that might be applied (like date queries for the archive for example). A better solution is to use a filter. This code is based on a similar article I wrote about limiting categories in RSS feeds. article

    The following code will do the same thing, but on on the home and archive pages instead.


    function only_news_cat($query) {
    if ( is_home() || is_archive() ) {
    $query->set('category_name', 'News');
    }
    return $query;
    }
    add_filter('pre_get_posts', 'only_news_cat');


    I would suggest putting that code in your functions.php file. You may need to change the capitalization of the word "News", depending on whether the actual category name is capitalized.

    -Best
    Will Anderson

    • 05/10/10 4:15pm

      Kelli Anderson says:

      Hi Will,

      When I post your code into the end of my wp-includes/functions.php file, I get this error:
      (when I reload the site)


      Fatal error: Call to undefined function add_filter() in /home/merrifieldrecords/merrifieldrecords.com/wp-includes/functions.php on line 3654

      Thanks!

      Kelli

    • 05/10/10 4:22pm

      Will Anderson says:

      Oh, not wp-includes/functions.php, but the functions.php in your theme folder (if that file doesn't exist, you should create it).

    • 05/10/10 4:45pm

      Kelli Anderson says:

      Cool thanks!

      Worked like a charm

This question has expired.





Current status of this question: Completed