is_singular won’t call my functions?

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

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

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x