wordpress 获取子页面内容
时间 : 2023-12-19 23:16:02声明: : 文章内容来自网络,不保证准确性,请自行甄别信息有效性

最佳答案

要从WordPress获取子页面的内容,你可以使用WordPress提供的函数和API。

首先,你需要获取当前页面的子页面的ID。可以使用函数get_children()来获取子页面的ID数组。下面是具体的代码示例:

$child_pages = get_children( array(

'post_parent' => get_the_ID(), // 当前页面的ID

'post_type' => 'page', // 只检索页面

'orderby' => 'menu_order' // 按照菜单顺序进行排序

) );

if ( $child_pages ) {

foreach ( $child_pages as $child ) {

// 获取子页面的内容

$content = apply_filters( 'the_content', $child->post_content );

// 打印子页面的标题和内容

echo '<h2>' . $child->post_title . '</h2>';

echo $content;

}

}

上述代码首先通过get_children()函数获取了当前页面的子页面的ID数组。然后,通过循环遍历子页面的ID,使用apply_filters()函数和the_content过滤器获取子页面的内容。最后,打印出子页面的标题和内容。

请注意,在使用上述代码之前,你需要确保在一个WordPress页面的模板文件中使用。

其他答案

在WordPress中获取子页面的内容可以通过以下步骤实现:

1. 首先,确定你要获取子页面内容的页面的ID或slug。你可以在WordPress的管理后台中找到该页面的编辑页面,从浏览器地址栏中复制ID或slug。

2. 在你希望显示子页面内容的地方,添加以下代码:

```php

<?php

$child_page = get_page_by_path('子页面的slug'); // 如果使用的是ID,可以使用 get_page_by_id() 函数

if ($child_page) {

$content = apply_filters('the_content', $child_page->post_content);

echo $content;

}

?>

上述代码首先通过`get_page_by_path()`函数或`get_page_by_id()`函数获取子页面的信息,然后使用`apply_filters()`函数将内容过滤并赋值给`$content`变量,最后使用`echo`输出内容。

请注意将代码中的`子页面的slug`替换为实际子页面的slug或使用`get_page_by_id()`函数获取的ID。

这样就可以获取并显示WordPress子页面的内容了。你可以根据需要添加样式或其他内容来定制显示效果。