Home » Php » php – get_template_part inside get_posts()

php – get_template_part inside get_posts()

Posted by: admin February 25, 2020 Leave a comment


Im trying to show a list of posts including their Title & Image on a post. Im using the get_posts() function with an array to filter the posts. I know that $template_loader->get_template_part( 'content-listing-image'); get the post image, but when I use it on the output it shows me the image of the current post, not the filtered ones ($evento)

This is my code:


$args = array(
    'author'         => $post->post_author,
    'order'          => 'ASC',
    'posts_per_page' => -1,
    'meta_query'     => array(
            'key'   => '_listing_type',
            'value' => 'event'

    $eventos = get_posts( $args );

    foreach ( $eventos as $evento ) {
        $template_loader = new Listeo_Core_Template_Loader;

        $output .= '<div><a href="'.get_permalink( $evento->ID ).'" target="_blank"> '.$template_loader->get_template_part( 'content-listing-image').' '.$evento->post_title.'</a></div>';  

    if(!empty($eventos)) : ?> 
        <div id="listing-eventosartista" class="listing-section">
            <h3 class="listing-desc-headline margin-top-60 margin-bottom-30"><?php esc_html_e('Eventos','listeo_core'); ?></h3>
                <div class="single-item">
                    <?php echo $output; ?>
    <?php endif ?>

I want to show the corresponding image to each $evento (post).

Hope yoy can help me!

How to&Answers:

You can use get_the_post_thumbnail(), like this

$output .= 'ID ).'" target="_blank"> '.get_the_post_thumbnail( $evento->ID).' '.$evento->post_title.'';