• Resolved Webbasica

    (@webbasica)


    Thanks for the plugin!

    I’m trying to use the conditional you guys recommended on the FAQ, but instead of a page, I’m loading the listing on a category page.

    This is what I tried:

    add_action( 'init', 'your_override_wrapper_function', 99 );
    function your_override_wrapper_function() {
        if ( ! is_category( '20' ) ) { // ID is numeric, slug is a string.
            return; // we don't want to run for anything except the page we're interested in.
        }
        a_z_listing_force_enable_styles(); // this is the page we want, so run the function to enqueue the styles.
    }

    Doesn’t work. However, the other function, without the conditional, works fine.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Dani Llewellyn

    (@diddledani)

    Hi,

    Is your page for a term inside the taxonomy with slug of category? If your term is in a different taxonomy, then you might find you need to use is_tax. This lets you specify which taxonomy to check the term of where is_category only checks for a term in the “categories” taxonomy.

    As a temporary check you can verify the function is working by commenting-out the if to see whether the stylesheet is included without page-targeting.

    I’ve seen cases where some optimisation plugins are overzealous in their caching and combining and minifying mechanisms where they don’t expect page-specific stylesheets so only store one combined file for the whole site.

    Thread Starter Webbasica

    (@webbasica)

    Thanks for the reply.
    It is a category, and I also tried removing the conditional and the CSS loads as expected.

    Just one last clarification. I’m trying to load the css at the archive page, the actual category index.

    Plugin Author Dani Llewellyn

    (@diddledani)

    If you are showing the listing on the archive page for all “categories”, e.g. https://example.com/categories/, then I think you want to use is_category(), with no parameters. If you are showing it on a single category’s archive page, e.g. https://example.com/categories/category-name, then you need to supply the category as the parameter to is_category().

    Thread Starter Webbasica

    (@webbasica)

    That is exactly what I’m trying to do. To load the css files on the archive page of category 20, using:

    add_action( 'init', 'your_override_wrapper_function', 99 );
    function your_override_wrapper_function() {
        if ( ! is_category( '20' ) ) { // ID is numeric, slug is a string.
            return; // we don't want to run for anything except the page we're interested in.
        }
        a_z_listing_force_enable_styles(); // this is the page we want, so run the function to enqueue the styles.
    }
    Plugin Author Dani Llewellyn

    (@diddledani)

    Perhaps init is too early. Can you try replacing the add_action call with this?:

    add_action( 'pre_get_posts', 'your_override_wrapper_function', 99 );
    

    If that works then I’ll update the documentation to reflect it.

    Plugin Author Dani Llewellyn

    (@diddledani)

    The documentation for pre_get_posts has a bit of extra conditional logic which might be worth including at https://developer.ww.wp.xz.cn/reference/hooks/pre_get_posts/

    add_action( 'pre_get_posts', 'your_override_wrapper_function', 99 );
    function your_override_wrapper_function( $query ) {
        if ( is_admin() || ! $query->is_main_query() || ! is_category( '20' ) ) { // ID is numeric, slug is a string.
            return; // we don't want to run for anything except the page we're interested in.
        }
        a_z_listing_force_enable_styles(); // this is the page we want, so run the function to enqueue the styles.
    }
    Thread Starter Webbasica

    (@webbasica)

    That last one worked great.
    Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Load styles for specific category only’ is closed to new replies.