![]() |
Can somebody help me with one line of PHP (wordpress)?
I have a template where the following code creates the link for the featured image:
$output .= '<a href="' . esc_url( get_the_permalink( $post_id ) ) . '" class="entry-featured-image-url">'; now I need it changed, so that it links to an url specified in the custom field "ExternalUrl" with target blank and if there is no Url in that field, it links normaly (like it is now) to the permalink page (without target blank). I did stuff like this before in other templates but I am not good with PHP and having it in this format ($output .= ...) causes trouble for me.. |
Actually the following works, but no matter how I implent it, I can't change "https://www.test.com" to the custom field ExternalUrl, what am I doing wrong?
$externalurl = get_post_meta( get_the_ID(), 'ExternalUrl', true); if( ! empty( $externalurl) ) { $externalurl = get_post_meta($post->ID, 'ExternalUrl', true); $output .= '<a href="https://www.test.com" class="entry-featured-image-url">'; } else{ $output .= '<a href="' . esc_url( get_the_permalink( $post_id ) ) . '" class="entry-featured-image-url">'; } |
solved...
for anybody who runs into such a (similar) problem, this works: $externalurl = get_post_meta( get_the_ID(), 'ExternalUrl', true); if( ! empty( $externalurl) ) { $externalurl = get_post_meta($post->ID, 'ExternalUrl', true); $output .= '<a href="' . get_post_meta( get_the_ID(), 'ExternalUrl', true) . '" class="entry-featured-image-url" target="_blank">'; } else{ $output .= '<a href="' . esc_url( get_the_permalink( $post_id ) ) . '" class="entry-featured-image-url">'; } |
Quote:
Second, here is a little cleaned up version of the same thing for you. Once you pull the externalurl into the $externalurl variable, use that for the output. Since you do that at the start, you do not have to re=-do it inside the if. $externalurl = get_post_meta( get_the_ID(), 'ExternalUrl', true); if( ! empty( $externalurl) ) { $output .= '<a href="' . $externalurl . '" class="entry-featured-image-url" target="_blank">'; } else { $output .= '<a href="' . esc_url( get_the_permalink( $post_id ) ) . '" class="entry-featured-image-url">'; } Hope that helps. . |
Thanks a lot sarettah :-)
|
How complicated would it be to create a plugin that performs these changes? With such a plugin I wouldn't have to change the code of the theme each time it gets an update. I saw a plugin of this kind, that has like 10 lines of code, but I guess I need a plugin specific to my template?
|
Quote:
|
Quote:
Or you use the code and use a child theme |
Quote:
|
Quote:
|
All times are GMT -7. The time now is 08:19 PM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123