is_single not working

Here is something that I do not understand… I have roots installed on the http://3d-car-shows.com site. I have downloaded the latest roots theme, and activated on the server. Now suddenly (or after the new install) I cannot get is_single is_frontpage or is_singular or any of the others to work…

Eg I added the following line into functions.php under $roots_includes = array( ‘custom/posts.php’
); and kept the other includes intact…

I then created the custom/custom-posts.php page, where I want to check if a page is_single or is_home etc…

<?php
if (is_page()) {
echo "<h2>Front Page</h2>";
}
if (is_single()) {
echo "<h2>Front Page</h2>";
}
?>

Something like the code above… But for the live of me, I am not getting it to work…

Am I missing something? Any idea why this might not be working?

It works for me at the bottom of this single (post): https://3d-car-shows.com/morocco-rally-sees-dakar-rally-champs-on-top/

Consider brushing up on WordPress conditionals.

Hi CFX - It is because I have included it into the content-single.php file to make sure the is_single is working, if it sits inside the content-single.php page, that is why you are getting the output at the bottom…

if (is_single()) {
echo "<h2>Single</h2>";  

However the other one should appear as a H2 on the top of the page…

Where are you calling custom/custom-posts.php and how is it being included in your template files?

I use the conditional template tags regularly and I can assure you they behave as expected if you use them correctly.

I have now changed it as follows: in my functions.php file I have this:

$roots_includes = array(
  'lib/utils.php',           // Utility functions
  'lib/init.php',            // Initial theme setup and constants
  'lib/wrapper.php',         // Theme wrapper class
  'lib/sidebar.php',         // Sidebar class
  'lib/config.php',          // Configuration
  'lib/activation.php',      // Theme activation
  'lib/titles.php',          // Page titles
  'lib/nav.php',             // Custom nav modifications
  'lib/gallery.php',         // Custom [gallery] modifications
  'lib/comments.php',        // Custom comments modifications
  'lib/scripts.php',         // Scripts and stylesheets
  'lib/extras.php',          // Custom functions
  'lib/custom_gerald.php'
);

Which is standard/default for roots… except for the last one ‘lib/custom_gerald.php’

The ‘lib/custom_gerald.php’ file is now as follows:

if (is_single()) {
echo "<h2>Single</h2>";
}
$post_type = get_post_type();
echo "<h2>".get_post_type( $post_id )."</h2>";

and if I am not mistaken it should echo the message “SINGLE” ontop of the site, when the condition is met for any single page…

If I leave out the is_single and just do an echo, it display <h2>single</h2>on all the pages…

This has nothing to do with Roots. If you’re using conditional tags within a file that’s outside of the WordPress template hierarchy then you will need to hook into an appropriate action.

1 Like

HAHA!!! Thanks a million CFX

You actually just helped me to solve it, much appreciated…

<?php
function carshows_scripts1() {
if (is_single()) {
echo "<h2>Single</h2>";
}}
add_action('wp_enqueue_scripts', 'carshows_scripts1', 100);
?>

Now it works!