Last active
May 28, 2019 08:25
-
-
Save DriftwoodJP/23a1d0fdffb88823d93ac7fcaf03a80f to your computer and use it in GitHub Desktop.
How to link the WordPress title & thumbnail to an external URL.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> | |
<header class="entry-header"> | |
<?php | |
if ( has_post_thumbnail() ) : | |
the_post_thumbnail(); | |
else : | |
print_post_no_image_thumbnail(); | |
endif; | |
?> | |
<?php | |
if ( is_singular() ) : | |
the_title( '<h1 class="entry-title">', '</h1>' ); | |
else : | |
print_post_title(); | |
endif; | |
?> | |
</header><!-- .entry-header --> | |
</article> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Add Dashicons | |
*/ | |
function load_dashicons_front_end() { | |
wp_enqueue_style( 'dashicons' ); | |
} | |
add_action( 'wp_enqueue_scripts', 'load_dashicons_front_end' ); | |
/** | |
* Generate the post title link tag to external url with icon. | |
* | |
* @return void | |
*/ | |
function print_post_title() { | |
$id = get_the_ID(); | |
$title = get_the_title( $id ); | |
$key_value = get_post_meta( $id, 'external_url', true ); | |
$icon = ''; | |
// Check the custom field value. | |
if ( ! empty( $key_value ) ) { | |
$path = $key_value; | |
$extension = pathinfo( $path, PATHINFO_EXTENSION ); | |
if ( 'pdf' === $extension ) { | |
$icon = '<span class="entry-icn dashicons dashicons-media-default"></span>'; | |
} else { | |
$icon = '<span class="entry-icn dashicons dashicons-external"></span>'; | |
} | |
} else { | |
$path = get_permalink( $id ); | |
} | |
// @link https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Whitelisting-code-which-flags-errors | |
echo '<p class="entry-title"><a href="' . esc_url( $path ) . '" rel="bookmark" title="' . esc_html( $title ) . '">' . esc_html( $title ) . '</a>' . $icon . '</p>'; // WPCS: XSS ok. | |
} | |
/** | |
* Generate the post no image thumbnail tag to external url. | |
* | |
* @return void | |
*/ | |
function print_post_no_image_thumbnail() { | |
$id = get_the_ID(); | |
$key_value = get_post_meta( $id, 'external_url', true ); | |
// Check the custom field value. | |
if ( ! empty( $key_value ) ) { | |
$path = $key_value; | |
} else { | |
$path = get_permalink( $id ); | |
} | |
echo '<a class="post-thumbnail" href="' . esc_url( $path ) . '" aria-hidden="true" tabindex="-1">'; | |
echo '<img class="attachment-post-thumbnail" src="' . esc_url( get_template_directory_uri() ) . '/img/news-no-image.png">'; | |
echo '</a>'; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment