0

I have a sidebar on my site and there I want to show the current post's meta information, such as the last modified date, the author and more.

Thus I wrote the following function:

function showPostMetaInfo()
{   
    global $post;

    setup_postdata($post);

    $mypost = get_post();

            $string.= "<table id='meta-info'>"
                    . "<thead>"
                    . "<tr>"
                    . "<th> Meta Type </th>"
                    . "<th> Value"
                    . "</tr>"
                    . "</thead>"
                    . "<tbody>"
                    . "<tr>"
                    . "<td> Title </td>"
                    . "<td>".get_the_title()."</td>"
                    . "</tr>"
                    . "<tr>"
                    . "<td> Author </td>"
                    . "<td>".get_the_author()."</td>"
                    . "</tr>"
                    . "<tr>"
                    . "<td> Published </td>"
                    . "<td>".get_the_date()."</td>"
                    . "</tr>"
                    . "<tr>"
                    . "<td> Last Modified </td>"
                    . "<td>".get_the_modified_date()."</td>"
                    . "</tr>"
                    . "<tr>"
                    . "<td> Categories </td>"
                    . "<td>".listCategories()."</td>"
                    . "</tr>"
                    . "<tr>"
                    . "<td> Tags </td>"
                    . "<td>".listTags()."</td>"
                    . "</tr>"
                    . "</tbody>"
                    . "</table>";

    return $string;
}

However, var_dump($mypost) returns NULL.

From what I understand this is because I call get_post() before the information is ready.

My problem now is that I don't know WHERE and HOW and AFTER WHICH HOOK to call add_action(...) properly. The function responsible for the sidebar is createPageInfo( $type ) where $type can be single, page or null. How to pass that argument $type to add_action properly?

I might have a conceptual misunderstanding but here's roughly how i built my current site:

index.php:

...
get_header();
createMainPanel();
get_sidebar();
get_footer();
...

sidebar.php:

 ...
 if( is_single() || is_page() )
    {
        if( is_single() ){createPageInfo( "single" );}
        elseif ( is_page() ){createPageInfo( "page" );}
    }

    else
    {
        createPageInfo ( null );
    }
 ...

EDIT:

Following Rarst's advice i changed showPostMetaInfo() to the following:

function showPostMetaInfo()
{   
    $mypost = get_post();

    $id = $mypost->ID;
    $title = $mypost->post_title;
    $title2 = get_the_title();
    $date = $mypost->post_date;

    echo $id;
    echo $title;
    echo $title2;
    echo $date;

    $string .= "<p>".$title."</p>";
    return $string;
}

It turns out that $id, $title, $title2, and $date are echoed correctly, but the return of the function is "<p></p>" only...

Now I'm really puzzled...

1 Answer 1

0

It's not clear why you mess with the global, under normal circumstances you don't need to.

Out of the box, when viewing a post page, get_post() will return current post object. Regardless of the loop even, since it is set up as part of main query before template is even reached.

The difference between post and page for this purpose is minimal as well.

If get_post() call alone return empty/unexpected results, then the likely issue is that something messes up global context before this point is reached.

5
  • Okay, I feel a little bit dumb now, after removing the global stuff get_post() works just fine, as var_dump($mypost) returns the whole post including the meta info . However $mypost->ID for instance still returns NULL. This is also the case for functions such as get_the_date(). What am I doing wrong here?
    – Alex
    Commented Nov 5, 2016 at 14:20
  • Post object without ID?.. I have no remote idea how that can even happen at all.
    – Rarst
    Commented Nov 5, 2016 at 14:43
  • I have not the slightest idea what's happening... must be something very obvious... see my edit. The id is returned correctly from get_post(). But inside the a concatenated string is not saved lol...
    – Alex
    Commented Nov 5, 2016 at 14:58
  • I noticed that the correct values from echo were only returned because I added add_action('redirect-template',' showPostMetaInfo'). The problem still remains. The correct values are displayed in the wrong part of the page.
    – Alex
    Commented Nov 5, 2016 at 15:32
  • This is a Logic behaviour, the returns value isn't echo by your template file, you'll need to add echo createPageInfo to echoing the value or string returns by the function. An echo (in a function) will be print during execution even the return will be or not from where the function is called.
    – Benoti
    Commented Nov 5, 2016 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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