GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Can somebody help me with one line of PHP (wordpress)? (https://gfy.com/showthread.php?t=1326207)

seoguy 04-13-2020 03:32 AM

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..

seoguy 04-13-2020 04:54 AM

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">';
}

seoguy 04-13-2020 06:30 AM

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">';
}

sarettah 04-13-2020 07:14 AM

Quote:

Originally Posted by seoguy (Post 22647291)
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">';
}

First, congrats on working through it.

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.

.

seoguy 04-13-2020 07:35 AM

Thanks a lot sarettah :-)

seoguy 04-13-2020 07:55 AM

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?

blackmonsters 04-13-2020 09:43 AM

Quote:

Originally Posted by sarettah (Post 22647312)
First, congrats on working through it.

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.

.

:thumbsup:thumbsup

Cobra69erss 04-13-2020 12:55 PM

Quote:

Originally Posted by seoguy (Post 22647334)
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?

I use the plugin Page Links To for external links

Or you use the code and use a child theme

seoguy 04-13-2020 02:11 PM

Quote:

Originally Posted by Cobra69erss (Post 22647566)
I use the plugin Page Links To for external links

Or you use the code and use a child theme

the problem with Pages Links To is that it links not only the featured image but also the title and the read more button, I only want the featured image linked. And I already use a child theme and this will work for some time, but whenever the theme gets an update and the template changes, I have to manually change it again.

Cobra69erss 04-13-2020 02:43 PM

Quote:

Originally Posted by seoguy (Post 22647631)
the problem with Pages Links To is that it links not only the featured image but also the title and the read more button, I only want the featured image linked. And I already use a child theme and this will work for some time, but whenever the theme gets an update and the template changes, I have to manually change it again.

:thumbsup


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