Forums

Defining post ID via post_ID code? (2 posts)

  1. glamrock
    Member
    Posted 4 years ago #

    Hi! I'm modifying some code and am wondering if I'm doing something wrong. Instead of having it use the date (in Y-m-d format), I want it to use the post ID. The code's purpose is basically defining a header image dynamically, based on the post ID. It worked with the date code, but doesn't work with the post_ID code.

    Here's the original code:
    <?php the_time('Y-m-d') ?>

    And here's my modification:
    <?php the_ID(); ?>

    So the final code for the entire section would be:

    <div id="comic">
    <?php
    error_reporting(0);
    $gposts = get_posts('numberposts=1');
    foreach ($posts as $post) :
    ?>
    
    <?php echo '<img src="http://collegeandbeyond.org/headers/' ?>
    <?php the_ID(); ?><?php echo '.jpg" alt="Comic" />' ?>
    
    <?php
    endforeach;
    ?>
    </div>

    Any ideas?

    Thanks for the help!

  2. MichaelH
    Volunteer
    Posted 4 years ago #

    At least one problem:

    $gposts = get_posts('numberposts=1');

    try
    $posts = get_posts('numberposts=1');

    I believe you need a setup_postdata($post); statement also.

    Resource:
    Template_Tags/get_posts

Topic Closed

This topic has been closed to new replies.

About this Topic