WordPress.org

Ready to get started?Download WordPress

Forums

P2 How to make only one category on home page? (8 posts)

  1. rolandogomez
    Member
    Posted 3 years ago #

    I'm no programmer, so be easy on me ;) How do I make it on the current P2 theme where it only shows one category on the front page and is this in the index.php file? Thanks, rg

  2. keesiemeijer
    moderator
    Posted 3 years ago #

    Use a query post to show only one category on the home page (index.php)

  3. rolandogomez
    Member
    Posted 3 years ago #

    I appreciate it, though I'm not a coder, I spent hours figuring it out and came up with this:

    <?php if ( is_home() or is_front_page() ) :
    global $query_string;
    query_posts( $query_string . "&cat=4'" );
     ?>

    Not sure if it's right. For the future though, please take "non-coders" into consideration, as when you say "use a query post" that's like me telling you to set your camera white-balance at 3700K and unless you're a photographer and author like I am, you probably may not know how to do that on your camera. Again, I really appreciate your quickness and help. For the record, I have it in place here: http://www.quotethequotes.com still working on it ;) Thanks!

  4. keesiemeijer
    moderator
    Posted 3 years ago #

    Of course I take "non-coders" in consideration, but now you have learned what a query post does and in the future when you have to alter the loop you know where to look for it. You don't need the single quote in "&cat=4'". the correct way would be "&cat=4". For the rest you did it the way it should be done.

  5. rolandogomez
    Member
    Posted 3 years ago #

    Thanks, yes, I saw that single quote after I posted, thanks!

  6. Nobble
    Member
    Posted 3 years ago #

    For other themes this is pretty straight forward, the above solution would work, but because P2 injects new posts via ajax (it continually checks for updates a user might have posted while you're on the page), you'd still get live posts that happen when another users posts outside of the desired category appearing on the frontpage regardless of the above tweak. You'd need another few tweaks to fully get the effect. I'm not sure you want to edit multiple files but I could share how, if you like.

  7. rolandogomez
    Member
    Posted 3 years ago #

    Yes please do share, as I noticed that the next day, though, when I refreshed, it resets. I'm the only poster on it for now, but will be adding one or two more people, however, I run an automated script for my Amazon affiliate, that's what I noticed the next day, the posts it does. So please explain, which files and what to do, step by step I'd prefer ;) as I'm no coder, but I can do changes as I do all my websites and am trained by the school of hard knocks ;) Thanks!

  8. Nobble
    Member
    Posted 3 years ago #

    This should work, back up before trying though.

    Add this to functions.php (located in p2's theme directory).

    /* the following tweak filters the front page to only contain a single category of choice */
    add_action('pre_get_posts', 'front_page_cat_filter' );
    
    function front_page_cat_filter()
    {
      global $wp_query;
      $cat = get_cat_id('post');
      if( is_home() || is_front_page () ) {
         $wp_query->query_vars['cat'] = $cat;
      }
    }
    /* end of tweak */

    then in ajax.php, on line 245 in the most current version of p2 inside the 'get_latest_posts()' function, find the line:
    query_posts( 'showposts=' . $num_posts . '&post_status=publish' );
    and replace this with:

    /* tweaking the updated posts to only include a certain category */
    if (is_home () || is_front_page () ) {
    query_posts( 'showposts='. $num_posts . '&post_status=publish&category_name=post');
    }
    else
    {
    query_posts( 'showposts=' . $num_posts . '&post_status=publish' );
    }
    /* end of tweak */

    Depending on the category you want to show, you have to replace the word post with your desired category (so...category_name=<type your category here>) and get_cat_id('<type your category here>')

    I haven't tested this extensively but it should establish what you want to do.

    * made a few small corrections since posting this

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags