is_singular won’t call my functions?

The question:

Hi i’m using this code to call for a different stylesheet for a single post type but the problem is, It won’t call the stylesheet.

It’s in header.php, i also tried placing it in single.php

<?php
if ( ! is_home() ) {

if ( is_single() == 'pretty-little-liars' )  {
    echo '<link rel="stylesheet" href="http://www.tv-cafe.com/wp-content/themes/tvcafe/posttypecss/style-pll.css" rel="nofollow noreferrer noopener" type="text/css" media="screen" />';
}
}
?>

can anyone tell me what the problem is?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

is_single() returns TRUE or FALSE, not a string. Additionally, you can test for a specific post with is_single() function by putting the post slug into the function call:

if ( is_single( 'your-post-slug' ) )
{
    # do something
}

If you want to test for the proper post type use:

if ( is_singular() and 'your-post-type' === get_post_type() )

or just:

if ( is_singular( 'your-post-type' ) )

Edit

And for your specific question, you should wrap that script into a callback, hooked into wp_enqueue_scripts. In functions.php:

function wpse78368_enqueue_custom_stylesheet() {
    if ( is_singular( 'pretty-little-liars' ) ) {
        wp_enqueue_style( 
            'style-pll',
            get_template_directory_uri() . '/posttypecss/style-pll.css' 
        );
    }
}
add_action( 'wp_enqueue_scripts', 'wpse78368_enqueue_custom_stylesheet' );

Note: Use wp_enqueue_scripts because wp_enqueue_styles doesn’t exist as a do_action().


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment