• Resolved alengton

    (@alengton)


    Hi guys, I have the single.php file shown as homepage and I show one post per page. Now, I need to get the post data outside the loop, and place them in the footer. I already do it with a small function and this code

    <?php
    $data = get_post_data(1);
    echo $data[0]->post_date; //Print post date
    ?>

    but I need to do the same thing, without knowing the ID because I need to do it in every single post page. To put it simple: I just need to extract the date and the categories of the post displayed in the current page, and put them in the footer. Do you know how I can do it? Really thanks in advance for your help ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • If I am reading it right try, the single.php is called with a $post so try!

    <?php
    Global $post;
    echo $post->post_date;
    ?>

    HTH

    David

    Thread Starter alengton

    (@alengton)

    Hi, it seems to work. Do you know how I can get post categories? I tried

    <?php
    Global $post;
    echo $post->wp_get_post_categories;
    ?>

    but it doesn’t seems to work. Also, do you know how I can set the date to j F, Y? Right now I get the full date and time. Anyway, really thanks for the help!! ??

    Read the Codex for wp_get_post_categories()

    <?php
    Global $post;
    $categories = array();
    $categories = wp_get_post_categories($post->ID);
    ?>

    Have a read in the Codex link there is an example to loop through the categories and output them.

    Also the Twenty Eleven and Twenty ten themes are a “good place to look”, I think there is a function in functions.php that will return the post categories.

    HTH

    David

    Thread Starter alengton

    (@alengton)

    Really thanks for your help, I figured out a probably cleaner way to do so by putting this on top of the footer.php file

    <?php
    Global $post;
    ?>

    and than call date and category for the post, just like that

    <?php the_time('j F, Y'); ?>
    <?php the_category(', ') ?>

    ??

    Cool,
    But the footer is generated for all files as well as single.php, so adding a condition is_single() might help.

    David

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘get post data outside the loop but without post ID’ is closed to new replies.