Hey
I am trying to get the raw URL to print without the HTTP or HTTPS.
Looking at online documentation I used the following:
[types termmeta='image-for-category' output='raw' no_protocol='true'][/types]
But this doesn't seem to work? Am I missing something?
Hi, based on the field name I'm assuming this is an image field. The no_protocol attribute is not available for the image field type, only for the URL field type: https://toolset.com/documentation/customizing-sites-using-php/functions/#image
So this would require a custom shortcode, or the use of a different field type here. The format and syntax for a custom shortcode looks like this:
add_shortcode( 'shortcode-name', 'shortcode_function');
function shortcode_function()
{
// add your code here and return some value to display on screen:
return $something;
}
I don't have a code snippet handy for this specific problem, but you would use the WordPress API get_term_meta to access custom term fields: https://developer.wordpress.org/reference/functions/get_term_meta/
$img_url = get_term_meta( 12345, 'wpcf-fieldslug', true );
... where 12345 represents the term ID.
To get the id of the current archive term, you can do something like this in your shortcode:
$current_taxonomy = isset(get_queried_object()->taxonomy) ? get_queried_object()->taxonomy : null;
$current_term_id = isset(get_queried_object()->taxonomy) ? get_queried_object()->term_id : 0;
Then you would need to do some string manipulation with PHP to strip off the leading protocol string, and return that value somehow with your custom shortcode. Here's a link I found with some code that seems to do the trick:
https://stackoverflow.com/questions/9549866/php-regex-to-remove-http-from-string
Hey Christian
Thanks a lot for all this information. I think I am really close with this but for some reason it is stripping the HTTP/HTTPs from the URL and printing the correct URL on the page but as soon as I try and wrap an img tag round it, it doesn't work.
It looks like its printing the URL but with " " around it maybe, automatically. Here is the code I am using, any ideas why its not working correctly? Or is it not getting the raw URL from the database?
add_shortcode( 'category-url-no-http', 'shortcode_function');
function shortcode_function()
{
$current_taxonomy = isset(get_queried_object()->taxonomy) ? get_queried_object()->taxonomy : null;
$current_term_id = isset(get_queried_object()->taxonomy) ? get_queried_object()->term_id : 0;
$img_url = get_term_meta( $current_term_id, 'wpcf-image-for-category', true );
$str = $img_url;
$str = preg_replace('#^https?://#', '', $str);
echo $str;
return $something;
}
Don't use echo in a shortcode. Return the $str variable instead.
Great thanks Christian. That worked.
Is there a way with Toolset to easily insert the alt tag set for the image? I can only see a way to insert the image tag or Raw URL no way to insert the alt text assigned to the image?
Cheers
You can insert the alt attribute in the img tag using the Types field shortcode, but there's not an easy way to access just the alt text outside of that image tag. I think that would require a custom shortcode, because you would need to be able to access that image's attachment post ID somehow (probably by querying posts with the image URL as the post guid) then use that ID to get the postmeta value of "_wp_attachment_image_alt".
https://toolset.com/documentation/customizing-sites-using-php/functions/#image