1

I'm using the following code at the top of my index.php page in my theme and it never hits the is_single() code even on single posts.

if ( have_posts() ) {

    echo "****************we have posts";

    if ( is_single() ) {
        echo "******************It's single";
    }

    while ( have_posts() ) {
        the_post();
            echo "<br >******************after the_post(): " . $post->ID;

        if ( is_single() ) {
            echo "******************It's single";
            //$myHTML = get_post_meta($post->ID, 'html', true);

            if (false && $myHTML) {
                echo $myHTML;
                die();
            }
            else {
                //
            }
        }
    }
}
else {
    echo "******************we have no posts";
}
3

Your question and answer comes down to template hierarchy and how it works, and it is really really useful to know the basics here.

I'm not going to go into details as I will need to write a short story, but here are the basics:

  • Wordpress uses a very strict hierarchy for loading templates on a specific page request. This hierarchy is known as the template hierarchy which you will have to work through at your own pace. This hierarchy remains constant and never change, except if the core developers changes it in newer versions (like the introduction of some new extra templates in versions 4.3 and 4.4), or if it is changes via one of the template filters.

  • Wordpress is coded in such a way that the only template you will ever need to display any page on is index.php. This template is the ultimate fallback template for any page request and is the template lowest in any given hierarchy. This will in all probability never change.

  • The template highest in the hierarchy will be loaded first, if found, otherwise, the first available template lower down in the hierarchy will be loaded. As it goes, if no special template is found, Wordpress will always load index.php. Any other custom template can however be used in stead of these through the specific template filters provided

  • Any other template in a specific hierarchy, except index.php (which is a must-have template), is the nice-to-have templates. They are only there for convenience and their usage are optional. These template only serves to create a specific look and feel on the front end of the site for a specific page request through special functions or CSS and/or JS effects.

To put everything in perspective, lets look at your specific case. Lets look at the template hierarchy for single posts

The single post template file is used to render a single post. WordPress uses the following path:

  1. single-{post-type}-{slug}.php – (Since 4.4) First, WordPress looks for a template for the specific post. For example, if post type is product and the post slug is dmc-12, WordPress would look for single-product-dmc-12.php.

  2. single-{post-type}.php – If the post type is product, WordPress would look for single-product.php.

  3. single.php – WordPress then falls back to single.php.

  4. singular.php – Then it falls back to singular.php.

  5. index.php – Finally, as mentioned above, WordPress ultimately falls back to index.php.

As you can see, in the above hierarchy, single.php is a higher ranking template than index.php, and since single.php exist in your theme, Wordpress will always load single.php, and never index.php (or even singular.php) for a single post page (except if this behavior is interfered with through the single_template filter).

1

It looks like it's not hitting that code because it's redirecting to single.php. I didn't have this in my other theme so it was working there.

I have to delete single.php so the logic workflow will work there or move my code to single.php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.