• Resolved calebt2

    (@calebt2)


    I’d love to be able to have posts showing on the homepage, but be able to customize the category that is showing, for example I’d like to have a few posts categorized as “home” that always show when you visit the home page.

    Right now you can only have latest posts from all categories. Just started using the theme today and am liking it already.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Theme Author Ben Sibley

    (@bensibley)

    Thanks for using Tracks!

    That’s an interesting idea. There is an option to make posts “sticky” that may work for you. You could make a few posts sticky and they would always show at the top of the Blog.

    If you visit the post editor and click on the “Edit” link next to “Visibility” on the right side of the screen, you’ll see a check box that allows you to make a post sticky.

    Let me know if this seems like a viable solution to you.

    Hi,

    I had the same requirement and found the solution somewhere in the forums.
    What you need is a custom plugin to do that. First take a look at your categories and grap the category ID you want to display. In the example the Cat ID is “2”.
    $query->set('cat', 2);
    Change the code below to match your ID.

    <?php
    /*
    Plugin Name: Limit homepage to display a single Category
    */
    function my_before_query( $query ) {
    
       if( $query->is_main_query() && is_home() ){
           $query->set('cat', 2); 
    
       }
    }
    add_action( 'pre_get_posts', 'my_before_query', 1 );
    ?>

    I created a php file named limitHomepageToCat.php, put in the code and copied it to the plugin-folder.
    Simply activate the plugin and your homepage will only display the posts from one category.
    You might also put that code in the functions.php of your child theme, but I did not test that.

    Theme Author Ben Sibley

    (@bensibley)

    Sascha,

    That’s a nice snippet, thanks for sharing!

    For anyone interested in using it, you can find the category ID by going to a category page in your dashboard. The ID will be in the URL of that page.

    You could use the code above as-is to make a plugin, or include it in the functions.php file of your child theme like this:

    <?php
    function my_change_home_page_category( $query ) {
    
       if( $query->is_main_query() && is_home() ){
           $query->set('cat', 2); 
    
       }
    }
    add_action( 'pre_get_posts', 'my_change_home_page_category', 1 );

    If you’re new to using PHP, please try this locally first, or have your FTP credentials at hand. A misplaced character will temporarily crash your site stopping you from accessing to the code editor in your dashboard.

    You’re welcome!
    I tried your solution by including your snippet into the functions.php and it is working. I think it’s the better solution anyways because as a plugin one should be able to customize the Cat ID.
    For the “copy & paste”-guys of you:
    Don’t copy the
    <?php
    into the functions.php unless the snippet is the only piece of code in that file.
    Then you also need to add the closing tag
    ?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Customize What Categories Show On Homepage’ is closed to new replies.