• Resolved Kevin

    (@torahboy)


    I’ve been trying to find a simple way to add an external url to a featured image on a page. Where someone will click on the featured image and a new external url will open in another window.

    I found this code Example #1:

    ‘<?php $name = get_post_meta($post->ID, ‘ExternalUrl’, true); ?>
    “<img />

    I added the name ExternalUrl myself.

    I also added a custom field to my page:
    Name: ExternalUrl
    Value: https://www.google.com

    Now my problem….
    1. Where do I put the code (Example #1)? Does it go in the functions.php? or another file?
    2. Does “get_post_meta” work for pages also?

    If anyone can give me their expert guidance on this, I would truly appreciate it. I know I’m close, (I think, I hope) but I haven’t really a clue as to where this code goes. Thanks.

Viewing 15 replies - 1 through 15 (of 24 total)
  • 1.
    the code should possibly go into page.php of your theme;

    try and locate the line with the_post_thumbnail() in page.php, and add the link there;

    example:

    from:

    <?php the_post_thumbnail(); ?>

    to:

    <?php $name = get_post_meta($post->ID, 'ExternalUrl', true);
    if( $name ) { ?>
    <a href="<?php echo $name; ?>"><?php the_post_thumbnail(); ?></a>
    <?php } else {
    the_post_thumbnail();
    } ?>

    Thread Starter Kevin

    (@torahboy)

    You are awesome. It worked like a charm. That was exactly the solution I needed. And you delivered it in such a simple way that even I could follow your instruction and your visual demonstration.

    Thank you so much again. I truly appreciate your time and WordPress expertise in helping me out with this problem.

    Thread Starter Kevin

    (@torahboy)

    Is there a simple way in making this a simple plugin?

    Is there a simple way in making this a simple plugin?

    I am not sure about ‘simple’ but you can try and integrate the linking code with a filter function for ‘post_thumbnail_html’, and pack that into a plugin;

    https://pastebin.com/vEbaGp4t

    using FTP, create a new file in the /wp-content/plugins/ folder, and add the code into it; activate from within the plugins’ panel in admin.

    https://codex.www.ads-software.com/Writing_a_Plugin
    https://codex.www.ads-software.com/Plugin_API/Filter_Reference

    I have added in_the_loop() to make sure that the link is only added if the thumbnail is in the loop:
    and I added esc_url() for the link url; https://codex.www.ads-software.com/Function_Reference/esc_url

    if any of these make problems for what you are using the code, just remove them.

    Thread Starter Kevin

    (@torahboy)

    Wow, what a great lesson. First off, thanks so much again for your time. I created a new file and added your code to it. Then I activated it through the plugins manager. I read all of your resource links then tried out the plugin.

    First, I removed the original code I had placed in the page.php:

    ‘<?php $name = get_post_meta($post->ID, ‘ExternalUrl’, true);
    if( $name ) { ?>
    “><?php the_post_thumbnail(); ?>
    <?php } else {
    the_post_thumbnail();
    } ?>’

    I saved and refreshed my test page, but no featured image was visible.

    So I removed the in_the_loop()

    FROM THIS:
    ‘if( is_page() && in_the_loop() )’

    TO THIS:
    ‘if( is_page() )’

    I saved and refreshed my test page, but no featured image was visible.

    Then I removed the esc_url()

    FROM THIS:
    ‘$html = ‘<a href=”‘ . esc_url( $name ) . ”

    TO THIS:
    ‘$html = ‘<a href=”‘ . ( $name ) . ”

    I saved and refreshed my test page, but no featured image was visible.

    Since my PHP skills are still somewhat lame, (but vastly improving IMHO) I’m hoping that I removed something that I shouldn’t have or added some space I shouldn’t have. In either case, if you have a moment, would you take a look and tell me what I did wrong? I have already been inspired by your website to dig deeper into all of this. So again, thanks for firing me up on learning more, and trying to improve my skills. Best Wishes.

    First, I removed the original code I had placed in the page.php:

    did you keep <?php the_post_thumbnail(); ?> where it originally was?

    Thread Starter Kevin

    (@torahboy)

    OY! I still had it commented out. I’m so sorry… what a rookie mistake.

    I uncommented it, and still having removed the in_the_loop() and esc_url() in the code you so graciously provided…

    NOTE: I also discovered that a backtick isn’t a single quote… ??

    It worked like a champ!

    Thank you, thank you, thank you. I learned so much about WordPress pages and plugins, and I am so grateful and blessed for your help, instruction, time, and patience. I also look forward to reading more of your posts on TPT.

    Hello alchymyth,i seriously need your help and that is why am being forced to write against your latest reply just in my opinion, get your immediate attention.Deep from my heart i want to thank you for all the reply you have provided in this platform.
    1.My first problem is how to control my twenty eleven theme padding to the TOP,BOTTOM,RIGHT AND LEFT using the Default template to enable me close the spaces or gap around my site content.
    2.My second HEADACHE and NIGHTMARE is that because i can’t control the padding in the twenty eleven theme as i have stated above,i have created custom fullwidth template using my page.php which enabled me to control only the padding to the right.Kindly please guide me on how to control the rest of the padding(TOP,BOTTOM and LEFT).
    3.Would be very grateful to hear from you soon with the exact codes to use.Thanks in advance.

    @richame – please stick with your thread – this thread is on a totally different question and it is already marked resolved.

    https://www.ads-software.com/support/topic/padding-to-both-the-top-and-bottom-of-my-fullwidth-template-in-twenty-eleven?replies=2

    thank you guys so much, I was just trying to figure this out. Is there any way to get the external link to open in a new tab.

    @loudivers – please start your own thread – this one is resolved. –

    https://codex.www.ads-software.com/Forum_Welcome#Where_To_Post

    never mind – got it.
    for anyone else that may need to know

    <?php $name = get_post_meta($post->ID, 'ExternalUrl', true);
    if( $name ) { ?>
    <a href="<?php echo $name; ?>"target="_blank">><?php the_post_thumbnail(); ?></a>
    <?php } else {
    the_post_thumbnail();
    } ?>

    @alchymyth Could you help me get the plugin working?

    I read this thread to create the featured external link plugin. It displays in list of plugins in WP admin but not in the post. I have Custom Fields checked in screen display. However, the custom field is not displaying in post so I’m unable to add “ExternalUrl”.

    1. I have a file in /plugins/ folder called featuredexternallink.php
    2. Code used is https://pastebin.com/gMpGufV4
    3. I read a couple years ago that good plugins all have enquery code to prevent incompatibility with other plugins. Could you tell me the code I need to add to prevent plugin incompatibility? I’m reading https://wp.tutsplus.com/tutorials/the-ins-and-outs-of-the-enqueue-script-for-wordpress-themes-and-plugins/

    the plugin code is restricted to static pages.

    try to change this line:

    if( is_page() ) {

    to:

    if( is_single() ) {

    or:

    if( is_singular() ) {

    I changed the default to each of the 2 lines but no custom field appears in the Edit Listing page.

    I forgot to mention that I’m using a business directory theme but it integrates well with native WordPress. It’s called Vantage — see https://tinypic.com/r/2rxv8gj/8

Viewing 15 replies - 1 through 15 (of 24 total)
  • The topic ‘How to add an external url to a featured image’ is closed to new replies.