Skip Navigation

[Resolved] Display custom fields of the featured image

This support ticket is created 2 years, 8 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/Karachi (GMT+05:00)

This topic contains 16 replies, has 3 voices.

Last updated by Arno 2 years, 8 months ago.

Assisted by: Waqar.

Author
Posts
#2382171

Hi,

I'm sorry if this is obvious but I can't find how to do it:

My media post type has custom fields, e.g. a taxonomy to select the media owner from. This works.
My posts show the featured image of the post. This works too.

I want to include a dynamic text/caption for the featured image, for example showing the owner (taxonomy term) along with some static text. Perhaps a different text based on who's the owner: if term="x" then shows this HTML, otherwise this HTML.

I tried the Fields and Text block, then I tried a View to select the media (the featured one for the current post). But I fail to see how to do this.

#2383603

Nigel
Supporter

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

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

That's not something you can do through the UI, you would need a code solution.

There are 3 steps. For the current post, you need to find the ID of the featured image, then find what taxonomy terms are assigned to that post ID, and then find the custom field value for that term.

I drafted a custom shortcode you can use to output such a field:

add_shortcode('media-term', function () {

    $tax = 'colour'; // Edit slug of taxonomy
    $term_field_key = 'wpcf-hex-color'; // Edit term custom field key ("wpcf-" plus field slug)

    global $post;
    $term_field = "";
    
    // get featured image post id
    $media_id = get_post_meta( $post->ID, '_thumbnail_id', true );

    if ( is_numeric( $media_id ) ) {
        // get taxonomy term assigned to media
        $terms = wp_get_post_terms( $media_id, $tax, array( 'fields' => 'ids' ) );
    }

    if ( is_array( $terms ) && is_numeric( $terms[0] ) ) {
        // get the term custom field
        $term_field = get_term_meta( $terms[0], $term_field_key, true );
    }

    // return the field value
    return $term_field;

});

Note that you need to edit the first lines with the slug of your taxonomy, and the key of the term meta (the slug of the term custom field with a prefix of 'wpcf-').

That will simply output the value of the relevant term field (you can use a shortcode block to insert the shortcode [media-term], or any block that expands shortcodes).

You could also use the shortcode in the expression of a conditional block.

#2383681
fields-text-block-output.jpg

Hi Nigel,

Thank you. If at all possible I prefer a no-code solution. I gave it another try and I'm getting close. I inserted a Fields and Text block in the post template with this:

<p>Copyright: [wpv-post-taxonomy item='[wpv-post-featured-image output="id"]' type='media-owner' format='name'], more information: [types field='media-copyright-url' item='[wpv-post-featured-image output="id"]' output='raw'][/types]</p><p>[wpv-post-featured-image output="id"]</p>

This works perfectly.

When I am doing the except same thing, but not directly in the post template but in a Kadence content element (inserted in the hero section of each post), then the frontend shows as in the attachment. So, it successfully get the ID of the featured image, but not the taxonomy term and custom field. Any suggestions on what might solve that? Do the nested quotes cause a problem here?

Thanks,
Arno

#2383763

Nigel
Supporter

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

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

If it works in a post template, but not in a Kadence content element, it sounds like that may be stretching the context too far.

The use of quotes (single and double) looks okay, and the fact that it works in a post template suggests that the problem isn't using a shortcode-with-attributes to provide the value of another shortcode attribute.

Given that the wpv-post-featured-image shortcode outputs the expected value in this context, what happens if you replace that shortcode where you use it to provide the item attribute values with the hard-coded value of 658. Does that produce the expected result, or is it still broken?

#2383799

This code works fine inside the Kadence element:

<p>Copyright: [wpv-post-taxonomy item='658' type='media-owner' format='name'], more information: [types field='media-copyright-url' item='658' output='raw'][/types]</p><p>[wpv-post-featured-image output="id"]</p>

So we're getting very close. The only thing left is how to replace the fixed ID by the dynamic one.

#2383847

Nigel
Supporter

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

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

If the dynamic version works in the context of a post template, but not the Kadence content element, it looks like you may have to resort to some custom code after all.

I suspect the issue is that the wpv-post-featured-image shortcode, when used to provide the value of the item attribute of other shortcodes, itself needs a shortcode attribute.

Try registering a custom shortcode that simply returns "658" and use that shortcode to provide the item attribute value. Does it work?

If so, you could update the shortcode so that it returned a dynamic rather than static value, and use it in place of the wpv-post-featured-image shortcode.

#2383895

That does not work but I don't have experience with custom shortcodes, so I may have made a mistake.

I have this code:

