【发布时间】:2016-11-17 14:40:48
【问题描述】:
无法从父页面中获取子页面的摘录。我有一个静态父页面,其中列出了属于该父级的所有子页面的摘录。
出于某种原因,我可以返回每个子页面的每个标题和发布日期。但是,当我尝试回显 get_the_excerpt 或 the_excerpt 时,我得到的只是列出的每个子页面的父页面的摘录或内容。我还将标准摘录转换为自定义修剪摘录。这适用于“首页”,但不适用于标准的“父页面”。
不确定我做错了什么或忽略了什么。
这是出现的内容...注意:“父页面内容”重复。
父页面标题
父页面内容
测试子页面标题 A
父页面内容
2016 年 11 月 16 日
测试子页面标题 B
父页面内容
2016 年 10 月 5 日
使用此解决方案:
<?php
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
// Begin listing all child pages
$childpages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );
foreach( $childpages as $childpage ) { ?>
<div class="paged-entries">
<div class="entry">
<h3 class="page-headline"><?php echo $childpage->post_title; ?></h3>
<!-- .entry-summary -->
<div class="post-excerpt">
<?php
$content = get_extended( $childpage->post_content );
$page_excerpt = wpse0002_custom_wp_trim_excerpt($content);
//echo wpse0002_custom_wp_trim_excerpt($content); // echoes the word "Array" only.
//echo $page_excerpt = wpse0002_custom_wp_trim_excerpt($content); // echoes the word "Array" only.
//echo $content['main']; // prints the full child post expected.
//print_r ($page_excerpt); // prints full post with some php wrapping: Array ( [main] => This is some fresh music. [extended] => [more_text] => )
print_r( $childpage->post_content ); // prints the full child post expected.
?>
</div>
<!-- end .entry-summary -->
<h6>Published: <?php echo date("M Y", strtotime($pfx_date = get_the_date( $format, $childpage->ID ))); // d m y ?></h6>
</div>
</div>
</article>
<?php
}
?>
这就是我所追求的:
父页面标题
父页面内容
测试子页面标题 A
子页面摘录A
2016 年 11 月 16 日
测试子页面标题 B
子页面摘录B
2016 年 10 月 5 日
这是我的自定义修剪器
// 自定义摘录
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wpse_custom_wp_trim_excerpt');
$wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags());
function wpse_allowedtags() {
return '<video>,<audio>,<embed>,<iframe>,<figure>,<figcaption>';
}
if ( ! function_exists( 'wpse0002_custom_wp_trim_excerpt' ) ) :
function wpse0002_custom_wp_trim_excerpt($wpse0002_excerpt) {
global $post;
$raw_excerpt = $wpse0002_excerpt;
if ( '' == $wpse0002_excerpt ) {
$wpse0002_excerpt = get_the_content('');
$wpse0002_excerpt = strip_shortcodes( $wpse0002_excerpt );
$wpse0002_excerpt = apply_filters('the_content', $wpse0002_excerpt);
$wpse0002_excerpt = substr( $wpse0002_excerpt, 0, strpos( $wpse0002_excerpt, '</p>' ) + 4 );
$wpse0002_excerpt = str_replace(']]>', ']]>', $wpse0002_excerpt);
$wpse0002_excerpt = strip_tags($wpse0002_excerpt, wpse_allowedtags()); /*IF you need to allow just certain tags. Delete if all tags are allowed */
return $wpse0002_excerpt;
}
return apply_filters('wpse0002_custom_wp_trim_excerpt', $wpse0002_excerpt, $raw_excerpt);
}
endif;
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wpse0002_custom_wp_trim_excerpt');
【问题讨论】:
-
避免使用
$page,因为它是一个引用当前(即父)页面的全局变量:请改用不同的变量名。这可能会解决您的问题。 -
@Dre这是一个值得注意的更新。我将更新上面的代码。然而,这并没有解决问题,我仍然得到整个帖子内容。使用@Stanimir Stoyanov建议查看我在上面添加的内容。 -
您确定
echo get_the_excerpt($childpage->ID)不起作用吗?另外,您是如何创建自定义修剪摘录的? -
@Dre 是的。这只是打印我正在显示子页面的实际父页面的摘录。见上文,因为我也添加了自定义修剪并更新了上面的代码。
-
啊,那个视觉确实澄清了一些事情!我现在明白你试图用你的过滤器做什么了。这在一定程度上改变了我的回答;我会在一段时间内更新它以反映这一点。所以,如果我理解这一点:您希望您的摘录获取第一个 YouTube 嵌入、音频标签和然后第一段?
标签: php wordpress parent-child