skip to Main Content

I’ve set up this query to show all Post from certain Custom Post Types but I would like to alter the query and change the featured image to a specific icon that relates to the Post Type instead of the set Featured Images

function my_query_by_post_types( $query ) {
    $query->set( 'post_type', [ 'articles', 'events' ] );
    }
add_action( 'elementor/query/all_post_types', 'my_query_by_post_types' );

For example instead of showing the featured images for the events, I’d like to show a calendar Icon for all the posts that appear.

2

Answers


  1. You can do this using WP_Query, In your front-page.php you can put this

    $args = array(
                 'post_type' => 'my-cpt'
    );
    
    $loop = new WP_Query($args);
    
    while($loop->have_posts()) : $loop->the_post();
    
    if( is_front_page()){
    set_post_thumbnail(get_the_ID(),int $thumbnail_id );
    }
    
    // Shows the featured image of your choice with`$thumbnail_id`
    
    the_post_thumbnail()
    
    endwhile;
    

    Get the $thumbnail_id of the image you want to set from "Media Library" and hovering/pressing Edit button .

    Login or Signup to reply.
  2. You can filter the featured image output conditionally

    function wpdocs_addTitleToThumbnail( $html,$post_id ) {
    if ( get_post_type($post_id) === 'events' ) 
    {
      $html = '<img src="your_img_src"/>';
      return $html;
    }
    
    
    return $html;
    }
    add_filter( 'post_thumbnail_html', 'wpdocs_addTitleToThumbnail' ,10,2);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search