Skip to content

Instantly share code, notes, and snippets.

@davidwolfpaw
Created September 8, 2018 18:37
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 davidwolfpaw/0baa572e4757bb2c41ac59f88a2cafce to your computer and use it in GitHub Desktop.
Save davidwolfpaw/0baa572e4757bb2c41ac59f88a2cafce to your computer and use it in GitHub Desktop.
This is a custom shortcode to allow the link to a post while using the Genesis Framework for WordPress
<?php
/*
* This is a custom shortcode to allow the link to a post while using the Genesis Framework.
*
* It was created to fill the need of a loop that needed links to posts.
*/
add_shortcode( 'post_link', 'genesis_post_link_shortcode' );
/**
* Produces a link to the post with custom text.
*
* Supported shortcode attributes are:
* after (output after link, default is empty string),
* before (output before link, default is empty string),
* link_text (output text of link, default is 'Read More &hellip;')
*
* Output passes through 'genesis_post_link_shortcode' filter before returning.
*
* @param array|string $atts Shortcode attributes. Empty string if no attributes.
* @return string Return output for `post_link` shortcode.
*/
function genesis_post_link_shortcode( $atts ) {
$defaults = array(
'link_text' => 'Read More &hellip;',
'before' => '',
'after' => '',
);
$atts = shortcode_atts( $defaults, $atts, 'post_link' );
$link = get_the_permalink();
if ( genesis_html5() ) {
$output = $atts['before'] . sprintf( '<a href="%s">%s</a>', $link, $atts['link_text'] ) . $atts['after'];
} else {
$output = $atts['before'] . '<a href="' . $link . '">' . $atts['link_text'] . '</a>' . $atts['after'];
}
return apply_filters( 'genesis_post_link_shortcode', $output, $atts );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment