1

this code below shows the titles and links for 5 posts in get_posts no problem but only displays the date and excerpt for first post... any help please?

<?php
$args = array(
    'posts_per_page' => 5,
    'post_type' => 'my',  
    'order_by' => 'post_date',
    'tax_query' => array(
                        array(
                    'taxonomy' => 'mycategory',
                    'field' => 'id',
                    'terms' => array(36, 38, 83, 84),
                    'operator' => 'NOT IN'
                    ),
                    ),
);
$last_five_posts = get_posts( $args );

foreach ( $last_five_posts as $post ) : setup_postdata( $post ); ?>
    <div><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>" id="frontpagelatestposttitle"><?php the_title(); ?></a></div>
    <div><?php the_date(); ?></div>
    <div id="frontpagelatestpostexcerpt"> <p>
    <?php the_excerpt(); ?>

    </p> </div>
<?php endforeach;
wp_reset_postdata();
?>
2
  • To do that replace <?php the_date(); ?> in your code with <?php the_title(); ?>.
    – Robert hue
    Commented Dec 23, 2014 at 10:03
  • OK, well it displays the_title for all of the posts... but I already have that. why do the_date and the_excerpt not work?
    – fff333
    Commented Dec 23, 2014 at 10:05

1 Answer 1

0

I think you are having same issue as I was having earlier. In this question.

So here is the fixed code. This should work. I replaced both of these functions with get_the_date and get_the_excerpt. For the detailed explanation why/why not this work, read @kaiser's answer in same question.

<?php
$args = array(
    'posts_per_page' => 5,
    'post_type' => 'my',  
    'order_by' => 'post_date',
    'tax_query' => array(
        array(
            'taxonomy' => 'mycategory',
            'field' => 'id',
            'terms' => array(36, 38, 83, 84),
            'operator' => 'NOT IN'
        ),
    ),
);
$last_five_posts = get_posts( $args );

foreach ( $last_five_posts as $post ) : setup_postdata( $post ); ?>
    <div><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>" id="frontpagelatestposttitle"><?php the_title(); ?></a></div>
    <div><?php echo get_the_date(); ?></div>
    <div id="frontpagelatestpostexcerpt">
        <p><?php echo get_the_excerpt(); ?></p>
    </div>
<?php endforeach;
wp_reset_postdata();
?>
1
  • Glad I could help :)
    – Robert hue
    Commented Dec 23, 2014 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.