Skip Navigation

[Resolved] Automatic featured image

This support ticket is created 3 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 10 replies, has 3 voices.

Last updated by Luo Yang 3 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#2175807

Hello,

Is it possible with post type and taxonomy conditions to have an automatic featured image?

Thank you.

#2175963

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Conditions? I don't understand the question, could you please clarify?

I imagine you might want a template for posts to display a default image if the post has no featured image, is that the kind of thing you mean?

#2176657

Exactly.

A function that allows you to do this?

Thank you

#2176955

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screenshot 2021-09-23 at 07.49.36.png

You can use a conditional block that checks whether the featured image is empty, and inside the block (for when the condition is met) add an image block to output your default image.

Outside of the conditional block you add an image block for the featured image, which won't output anything if there is no featured image.

You can see in the screenshot how it looks adding such a condition.

If you are not using blocks you will be doing the same but with shortcodes, and the condition for the conditional shortcode would look like...

[wpv-conditional if="( empty( '[wpv-post-featured-image size='full' output='url' ]') ) "]
#2176999

Hello,

Please let me know if you need more assistance for it.

And here are some related documents:
Display Content Conditionally in WordPress
https://toolset.com/course-lesson/using-toolset-conditional-block/
shortcode [wpv-conditional]:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-conditional

For your reference.

#2177919

Hello,

I would like some php code in the function.php file

For example if the publication type is tournament and the category is football.

If there is no featured image then we display an automatic featured image url.

Thank you.

#2178249

According to our support policy, we don't provide custom codes support.
https://toolset.com/toolset-support-policy/

In your case, you can try these:
1) Setup a content template "my-ct"
2) In above content template, use conditional block to check the current post's post type value and category term's value, then display different results, see the documents I mentioned above:
Display Content Conditionally in WordPress
https://toolset.com/course-lesson/using-toolset-conditional-block/
shortcode [wpv-conditional]:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-conditional
3) In your custom PHP codes, output above content template with function render_view_template():
https://toolset.com/documentation/programmer-reference/views-api/#render_view_template

#2180333

Hello,

do you think it can work like this ?? thank you

// Inside your functions file add the following code
// 
function tournoi_featured() {
          global $post;
          $already_has_thumb = has_post_thumbnail($post->ID); // If post have a featured image use that.
              if (!$already_has_thumb)  {
              // If post does not have a featured image then get the first post image and set as featured image.
              $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" ); // Number 1 relates to taking post image number 1 and adding it as a featured image.
                          if ($attached_image) {
                                foreach ($attached_image as $attachment_id => $attachment) {
                                set_post_thumbnail($post->ID, $attachment_id);
                                //$attachment_id = attachment_url_to_postid( $image_url );
                                //   echo $attachment_id;
                               
                                }
                           } 
			   else if (is_post_type('tournoi')) && ( has_term( 'football', 'sport-tournoi' ) ) {
                           set_post_thumbnail($post->ID, '1'); 
                           }                      
                           else if (is_post_type('tournoi')) && ( has_term( 'futsal', 'sport-tournoi' ) ) {
                           set_post_thumbnail($post->ID, '2');
                           }
                           else if (is_post_type('tournoi')) && ( has_term( 'basket', 'sport-tournoi' ) ) {
                           set_post_thumbnail($post->ID, '3');
                           }
                           else if (is_post_type('tournoi')) && ( has_term( 'handball', 'sport-tournoi' ) ) {
                           set_post_thumbnail($post->ID, '4');
                           }
			   else if (is_post_type('tournoi')) && ( has_term( 'rugby', 'sport-tournoi' ) ) {
                           set_post_thumbnail($post->ID, '5');
                           }
                        }
      }  //end function
add_action('the_post', 'tournoi_featured');
add_action('save_post', 'tournoi_featured');
add_action('draft_to_publish', 'tournoi_featured');
add_action('new_to_publish', 'tournoi_featured');
add_action('pending_to_publish', 'tournoi_featured');
add_action('future_to_publish', 'tournoi_featured');
#2180509

Your custom codes will update the post feature image setting when edit/publish any post, it won't update existed posts.
You can test those custom codes, check if it works for you.

#2180733

I have a new code with yoast SEO but it doesn't work.

function wpc_custom_opengraph_image() {
$post = get_queried_object();

  if (is_a($post, 'WP_Post')) {
    set_post_thumbnail($post->ID, $attachment_id);
  }
  elseif ( is_post_type('tournoi') && has_term( 'football', 'sport-tournoi' ) ) {
      set_post_thumbnail($post->ID, '24482');
  }
  elseif ( is_post_type('tournoi') && has_term( 'futsal', 'sport-tournoi' ) ) {
      set_post_thumbnail($post->ID, '24483');
  }
  elseif ( is_post_type('tournoi') && has_term( 'basket', 'sport-tournoi' ) ) {
      set_post_thumbnail($post->ID, '24481');
  }
  elseif ( is_post_type('tournoi') && has_term( 'handball', 'sport-tournoi' ) ) {
      set_post_thumbnail($post->ID, '24484');
  }
  elseif ( is_post_type('tournoi') && has_term( 'rugby', 'sport-tournoi' ) ) {
      set_post_thumbnail($post->ID, '24485');
  }

}
add_filter('wpseo_opengraph_image', 'wpc_custom_opengraph_image');
#2180763

I think there are some misunderstandings:
1) According to our support policy, we don't provide custom codes support.
https://toolset.com/toolset-support-policy/

2) The custom codes you mentioned above is using yoast SEO filter hook "wpseo_opengraph_image", which is out the range of Toolset support.

And I have search it in Google, the filter hook "wpseo_opengraph_image" seems to be a filter hook for displaying an og:image tag in frontend, I don't think it is a good idea to use WP function set_post_thumbnail() to setup post featured image here, if you want to display a featured image from specific image, please try with function get_the_post_thumbnail_url(), see WP document:
https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/