0

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.

2

You 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

0

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.

  • It seems like an interesting plugin but I'm not sure that solves the challenge of displaying markup from another page. – EnglishTeacherEric Dec 30 '16 at 6:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.