// Add Shortcode
function ohcc_get_featured_image_id( $atts ) {
 
// Attributes
$atts = shortcode_atts(
array(
'post_id' => '',
'taxonomy' => '',
),
$atts
);
 
//$terms = wp_get_post_terms( $atts['post_id'], $atts['taxonomy']);
$terms = '658';
//return count($terms);
return $terms;
}
 
add_shortcode( 'ohcc_get_featured_image_id', 'ohcc_get_featured_image_id' );

I have registered [ohcc_get_featured_image_id] under "Third-party shortcode arguments" in the Toolset settings.

And I have this in the Fields and Text block:

<p>Copyright: [wpv-post-taxonomy item='[ohcc_get_featured_image_id]' type='media-owner' format='name'], more information: [types field='media-copyright-url' item='[ohcc_get_featured_image_id]' output='raw'][/types]</p><p>[wpv-post-featured-image output="id"]</p>

The result is exactly the same as in the last screenshot.

#2384059

By the way, I just did a quick test with a Fields and Text block and the same code in the view loop of a view that queries pages. The same problems occurs there. So that's without any Kadence elements. Perhaps you can reproduce that?

Here's the code:

(c) [wpv-post-taxonomy item='[wpv-post-featured-image output="id"]' type='media-owner' format='name']

Again, with the hardcoded ID it works fine. And just getting the ID works fine too:

[wpv-post-featured-image output="id"]

So it seems like a generic issue with wpv-post-featured-image being embedded.

#2384473

Thanks for writing back.

The steps that you've shared, work on my test website, so there must be something specific to your website involved.

Can you please share temporary admin login details, along with the example page where these shortcodes can be seen?

Note: Your next reply will be private and it is recommended to make a complete backup copy, before sharing the access details.

#2385297

Hi Waqar,

I just found something interesting. I created a Toolset templated named "Hero test". It contains a nested shortcode. I added the template to the Kadence element. As you can see, that works.

This was a fairly simple test but using a template might be a work around for this issue. But I wonder why it makes a difference, and if it gives us a clue of how to fix this for real. I'd rather not create additional templates.

#2385493

Thank you for waiting and for sharing your findings.

On my test website too, I was able to reproduce this behavior and the Kadence Pro's element feature doesn't support/recognize the nested shortcodes.
(i.e. where one shortcode is being used in the attribute of another shortcode)


[shortcode_1 attribute="[shortcode_2]"]

This limitation needs to be raised with the Kadence Pro's official support and the workaround of placing the nested shortcode inside a Toolset content template and then placing that template inside the Kadence elements seems like a good choice/workaround.

#2385697

Ok, I have passed on all details to Kadence and they are looking into this. Hopefully this will be fixed.

Thanks for your help!

#2386035

Hi,

I thought Kadence could solve this, but it seems like they do nothing different regarding shortcodes on their end. Here's their response:

"I did some testing. I created a couple shortcode and tested rendering out a shortcode inside a shortcode attribute. Everything worked in elements the same way they worked in pages or posts. This is because elements uses the same system that is in pages or posts to render. There isn't anything I can do on my end and I can't recreate an issue if you just create simple shortcodes.

While I think it's a very poor experience to need to render shortcodes inside of shortcodes I can't create any issue that would make it so you can't do this in elements. This makes me think their system for rendering shortcodes inside of shortcodes depends on some other piece of code they are applying, perhaps the the content filter or something else."

The last sentence makes me think there's something special Toolset does, that apparently fails if the context "is not Toolset". Not sure how else to describe this.

#2386185

Thank you for sharing your communication with the Kadence, but, I find it a bit confusing.

In the start, it mentions that they were able to make the shortcode inside the attribute of another shortcode work, from within the Kadence element.
( which is different from the test results that you and I observed )

Yet, it also states in the end that Toolset must be processing the shortcodes differently.

It is correct that WordPress in general doesn't like the idea of using shortcodes within the shortcodes. But Toolset users rely on this feature heavily, especially when working with the classic/legacy editor, as it provides a lot of flexibility, without the need for additional custom code. For this reason, we continue to support this, within our own content templates and views.

I wouldn't expect Kadence (or even other third-party plugins) to extend their elements, to support this particular feature, so I'll recommend continuing with the workaround of including the complex nested shortcodes inside a Toolset content template and then loading that template in the element, where they're not supported directly.

#2386361

Hi Waqar,

I guess I'll have to agree with you and I should stick to using the Toolset templates as a "wrapper" to make this work inside Kadence elements. It's a classic case of how complex the WordPress ecosystem is when two (or more) suppliers are involved.

Thanks,
Arno