Home > Net >  Wordpress: Query to show all Custom Post Types and set a Post Type icon for each post instead of Fea
Wordpress: Query to show all Custom Post Types and set a Post Type icon for each post instead of Fea

Time:04-24

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.

CodePudding user response:

You can do this using WP_Query

$args = array(
             'post_type' => 'my-cpt'
);

$loop = new WP_Query($args);

while($loop->have_posts()) : $loop->the_post();

set_post_thumbnail(get_the_ID(),int $thumbnail_id );

endwhile;

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

CodePudding user response:

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);

Log in to add feedback

  • Related