Forums

[resolved] Help with getting a post's year? (3 posts)

  1. ydavis
    Member
    Posted 1 year ago #

    I want to arrange my posts in a specific way, which first involves grouping them by month and year. However, I can't write the function to do for reasons that are probably simple that I am not getting.

    Within a foreach(myposts) loop (included below), I've tried stripping the year from both the_date_xml and the_modified_date, but in both cases, all my function does is display the output of either of those. How can I get that output into a temporary variable to play with? Or How else can I get just "2008" from each post in a loop?

    function display_travels(){
     global $post;
     $myposts = get_posts();
     foreach($myposts as $post) :
        if (the_modified_date(Y) == "2008") :
           echo "IF TRUE";
        endif;
     endforeach;
    }
  2. MichaelH
    moderator
    Posted 1 year ago #

    Try:

    <?php $post_date = mysql2date("Y", $post->post_date_gmt);
    echo 'post year is  '. $post_date;
    ?>
  3. ydavis
    Member
    Posted 1 year ago #

    PERFECT

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags