Only Show Certain Categories On Blog Page cont

Hi there,

Further to my previous post, I have tried to use the code as pointed to on this post:

However, this doesn’t seem to work and causes the site to hang. I have created a child theme for this purpose but with no luck. I would like to exclude certain Categories from the Blog post.

Any ideas?

Many thanks.

Hello There,

Thanks for updating in! Are your referring to this code?

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' );

Before this code will work, you must change the category ID 11 to the exact category ID you want to include and display in your blog index. You must know how to get of the category ID. This article might help you:

Hope this helps.

Hi,

I have changed the category to the correct one I need and added this script to the Functions.php. However, once added this does not seem to take. Is the below inclusion correct?

<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;

// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:
         
if ( !function_exists( 'child_theme_configurator_css' ) ):
    function child_theme_configurator_css() {
        wp_enqueue_style( 'chld_thm_cfg_child', trailingslashit( get_stylesheet_directory_uri() ) . 'style.css', array( 'x-stack','x-cranium-migration' ) );
    }
endif;
add_action( 'wp_enqueue_scripts', 'child_theme_configurator_css' );

function my_home_category( $query ) {
 if ( $query->is_home() && $query->is_main_query() ) {
 $query->set( 'cat', '60');
 }
}
add_action( 'pre_get_posts', 'my_home_category' );

// END ENQUEUE PARENT ACTION

HI,

I went ahead and added the code in your child theme’s functions.php file and it seems to work.

There is only one post under cat=60

Hi there,

Oddly this seems to have disappeared from the page - I can’t see the code on the Functions.php of the child theme and the blog page is still full of the other categories? Can you shed some light on this?

Thanks!

Hi There,

I can see there is only 1 post(news category) on the news page: http://cosmodic.co.uk/news/

Please clear the cache and double check.

Thanks.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.