1

I'm getting the following error in my debug.log:

PHP Notice:  Trying to get property of non-object in /my-wesbite/wp-content/themes/express/header.php on line 36

This is line 36 of header.php in my theme:

<body id="page-<?php echo $post->post_name;?>" <?php body_class(); ?> data-site-url="<?php echo site_url(); ?>">

wp_head() is called before line 36.

I have looked at the output source of every page of the website and $post->post_name, body_class(), and site_url() all echo properly.

My assumption is the error is related to $post->post_name, because it is talking about an object's property. Am I needing to declare global $post first or something else?

Looking at debug.log I can't tell if this error fires on every page or just on certain ones.

As a sidenote, I'm planning on adjusting the id to account for taxonomies as well, but I can get that sorted out once I figure out what is causing this error.

Thank you for any help you can offer.

  • How do you get $post before that call? – fuxia Feb 23 '15 at 21:36
  • @toscho I don't. It has always just echoed the proper information out. Do I need to first declare global $post? – Mark Rummel Feb 23 '15 at 21:38
  • Yes, of course. Variables don’t just appear fully set up out of the blue. :) – fuxia Feb 23 '15 at 21:41
  • @toscho Does it need to be a part of a post loop? Or do I just need to declare global $post? If the variables don't just appear out of the blue, where is it coming from now if wp_head() is the only wordpress-specific code before this line? – Mark Rummel Feb 23 '15 at 21:45
  • How did you go with this Mark? – Tim Malone Jul 6 '16 at 3:48
1

A better method for what you are trying to achieve

This answer is not about the php error you are getting. You need these classes for styling your website right?

Put the following code in your functions.php

// A better body class
function condensed_body_class($classes) {
    global $post;

    // add a class for the name of the page - later might want to remove the auto generated pageid class which isn't very useful
    if( is_page()) {
        $pn = $post->post_name;
        $classes[] = "page_".$pn;
    }

    if( !is_page() ) {          
        $pn = $post->post_name;
        $classes[] = "post_".$pn;
    }

    if ( $post && $post->post_parent ) {
        // add a class for the parent page name
        $post_parent = get_post($post->post_parent);
        $parentSlug = $post_parent->post_name;

        if ( is_page() && $post->post_parent ) {
                $classes[] = "parent_".$parentSlug;
        }

    }

    // add class for the name of the custom template used (if any)
    $temp = get_page_template();
    if ( $temp != null ) {
        $path = pathinfo($temp);
        $tmp = $path['filename'] . "." . $path['extension'];
        $tn= str_replace(".php", "", $tmp);
        $classes[] = "template_".$tn;
    }
    return $classes;
}


/* a better body class */
add_filter( 'body_class', 'condensed_body_class' );

And where you need the body class, put this:

<body <?php body_class(); ?>>

You will see that so many useful class information will be printed.

Good Luck!

| improve this answer | |
0

Keep in mind this will only work if you're on a single page or post, because when you're displaying the output of a loop, you don't yet have a post until you've started the loop (or, you might have the first post in the loop, but either way that's not what you'd want to set a body ID for).

I'd recommend wrapping this ID with two conditionals:

<?php if(is_single() || is_page()){ ?> id="page-<?php echo $post->post_name;?>" <?php } ?>

You'll probably find that this then just works. But if you want to be absolutely sure, you could try setting up your postdata first, either with global $post or by running the_post(). Without testing it I'm not entirely sure, but I would suspect the conditionals could solve your problem as the error is probably coming from pages where it doesn't all happen automatically for you (i.e. archive pages, search results, etc.)

Let me know if that fixes it!

| improve this answer | |

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.