最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Display content from another site page using a shortcode

programmeradmin2浏览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.

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 badges
Add a comment  | 

2 Answers 2

Reset to default 5

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

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.

发布评论

评论列表(0)

  1. 暂无评论