• Resolved jlstern

    (@jlstern)


    I’ve been trying to add a datestamp to each recent post listed on the Main Index page, working in the Yen theme (my site is here: https://www.thejetstream.org).

    I’m completely new to coding, but have been trying to insert different tags into the index.php file with varying degrees of success. As you can see, I’ve been able to get a date to show above each post title, but every post shows the date for the one most recent post (the second and third posts are from March 3 and January 11). I did that by entering this code:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    which I gather is the code for displaying posts with no featured image (I’m only planning to display posts with no featured image for now). I could use some advice on both what code to use and where to put it (I have played around with a few of the suggestions from the codex but none of them seem to work.) I hope this makes sense, all the terminology is new to me.

    Many thanks in advance for your help!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Use the_time() or echo get_the_time(), instead of echo get_the_date().

    Thread Starter jlstern

    (@jlstern)

    Same problem, which makes me think it’s the location of the tag that’s messed up. (Reverted to the code I described in the OP for now.)

    Thanks for the quick reply!

    Are you using the_date() and/or the_time() within the Loop?

    Thread Starter jlstern

    (@jlstern)

    I’m honestly not sure. I think I understand the concept of the Loop, but I can’t tell what/where it is in my out-of-the-box Yen code since it seems to be using slightly different terminology than the examples in the Codex. (And I don’t know enough to be able to identify “synonyms”).

    The full index.php file is here:
    https://pastebin.com/JwiWna7Y

    with my single edit on Line 149.

    If I had to guess, I’d say that the problem is not you, but rather the Theme.

    The Theme is using get_posts() to run multiple queries (i.e. multiple Loops). The problem with get_posts() is that it doesn’t prepare the Loop parameters in the same way that the normal Loop does.

    You need to add a call to setup_postdata($post); inside the get_posts() foreach loop.

    Try changing Line 111 from this:

    $hyperlink_url = get_permalink($recent_post->ID);

    to this:

    setup_postdata($post);
    $hyperlink_url = get_permalink($recent_post->ID);

    I won’t guarantee that it will work, but it should.

    Thread Starter jlstern

    (@jlstern)

    Still nothing. ?? This might be totally off base, but I’m wondering if something else should go inside of setup_postdata() besides $post, since I don’t see $post “defined” (?) anywhere else in the file.

    $post is defined by get_posts(). But, to be sure, you can always try putting:

    global $post;
    setup_postdata( $post );
    $hyperlink_url = get_permalink($recent_post->ID);

    Thread Starter jlstern

    (@jlstern)

    I did it!

    I noticed in the codex that I could set a second parameter for get_the_time(). I tried
    <?php echo get_the_time('j F Y', $recent_post->ID); ?> and it actually worked!!

    Thank you so much for your help, Chip.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Yen: Adding datestamp to recent posts on home page’ is closed to new replies.