External Link Featured Images

Hey folks,

I’m looking to externally link the Featured Images of posts that have a custom field (‘external_url’). I’ve created a function (external_link) that captures just this URL and it’s working great with the Post Titles… but I can’t figure out where the Featured Image URL is coming from.

My best guess-timation is that it’s in Framework - Functions - Frontend - Featured.php? I’ve got the Child Theme, etc., running. Here’s my code:

  switch ( is_singular() ) {
    case true:
      printf( '<div class="entry-thumb">%s</div>', $thumb );
      break;
    case false:
      printf( '<?php external_link(); ?>%3$s</a>', // this is where I think I need it?
        esc_url( get_permalink() ),
        esc_attr( sprintf( __( 'Permalink to: "%s"', '__x__' ), the_title_attribute( 'echo=0' ) ) ),
        $thumb
      );
      break;
  }

Hi Luke,

Thanks for reaching out.

Yes, it’s in that file but you don’t need to copy it as a file. Instead, please just copy the code from that file to child theme’s functions.php along with your customization, example

 function x_featured_image( $cropped = '' ) {

    $stack     = x_get_stack();
    $fullwidth = ( in_array( 'x-full-width-active', get_body_class() ) ) ? true : false;

    if ( has_post_thumbnail() ) {

      if ( $cropped == 'cropped' ) {
        if ( $fullwidth ) {
          $thumb = get_the_post_thumbnail( NULL, 'entry-cropped-fullwidth', NULL );
        } else {
          $thumb = get_the_post_thumbnail( NULL, 'entry-cropped', NULL );
        }
      } else {
        if ( $fullwidth ) {
          $thumb = get_the_post_thumbnail( NULL, 'entry-fullwidth', NULL );
        } else {
          $thumb = get_the_post_thumbnail( NULL, 'entry', NULL );
        }
      }

      switch ( is_singular() ) {
        case true:
          printf( '<div class="entry-thumb">%s</div>', $thumb );
          break;
        case false:
          printf( '<a href="%1$s" class="entry-thumb" title="%2$s">%3$s</a>',
            esc_url( your_external_link() ),
            esc_attr( sprintf( __( 'Permalink to: "%s"', '__x__' ), the_title_attribute( 'echo=0' ) ) ),
            $thumb
          );
          break;
      }

    }

  }

I use the function name your_external_link() as a sample since this line is wrong

printf( '<?php external_link(); ?>%3$s</a>', // this is where I think I need it?

Hope this helps.

Thanks Rad.

I think we might need to tweak my function to make this work. Here’s how the external_url function looks right now:

function external_link() {
	global $post;
	$thePostID = $post->ID;
	$post_id = get_post($thePostID);
	$title = $post_id->post_title;
	$perm = get_permalink($post_id);
	$post_keys = array(); $post_val = array();
	$post_keys = get_post_custom_keys($thePostID);
 
if (!empty($post_keys)) {
	foreach ($post_keys as $pkey) {
		if ($pkey=='external_url') {
		$post_val = get_post_custom_values($pkey);
		}
	}
	if (empty($post_val)) {
		$link = $perm;
	} else {
		$link = $post_val[0];
	}
	} else {
		$link = $perm;
	}
	echo '<a href="'.$link.'" rel="bookmark" title="'.$title.'">';
}

You can see that the featured image is just linking to its own page: https://recyclingdev.wpengine.com/news/

I suspect we’re going to need to get the PHP to print that .$link by its lonesome, yeah? I haven’t been able to figure it out as of yet… Any ideas?

Thanks! - Luke

Hi Luke,

Yes, you need to change this line from your code

echo '<a href="'.$link.'" rel="bookmark" title="'.$title.'">';

to just this line

return $link;

Then this code block

    case false:
      printf( '<?php external_link(); ?>%3$s</a>', // this is where I think I need it?
        esc_url( get_permalink() ),
        esc_attr( sprintf( __( 'Permalink to: "%s"', '__x__' ), the_title_attribute( 'echo=0' ) ) ),
        $thumb
      );
      break;

should be changed to this

case false:
          printf( '<a href="%1$s" class="entry-thumb" title="%2$s">%3$s</a>',
            esc_url( external_link() ),
            esc_attr( sprintf( __( 'Permalink to: "%s"', '__x__' ), the_title_attribute( 'echo=0' ) ) ),
            $thumb
          );
          break;

The problem there is the use of ECHO while calling it where ECHO will not work (eg. printf).

Thanks!

OK, that fixed up the Featured Image—but now the Title doesn’t work (with changing that child template).

Any advice on what I’m doing wrong here?

<h2 class="entry-title">
        <a href="<?php external_link(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to: "%s"', '__x__' ), the_title_attribute( 'echo=0' ) ) ); ?>"><?php the_title(); ?></a>
</h2>

Thank you!

Hi Luke,

The code looks correct. May I know which file and in what directory did you add the file in your child theme.

Kindly review the link below.

Thanks

Hi Paul,

It was working before we made the switch to $link! The path to the file is:

x-child / framework / views / renew / _content-post-header.php

Do I need to run that in the functions.php file? It seems that it’s not even registering the custom function.

Thanks!

Hi Luke,

Now that external_link uses return instead of echo, you should now use echo on the template. Example,

<h2 class="entry-title">
        <a href="<?php echo external_link(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to: "%s"', '__x__' ), the_title_attribute( 'echo=0' ) ) ); ?>"><?php the_title(); ?></a>
</h2>

The good side of using return, you can use your function anywhere, and just add echo just when needed.

Thanks!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.