-
omp-generatepress
Hi,
I have 2 different formats – depending on the category – for my featured images. One is upright, the other horizontal.
This is fine for all Category Pages. But I have a problem on TAG pages, as the two different image formats are mixed here. This does not look so good.
Do you have any ideas on how to solve this?
My idea was to upload a second alternative featured image in the posts for tag pages.
For example, I found the plugin that might make this possible: https://wordpress.org/plugins/multiple-featured-images/
However, the plugin has not been updated for a long time and I would prefer to implement it without a plugin. Is that possible?
-
omp-generatepress
Update: I have now added a new custom field via ACF plugin. This way I can add a 2nd image to posts.
How can I now manage on tag pages:
– if the AFC field “features_images_tag_pages” is filled, use this image as featured image for the post, otherwise use the default featured image.
-
Hi there,
Try this code, and set the ACF field return format to ID.
function add_fallback_featured_image($value, $post_id, $meta_key, $single) { if ($meta_key === '_thumbnail_id' && !$value) { // Retrieve the fallback image ID from an ACF field $fallback_image_id = get_field('features_images_tag_pages', $post_id); // If a fallback image ID exists, return it if ($fallback_image_id) { return $fallback_image_id; } } // Return the original value if no fallback is needed or set return $value; } // Hook into get_post_metadata for the _thumbnail_id meta key add_filter('get_post_metadata', 'add_fallback_featured_image', 10, 4);
Adding PHP: https://docs.generatepress.com/article/adding-php/
-
omp-generatepress
Hi Ying,
pretty cool, this works 🙂
But one thing: this code should only be active on TAG pages. Can you please adjust this code?
-
Change this line:
if ($meta_key === '_thumbnail_id' && !$value) {
to
if ($meta_key === '_thumbnail_id' && !$value && is_tag()) {
-
omp-generatepress
Perfect, thank you 🙂
-
No Problem 🙂
-
omp-generatepress
Hi Ying,
can this code be extended as follows:
Code should only be active on TAG Pages…
if ($meta_key === '_thumbnail_id' && !$value && is_tag()) {
BUT NOT on the following tag pages “/photography” and “/photographer”
-
yes, change it to:
if ($meta_key === '_thumbnail_id' && !$value && is_tag() && !is_tag('photography') && !is_tag('photographer')) {
-
omp-generatepress
perfect 🙂
-
omp-generatepress
Hi Ying,
I need another rule 😉
Can you extend this part of the code:
&& is_tag
to
&& is_tag OR is_search
I tried it but I didn´t have the right syntax.
-
Try this:
&& is_tag() || is_search()
-
omp-generatepress
I already tried this… but with this I get a blank search result site without any result
-
Try this:
function add_fallback_featured_image($value, $post_id, $meta_key, $single) { if ($meta_key === '_thumbnail_id' && !$value) { // Check if the conditions match for tag or search pages if ((is_search() || (is_tag() && !is_tag('photography') && !is_tag('photographer')))) { // Retrieve the fallback image ID from an ACF field $fallback_image_id = get_field('features_images_tag_pages', $post_id); // If a fallback image ID exists, return it if ($fallback_image_id) { return $fallback_image_id; } } } // Return the original value if no fallback is needed or set return $value; } // Hook into get_post_metadata for the _thumbnail_id meta key add_filter('get_post_metadata', 'add_fallback_featured_image', 10, 4);
-
omp-generatepress
perfect, thank you
-
No Problem 🙂
- You must be logged in to reply to this topic.