Support » Fixing WordPress » Displaying certain categories on frontpage

  • Resolved xvibg



    I have more than 300 categories on my site and I want to show only 30 of then on the frontpage. I tried to do this with the following code, but it doesn’t working.

    function my_home_category( $query ) {

    if ( $query->is_home() && $query->is_main_query() ) {
    $query->set( 'cat', '11');
    add_action( 'pre_get_posts', 'my_home_category' );
Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello there!

    Just like WordPress documentation page of that code says, that functions is designed to show specific category or categories on your frontpage.

    function ar_home_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '3' );
    add_action( 'pre_get_posts', 'ar_home_category' );

    I have tested this code on my local website, and it works perfectly.

    Please make sure that your category ID is correct and corresponds to that category you need.

    Can you tell me how did you get the ID (11) of the category that appears in your code, and where are you puting it?

    Ah, yes it is working, but I wanted to do something else, sorry for this.

    I want to show only 20 category names in category widget, not posts from certain category.

    Do you mean that you want to display your 20 desired category names, and each one of them with a URL link in a Widget?

    Yes, exactly.

    You can do that easily with a native function that WordPress includes by default, as you can see in the following code.

        <?php wp_list_categories( array(
            'orderby' => 'name',
            'include' => array( 3, 5, 9, 16 ),
            'title_li' => '',
        ) ); ?> 

    This code will show you the category names with a link. Just change the numbers to the IDs of your desired categories that exist in your website.

    You can past that code in the place when you want to show it in any file of your current theme. For example sidebar.php.

    If you want a solution more quick and practice for beginners, you can use the Display Categories Widget plugin.

    Let me know if this helps you.

    Thank you so much!!!!!!!!!!!

    • This reply was modified 1 year, 8 months ago by xvibg.

    You are welcome!

    Glad to help you.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Displaying certain categories on frontpage’ is closed to new replies.