Wordpress:将一个页面的内容包含在另一个页面中

2022-08-30 15:14:19

如何将一个或多个页面的页面内容包含在另一个页面中?

前任。我有页面A页面B页面C,我想在页面X中包含这些页面的内容

是否有wordpress功能可以加载指定页面/帖子的帖子?
喜欢show_post(“pageA”)??


答案 1

WordPress核心中没有功能本身,但它非常容易编写:show_post()

function show_post($path) {
  $post = get_page_by_path($path);
  $content = apply_filters('the_content', $post->post_content);
  echo $content;
}

请注意,这将使用页面的路径调用,即:

<?php show_post('about');  // Shows the content of the "About" page. ?>
<?php show_post('products/widget1');  // Shows content of the "Products > Widget" page. ?>

当然,我可能不会像WordPress core将来添加同名函数那样笼统地命名一个函数。不过,您的选择。show_post()

另外,没有丝毫的意思@kevtrout因为我知道他非常好,请考虑将来在StackOverflow的姊妹网站WordPress Answers上发布您的WordPress问题。在那里回答问题的WordPress爱好者的比例要高得多。


答案 2

我在Wordpress论坛上找到了这个答案。您可以向函数添加一些代码.php然后随时使用短代码。

function get_post_page_content( $atts ) {
        extract( shortcode_atts( array(
            'id' => null,
            'title' => false,
        ), $atts ) );

        $the_query = new WP_Query( 'page_id='.$id );
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
                if($title == true){
                the_title();
                }
                the_content();
        }
        wp_reset_postdata();

    }
    add_shortcode( 'my_content', 'get_post_page_content' );

对于短代码,

[my_content id="Enter your page id number" title=Set this to true if you want to show title /]

推荐