Skip to content

Instantly share code, notes, and snippets.

@kovshenin
Created January 8, 2014 09:19
Show Gist options
  • Save kovshenin/8313977 to your computer and use it in GitHub Desktop.
Save kovshenin/8313977 to your computer and use it in GitHub Desktop.
Append a link to the page parent at the end of the content when on children pages.
<?php
/**
* Plugin Name: Page Parent Link
*/
function my_the_content( $content ) {
$post = get_post();
if ( 'page' == $post->post_type && $post->post_parent > 0 ) {
$parent = new WP_Query( array(
'post_type' => 'page',
'post__in' => array( $post->post_parent ),
) );
if ( $parent->have_posts() ) {
$parent->the_post();
$content .= sprintf( '<p><a href="%s">&larr; %s</a></p>', get_permalink(), get_the_title() );
}
wp_reset_postdata();
}
return $content;
}
add_filter( 'the_content', 'my_the_content' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment