Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar Mark Forums Read
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 04-13-2020, 03:32 AM   #1
seoguy
Confirmed User
 
Join Date: Nov 2004
Posts: 381
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 is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-13-2020, 04:54 AM   #2
seoguy
Confirmed User
 
Join Date: Nov 2004
Posts: 381
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 is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-13-2020, 06:30 AM   #3
seoguy
Confirmed User
 
Join Date: Nov 2004
Posts: 381
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">';
}
seoguy is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-13-2020, 07:14 AM   #4
sarettah
see you later, I'm gone
 
Industry Role:
Join Date: Oct 2002
Posts: 14,080
Quote:
Originally Posted by seoguy View Post
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.

.
__________________
All cookies cleared!
sarettah is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-13-2020, 07:35 AM   #5
seoguy
Confirmed User
 
Join Date: Nov 2004
Posts: 381
Thanks a lot sarettah :-)
seoguy is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-13-2020, 07:55 AM   #6
seoguy
Confirmed User
 
Join Date: Nov 2004
Posts: 381
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?
seoguy is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-13-2020, 09:43 AM   #7
blackmonsters
Making PHP work
 
blackmonsters's Avatar
 
Industry Role:
Join Date: Nov 2002
Location: 🌎🌅🌈🌇
Posts: 20,392
Quote:
Originally Posted by sarettah View Post
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.

.
__________________
Make Money with Porn
blackmonsters is online now   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-13-2020, 12:55 PM   #8
Cobra69erss
Confirmed User
 
Industry Role:
Join Date: Mar 2017
Posts: 106
Quote:
Originally Posted by seoguy View Post
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
__________________
cobra69ers at hotmail dot com
Cobra69erss is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-13-2020, 02:11 PM   #9
seoguy
Confirmed User
 
Join Date: Nov 2004
Posts: 381
Quote:
Originally Posted by Cobra69erss View Post
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.
seoguy is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-13-2020, 02:43 PM   #10
Cobra69erss
Confirmed User
 
Industry Role:
Join Date: Mar 2017
Posts: 106
Quote:
Originally Posted by seoguy View Post
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.
__________________
cobra69ers at hotmail dot com
Cobra69erss is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks

Tags
$output, links, url, blank, target, field, php, page, stuff, permalink, normaly, format, trouble, templates, changed, featured, image, creates, code, template, custom, externalurl, line, wordpress, link
Thread Tools



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.