• Murphy

    (@danielpunchupproductionscom)


    The problem:
    I’m pulling 4 posts to display the date, title, and excerpt on my main page. Everything works just fine if I put the code directly in my index.php file. But when I create a function for it in the functions.php file, the title from the most recent post is displayed as the title for all 4 posts.

    Here’s the code that works fine in the index.php file:

    <?php
    $args = array( 'numberposts' => 4, 'order'=> 'DEC', 'category=-1' );
    $postslist = get_posts($args );
    foreach ($postslist as $post) :  setup_postdata($post);
    
    $mydate = get_the_date('F d', '', '',FALSE);
    $expdate = explode(' ', $mydate);
    
    $month = $expdate[0];
    $day = $expdate[1];?>
    <div class="dateBox">
    <div class="ramblingDay"> <?php echo $day;?></div>
    <div class="ramblingMonth"> <?php echo $month;?></div>
    </div>
    <div class="ramblingTitle"><?php the_title();?> </div>
    <div class="ramblingExcerpt"><?php the_excerpt();?></div>
    <?php       
    
    endforeach;
    ?>

    And here’s the code for the functions.php file. Only thing that is changed is a function wraps around the previous code.

    <?php
    function display_ramblings(){
    $args = array( 'numberposts' => 4, 'order'=> 'DEC', 'category=-1' );
    $postslist = get_posts($args );
    foreach ($postslist as $post) :  setup_postdata($post);
    
    $mydate = get_the_date('F d', '', '',FALSE);
    $expdate = explode(' ', $mydate);
    
    $month = $expdate[0];
    $day = $expdate[1];?>
    <div class="dateBox">
    <div class="ramblingDay"> <?php echo $day;?></div>
    <div class="ramblingMonth"> <?php echo $month;?></div>
    </div>
    <div class="ramblingTitle"><?php the_title();?> </div>
    <div class="ramblingExcerpt"><?php the_excerpt();?></div>
    <?php       
    
    endforeach;
    }
    ?>

    Thanks everyone in advance

  • The topic ‘Title repeats on every post’ is closed to new replies.