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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4090698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:55:10  来源:igfitidea点击:

Wordpress: include content of one page in another

phpwordpress

提问by Zebra

How do I include the page content of one or more page in another page?

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

ex. I have pageA, pageBand pageCand I want to include the contents of these pages in pageX

前任。我有pageApageBpageC并且我想在pageX 中包含这些页面的内容

is there a wordpress function that loads the post of a specified page/post?
like show_post("pageA")??

是否有加载指定页面/帖子的帖子的 wordpress 功能?
像 show_post("pageA")??

回答by MikeSchinkel

Hi @dany:

@dany

There is not a show_post()function per se in WordPress core but it is extremely easy to write:

show_post()WordPress 核心本身没有函数,但编写起来非常容易:

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

Note that this would be called with the page's path, i.e.:

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

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

Of course I probably wouldn't name a function as generically as show_post()in case WordPress core adds a same-named function in the future. Your choice though.

当然,我可能不会像show_post()WordPress 核心将来添加同名函数那样笼统地命名函数。你的选择。

Also, and no slight meant to @kevtroutbecause I know he is very good, consider posting your WordPress questions on StackOverflow's sister site WordPress Answersin the future. There's a much higher percentage of WordPress enthusiasts answering questions over there.

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

Hope this helps.

希望这可以帮助。

-Mike

-麦克风

回答by Dario

You could install a Plugin "Improved Include Page". Once installed, you create page X and enter:

您可以安装插件“改进的包含页面”。安装后,您创建页面 X 并输入:

[include-page id="123"]
[include-page id="124"]
[include-page id="125"]

where these are the ID's of pages A, B and C respectively

其中这些分别是页面 A、B 和 C 的 ID

回答by Kit Johnson

I found this answerposted on the Wordpress forums. You add a little code to functions.php and then just use a shortcode whenever you like.

我在 Wordpress 论坛上找到了这个答案。您向functions.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' );

For the shortcode,

对于短代码,

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

回答by kevtrout

Pages are just posts, with a post_type of 'page' in the database. You can show the content of multiple pages on another page by writing a post query in your pageX template that gets the posts you specify and output them in a Loop.

页面只是帖子,数据库中的 post_type 为“页面”。您可以通过在 pageX 模板中编写帖子查询来在另一个页面上显示多个页面的内容,该查询获取您指定的帖子并在循环中输出它们。

There are three ways to get post content from the database:

从数据库中获取帖子内容的方式有以下三种:

  1. get_posts
  2. query_posts
  3. WP_Query
  1. 获取帖子
  2. 查询帖子
  3. WP_Query

These links all point to the WordPress Codex. Get_posts and query_posts have an argument available, 'page_id', where you can specify the id of the page you'd like to retrieve and display.

这些链接都指向 WordPress Codex。Get_posts 和 query_posts 有一个可用参数“page_id”,您可以在其中指定要检索和显示的页面的 id。

回答by Stuart

<?php query_posts('p=43');

global $more;
//set $more to 0 in order to only get the first part of the post
$more = 0; 

    // the Loop
    while (have_posts()) : the_post(); 

     // the content of the post ?>
    the_title();
    the_content(); 

    endwhile; ?>

This is obviously a portion of the post, I got the detail from the wordpress codex.

这显然是帖子的一部分,我从 wordpress codex 中得到了详细信息。

回答by Hendy

Interesting... I looked around for how to embed Wordpress content elsewhere(as in, on another website), and found some things...

有趣......我四处寻找如何在其他地方(如在另一个网站上)嵌入 Wordpress 内容,并发现了一些东西......

  • www . shooflydesign.org/buzz/past/embedding_wordpress . html
    • Shows how to embed Wordpress content in another site with a php script; maybe just use the same thing to embed Wordpress into itself?
  • www . corvidworks . com/articles/wordpress-content-on-other-pages
    • Similar concept; embed Wordpress on another page; just try to use that tool in a new WP post
  • feeds themselves
    • My searching pulled up some suggestions to just use a feed to your own blog to embed into a post. There's nothing preventing that. You might want it automated and so restructuring the feed to look right might be problematic, but it's worth a shot depending on what you want to do.
  • 万维网。shooflydesign.org/buzz/past/embedding_wordpress。html
    • 展示了如何使用 php 脚本将 Wordpress 内容嵌入到另一个站点中;也许只是使用相同的东西将 Wordpress 嵌入到自身中?
  • 万维网。鸦片厂。com/articles/wordpress-content-on-other-pages
    • 类似的概念;将 Wordpress 嵌入另一个页面;尝试在新的 WP 帖子中使用该工具
  • 喂自己
    • 我的搜索提出了一些建议,只需使用您自己博客的提要即可嵌入到帖子中。没有什么可以阻止的。您可能希望它自动化,因此重组提要以使其看起来正确可能会出现问题,但值得一试,具体取决于您想要做什么。

Hope those are quasi-helpful. While they all are solutions for getting your WP content on some place otherthan WP... they might work for your given question and allow you to display A, B, and C on X.

希望这些是准有帮助的。虽然它们都是在 WP以外的其他地方获取 WP 内容的解决方案……但它们可能适用于您的给定问题,并允许您在 X 上显示 A、B 和 C。

回答by Neil

There is a Wordpress function to display the content of a particular page inside another using query_posts() it is:

有一个 Wordpress 函数可以使用 query_posts() 在另一个页面中显示特定页面的内容,它是:

<?php query_posts("posts_per_page=1&post_type=page&post_id=134"); the_post(); ?>

You set the number of pages to be displayed to 1, post type is page instead of post and the page id

您将要显示的页数设置为 1,帖子类型是页面而不是帖子和页面 id

回答by user2961664

Kit Johnson's wordpress forum solution with creating a shortcode works, but adds the inserted page in the top of the new page, not where the shortcode was added. Close though, and may work for other people.

Kit Johnson 的 wordpress 论坛解决方案创建了一个短代码,但在新页面的顶部添加了插入的页面,而不是添加了短代码的位置。关闭,但可能对其他人有用。

from the wordpress post, I pieced together this which inserts the page where the shortcode is put:

从 wordpress 帖子中,我拼凑了这个插入了放置短代码的页面:

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

    $the_query = new WP_Query( 'page_id='.$id );
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
            if($title == true){
            $output .= get_the_title();
            }
            $output .= get_the_content();
    }
    wp_reset_postdata();
    return $output;

}

Then, the shortcode bit works as expected. If you don't want the title, title=false does not work, you need to leave title off entirely.

然后,短代码位按预期工作。如果您不想要标题,则 title=false 不起作用,您需要完全关闭标题。