Skip to content

Instantly share code, notes, and snippets.

@BinaryMoon
Created August 11, 2020 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BinaryMoon/786f0dc7d9a67a43dbac3f9a90fc2fa6 to your computer and use it in GitHub Desktop.
Save BinaryMoon/786f0dc7d9a67a43dbac3f9a90fc2fa6 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Add post tags to the excerpt.
* Plugin URI: https://prothemedesign.com
* Description: Add post tags to the excerpt.
* Author: Ben Gillbanks
* Version: 1.0
* Author URI: https://prothemedesign.com
* Text Domain: ptd
*/
/**
* Add post tags to the end of the excerpt.
*
* @param string $excerpt The post excerpt.
* @return string
*/
function ptd_excerpt_tags( $excerpt ) {
$tags = get_the_tags();
$html = '';
// Display tags (if there are any).
if ( $tags ) {
foreach( $tags as $tag ) {
$html .= sprintf(
'<a href="%1$s">%2$s</a>',
esc_url( $tag->name ),
esc_html( $tag->name )
);
}
}
if ( $html ) {
$html = '<span class="post-tags">' . $html . '</span>';
}
return $excerpt . $html;
}
add_filter( 'get_the_excerpt', 'ptd_excerpt_tags' );
/**
* Display post tags styles.
*
* @return void
*/
function ptd_excerpt_tags_styles() {
?>
<style>
.post-tags {
display: block;
font-size: 0.7rem;
margin-top: 8px;
}
.post-tags a {
padding: 0.1em 0.4em;
background: #eee;
display: inline-block;
margin-inline-end: 0.25em;
}
</style>
<?php
}
add_action( 'wp_head', 'ptd_excerpt_tags_styles' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment