![]() |
![]() |
![]() |
||||
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. |
![]() ![]() |
|
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed. |
|
Thread Tools |
![]() |
#1 |
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.. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#2 |
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">'; } |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#3 |
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">'; } |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#4 | |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,080
|
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. .
__________________
All cookies cleared! |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#5 |
Confirmed User
Join Date: Nov 2004
Posts: 381
|
Thanks a lot sarettah :-)
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#6 |
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?
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#7 | |
Making PHP work
Industry Role:
Join Date: Nov 2002
Location: 🌎🌅🌈🌇
Posts: 20,392
|
Quote:
![]() ![]()
__________________
Make Money with Porn |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#8 | |
Confirmed User
Industry Role:
Join Date: Mar 2017
Posts: 106
|
Quote:
Or you use the code and use a child theme
__________________
cobra69ers at hotmail dot com |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#9 |
Confirmed User
Join Date: Nov 2004
Posts: 381
|
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.
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#10 | |
Confirmed User
Industry Role:
Join Date: Mar 2017
Posts: 106
|
Quote:
![]()
__________________
cobra69ers at hotmail dot com |
|
![]() |
![]() ![]() ![]() ![]() ![]() |