WordPress.org

Forums

[resolved] How to add an external url to a featured image (25 posts)

  1. TorahBoy
    Member
    Posted 2 years ago #

    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: http://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.

  2. alchymyth
    Forum Moderator
    Posted 2 years ago #

    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();
    } ?>
  3. TorahBoy
    Member
    Posted 2 years ago #

    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.

  4. TorahBoy
    Member
    Posted 2 years ago #

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

  5. alchymyth
    Forum Moderator
    Posted 2 years ago #

    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;

    http://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.

    http://codex.wordpress.org/Writing_a_Plugin
    http://codex.wordpress.org/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; http://codex.wordpress.org/Function_Reference/esc_url

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

  6. TorahBoy
    Member
    Posted 2 years ago #

    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.

  7. alchymyth
    Forum Moderator
    Posted 2 years ago #

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

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

  8. TorahBoy
    Member
    Posted 2 years ago #

    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.

  9. richame
    Member
    Posted 2 years ago #

    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.

  10. WPyogi
    Forum Moderator
    Posted 2 years ago #

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

    http://wordpress.org/support/topic/padding-to-both-the-top-and-bottom-of-my-fullwidth-template-in-twenty-eleven?replies=2

  11. loudivers
    Member
    Posted 2 years ago #

    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.

  12. WPyogi
    Forum Moderator
    Posted 2 years ago #

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

    http://codex.wordpress.org/Forum_Welcome#Where_To_Post

  13. loudivers
    Member
    Posted 2 years ago #

    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();
    } ?>
  14. befree22
    Member
    Posted 1 year ago #

    @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 http://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 http://wp.tutsplus.com/tutorials/the-ins-and-outs-of-the-enqueue-script-for-wordpress-themes-and-plugins/

  15. alchymyth
    Forum Moderator
    Posted 1 year ago #

    the plugin code is restricted to static pages.

    try to change this line:

    if( is_page() ) {

    to:

    if( is_single() ) {

    or:

    if( is_singular() ) {
  16. befree22
    Member
    Posted 1 year ago #

    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 http://tinypic.com/r/2rxv8gj/8

  17. befree22
    Member
    Posted 1 year ago #

    I'm getting a blank white page when click Draft or Publish when using
    the following code:
    if( is_single() ) {
    if( is_single() ) {

    I never got a blank page with the default code. Now I'm getting a blank white page with the default code also whenever I click Draft or Publish. So now the plugin name as _bak appended to it until this problem is solved.

  18. alchymyth
    Forum Moderator
    Posted 1 year ago #

    blank pages are ususally the result of invalid code, possibly typing errors or so.

    try to enable DEBUG to see if you get error messages; http://codex.wordpress.org/Debugging_in_WordPress

    the latest error free working full relevant code - using 'is_singular()' - is:

    add_filter('post_thumbnail_html','add_external_link_on_page_post_thumbnail',10);
    
        function add_external_link_on_page_post_thumbnail( $html ) {
        if( is_singular() ) {
                global $post;
                $name = get_post_meta($post->ID, 'ExternalUrl', true);
                if( $name ) {
                        $html = '<a href="' . ( $name ) . '" target="_blank" >' . $html . '</a>';
                }
        }
        return $html;
        }
  19. befree22
    Member
    Posted 1 year ago #

    I added the debug code (9 lines) from the Codex to wp-config.php and when I activated your plugin, I got the following message displaying on the Plugins screen:

    The plugin generated 3 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.

    1. No log file showing in /wp-content/debug.log file

    2. The custom field isn't displaying on posts, pages, etc

    Right now, I will delete the debug code from wp-config.php for security reasons.

  20. eddyemmer
    Member
    Posted 1 year ago #

    Hello,

    I want't the same to. But I can't found this line in my page.php.

    <?php the_post_thumbnail(); ?>

    This is what I have:

    <?php get_header(); ?>

    <div id="left" >

    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>

    <div class="post clearfix" id="post-<?php the_ID(); ?>">

    <div class="title">
    <h2>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></h2>
    </div>

    <div class="entry">
    <?php the_content('Read the rest of this entry »'); ?>
    <div class="clear"></div>
    </div>

    </div>

    <?php endwhile; else: ?>

    <h1 class="title">Not Found</h1>
    <p>I'm Sorry, you are looking for something that is not here. Try a different search.</p>

    <?php endif; ?>

    </div>

    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

  21. befree22
    Member
    Posted 1 year ago #

    If I'm creating the plugin, there's no need to modify page.phh, correct?

  22. eddyemmer
    Member
    Posted 1 year ago #

    And how can I make this plugin? Thnks!

  23. befree22
    Member
    Posted 1 year ago #

    I'm still trying to get the plugin to work. The plugin creates a custom field in WP admin where you see or type "ExternalUrl" (instructions given in WP admin after you activate plugin. Here's what I did...

    1. Create a .php file in wp-content/plugins folder and name is something like featuredimageexternallink.php

    2. Add the code that @alchymyth shared in his LAST post above (5 days old).

    3. Inside WP admin, activate the plugin.

    4. In a post page, find the custom field. When you do, you can add any external link for the featured image.

    My problem is that the custom field is NOT displaying in WP admin. Let me know how it works for you.

  24. alchymyth
    Forum Moderator
    Posted 1 year ago #

    The plugin creates a custom field in WP admin where you see or type "ExternalUrl" ...
    My problem is that the custom field is NOT displaying in WP admin. Let me know how it works for you.

    the plugin does not create that custom field automatically; you have to create the custom field manually;
    i.e. under 'Custom Fields' click 'add new', then type 'ExternalUrl' into the 'Name' field, then type your link into the 'Value' field, click 'Add Custom Field'

  25. henryplaythat
    Member
    Posted 1 year ago #

    Alchymyth,

    Thanks for posting this. You're a great asset to the community. Would I be able to add the URL to the thumbnail image as well, so that if the URL custom field has a value, it adds it to both the featured image on the post, and the thumbnail on the blog roll as well?

    I'm guessing something along the lines of:

    add_filter('post_thumbnail_html','add_external_link_on_page_post_thumbnail',10);
    
        function add_external_link_on_page_post_thumbnail( $html ) {
        if( is_singular() or is_page() ) {
                global $post;
                $name = get_post_meta($post->ID, 'ExternalUrl', true);
                if( $name ) {
                        $html = '<a href="' . ( $name ) . '" target="_blank" >' . $html . '</a>';
                }
        }
        return $html;
        }

Topic Closed

This topic has been closed to new replies.

About this Topic