0

i try to call the content of the recenet posts, not all the content, only the content before 'read more' tag. but it repeats the content of the lastest post. i want to creat one slider with 5 recent posts with post image and short post conent. Can someone please help me !!!! here is my code:

<ul class="slides">
<?php
    $args = array( 'numberposts' => '5' );
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ){
       echo '<li>';
       $content = get_the_content('Read more');
       print $content;
       echo '</li> ';
                }
       wp_reset_query();
    ?>

enter image description here

  • get_the_content uses the global $post, not $recent – inarilo Aug 17 '17 at 19:33
0

for latest 5 post you can use this code:

$args = array('post_type'=>'post', 'posts_per_page'=> 5);
$query = new WP_Query($args);
if($query->have_posts()):
    while($query->have_posts()):$query->the_post();
       echo '<li>';
       $content = get_the_excerpt();

//for featured image

if(has_post_thumbnail()):
the_post_thumbnail();
endif;
           print $content;
           echo '</li> ';
        endwhile;
        wp_reset_postdata();
    endif;

In case you want to change word count in excerpt use this action hook function. /** * Filter the except length to 20 words. * * @param int $length Excerpt length. * @return int (Maybe) modified excerpt length. */

   function wpdocs_custom_excerpt_length( $length ) {
        return 20;
    }
    add_filter( 'excerpt_length', 'wpdocs_custom_excerpt_length', 999 );

In case you need to change read more text.

/** * Filter the "read more" excerpt string link to the post. * * @param string $more "Read more" excerpt string. * @return string (Maybe) modified "read more" excerpt string. */

function wpdocs_excerpt_more( $more ) {
    return sprintf( '<a class="read-more" href="%1$s">%2$s</a>',
        get_permalink( get_the_ID() ),
        __( 'Read More', 'textdomain' )
    );
}
add_filter( 'excerpt_more', 'wpdocs_excerpt_more' );

hope this will help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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