Let's say I have two pages on a WordPress site with content (text, HTML, shortcodes). How could I create a shortcode to display the content from Page A when Page B loads?
I recognize of course that this has the potential to become an infinite loop if used improperly and that there are potential formatting hazards. Best practices aside, there is an important use case for it on our site.
Let's say I have two pages on a WordPress site with content (text, HTML, shortcodes). How could I create a shortcode to display the content from Page A when Page B loads?
I recognize of course that this has the potential to become an infinite loop if used improperly and that there are potential formatting hazards. Best practices aside, there is an important use case for it on our site.
Share Improve this question asked Dec 29, 2016 at 9:38 EnglishTeacherEricEnglishTeacherEric 3632 gold badges7 silver badges17 bronze badges2 Answers
Reset to default 5You can create a shortcode as below:
function wpse250662_post_content_shortcode($atts) {
$args = shortcode_atts( array(
'pagename' => ''
), $atts );
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
$content = apply_filters('the_content',get_the_content( ));
ob_start();
?>
<div class="content">
<?php echo $content; ?>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
return ob_get_clean();
}
add_shortcode('wpse250662-content', 'wpse250662_post_content_shortcode');
USAGE:
[wpse250662-content pagename="
PAGE SLUG YOU WANT TO DISPLAY
"]
Resources: Shortcode API
Jeff Starr's Simple Custom Content plugin provides Shortcodes to add custom content to specific Posts and Pages and one of its features is adding custom content to specific Posts and Pages. The plugin's primary use is adding boilerplate such as copyright information, official policies, disclaimers, etc. but may serve your purpose.
Check out the tl;dr Tags WordPress Plugins for plugins which enable content to be injected into a page or post.