David Stockdale's Scrapcode

Simple Button Linking To Parent Page

A simple bit of shortcode that can be placed onto any page to link to their parent page.

This was another simple sounding function that I found difficult to find a guide on how to do for some reason.

Here’s the shortcode:

/**
 * Adds shortcode (David Stockdale).
 */
add_shortcode( 'parent_finder', 'parent_finder_shortcode' );
/**
 * A button that loads the parent of the current page (David Stockdale).
 * Shortcode should be something like this:
 * [parent_finder]
 */
function parent_finder_shortcode() {
	$link = get_permalink(get_post_parent(get_the_ID()));
	return '<div class="wp-block-button"><a class="wp-block-button__link wp-element-button" href="'.$link.';">Go To Parent Page</a></div>';
}

Simply place that into the “functions.php” file of your child theme.

Then add a shortcode block to whatever page you need linking to it’s parent page with this shortcode:

[parent_finder]

And you should be sorted!

Leave a Reply