从父页面中获取子页面的__excerpt

时间:2016-11-17 14:40:49

标签: php wordpress parent-child

无法从父页面中获取子页面的摘录。我有一个静态父页面,在其中列出了所有父页面的摘录。

出于某种原因,我可以返回每个标题并发布每个子页面的日期。但是,当我尝试回显get_the_excerpt或the_excerpt时,我得到的是所列出的每个子页面的父页面的摘录或内容。此外,我还将标准摘录转换为自定义修剪摘录。这适用于"头版"但不是在标准的父母页面中#34;。

不确定我做错了什么或忽略了什么。

这就是出现的......注意:"父页面内容"重复。

  

父页面标题

     

父页面内容

     
    

测试儿童页面标题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(']]>', ']]&gt;', $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');

enter image description here

1 个答案:

答案 0 :(得分:1)

您应该使用get_extended获取更多标记之前的文本,context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { // weave the node in the route which has id = bar // and replace it with the following route path weaveById("bar").replace().multicast().to("mock:a").to("mock:b"); } }); 。如果我没有错,context.getRouteDefinition("yourRouteId").adviceWith(context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { weaveById("yourEndpointId").replace().setBody(new ConstantExpression("whateveryouwant" )); } }); 会从摘录字段返回内容,而get_excerpt会在更多标记之前返回内容。所以你的代码将成为:

get_the_excerpt