php - Get the_excerpt of a child page from within a parent page -


having trouble getting excerpt of child page within parent page. have static parent page, within, lists excerpts of child pages hierarchal of parent.

for reason can return each title , publish date of each individual child page. when try echo get_the_excerpt or the_excerpt excerpt or content of parent-page every child page listed. i'm converting standard excerpt custom trimmed excerpt. worked on "front-page" not in standard "parent-page".

not sure i'm doing wrong or overlooking.

this appears... note: "the parent page content" repetition.

parent page title

the parent page content

test child page title a

the parent page content

nov 16, 2016

test child page title b

the parent page content

oct 5, 2016

using solution:

<?php <article id="post-<?php the_id(); ?>" <?php post_class(); ?>>  // begin listing child pages $childpages = get_pages( array( 'child_of' => $post->id, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );   foreach( $childpages $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 word "array" only.                     //echo $page_excerpt = wpse0002_custom_wp_trim_excerpt($content); // echoes word "array" only.                     //echo $content['main']; // prints full child post expected.                     //print_r ($page_excerpt); // prints full post php wrapping: array ( [main] => fresh music. [extended] => [more_text] => )                     print_r( $childpage->post_content ); // prints 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 } ?> 

this i'm after:

parent page title

the parent page content

test child page title a

the child page excerpt a

nov 16, 2016

test child page title b

the child page excerpt b

oct 5, 2016

here custom trimmer

// custom excerpts

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 need allow tags. delete if tags 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

at risk of sounding patronising, i'm going over-explain other folks might find useful. having gone through all, i've got pretty idea of you're trying achieve: there's more 1 way go it, rather giving chunk of code, i'm going walk through logic behind can pick bits applicable.

debugging

firstly, you've got bunch of php errors on place, leading unpredictable behaviour, suggests don't have debugging turned on. add following wp-config.php:

// enable wp_debug mode define( 'wp_debug', true ); 

this display warnings , track down errors coming form. 1 notable error you're calling wpse0001_custom_wp_trim_excerpt() without passing argument.

excerpts

by default, pages -- unlike posts -- don't have separate excerpt field, why you've been using get_extended() extract content before more tag. however, can add excerpt field pages adding following functions.php. i'd recommend doing this, makes subsequent excerpt-related behaviour easier. means wp should automatically handle fallbacks pages don't have excerpts; no sense re-inventing wheel.

/**  * adds excerpt field pages  * @args void  * @returns void  */ function my_add_excerpts_to_pages() {      add_post_type_support( 'page', 'excerpt' ); } add_action( 'init', 'my_add_excerpts_to_pages' ); 

in admin area, might need check excerpt option under screen options display field. lets use wp's default functions , filters displaying , manipulating excerpts. populates $post->post_excerpt key, can use: echo $post->post_excerpt if ever need output without filtering.

the other 'gotcha' excerpts get_the_excerpt() has used in loop; can't pass id of post. i'm pretty sure used to able , it's since been deprecated: methinks online documentation needs updating, i'll try , log change there spare others confusion in future. c'est la vie. brings on our next topic...

secondary loops

you're using get_pages() fetch child pages, fine, because of way excerpts work you're stuck having use own functions fetching , filtering them. approach use wp_query rather get_pages(). class powers get_pages(), provides additional control. in instance, we're interested in fact overwrites global $post variable, allows the_excerpt() , get_the_excerpt() work correctly. here's how can create secondary loop fetch correct child pages:

$child_args = array(      'post_parent' => $post->id,     // assumes you're calling on parent music page     'sort_column' => 'post_date',     'sort_order' => 'desc',     'ignore_sticky_posts' => true,  // don't display sticky posts     'post_type' => 'page'           // fetch pages, not posts );  $child_query = new wp_query( $child_args );  // usual loop logic if ( $child_query->have_posts() ) : while ( $child_query->have_posts() ) : $child_query->the_post();      // can pretty whatever want in here...     the_title();    // should output child page title     the_excerpt();  // should output child page excerpt     $excerpt = get_the_excerpt(); // assigns excerpt variable in case want other things it.       endwhile; else:       // 'no posts' content go here  endif;  wp_reset_postdata(); // important; sets $post parent page. reset after loop. 

you'd want put in page-music.php, personal preference put in template part , call using get_template_part(), allow re-use on other pages. example, should able use featured design page without having change thing.

also, note use of wp_reset_postdata(): need @ end of loop, reset $post original page. want on front-page.php, after loop music pages (round line 81).

for more info on wp_query can check out this answer

filtering

now, if use secondary loops described above, can rely on $post being present. lets apply conditional filtering on excerpt, based on tags, parent page etc. save having create different output functions based on page parent -- you're doing -- can instead apply filter function, adding functions.php:

/**  * custom function when need filter excerpt differently particular pages  * needs used within loop  * @args $excerpt(string)       - incoming excerpt filter  * @args $post(obj)             - post excerpt comes (wp 4.5+)  * @returns $excerpt(string)    - filtered excerpt  */ if ( !function_exists('custom_excerpt_filter')) {     function custom_excerpt_filter($excerpt='', $post=null) {          // fallback wp 4.4 , below, don't pass $post         if (!$post) {             global $post;         }          // unfiltered excerpt if want it:         // if ( !empty($post->post_excerpt) ) {         //  $excerpt = $post->post_excerpt; // use excerpt if there one.         // } else {         //  $excerpt = get_extended($post->post_content)['main']; // use content before         //}           // can whatever string manipulations want $excerpt here          // filtering based on tags...         if ( has_tag('featured-music',$post) ) {             // apply additional filtering music page excerpts here         }          // or filtering based on parent page...         $parent = get_page_by_path('featured-music'); // parent post via slug         if ( $post->post_parent == $parent->id ) {             // additional filtering music child pages here...         }          return apply_filters('the_excerpt', $excerpt, $post); // apply other filters can chained     } } add_filter('get_the_excerpt','custom_excerpt_filter', 10, 2); 

i've left out actual string manipulations on $excerpt can pretty it.

now when use the_excerpt() within loop, filter can take account page tag/parent of excerpt in question , modify how ever want. way logic kept in 1 place, , applied consistently across templates.

inserting media excerpt

since want insert video , audio excerpt, you'll hit snag when using excerpt field in admin: won't let insert html tags. not worry, can still insert in content, , extract via our filter. we'll can use get_media_embedded_in_content() extract page content, , insert excerpt:

/**  * custom function when need filter excerpt differently particular pages  * needs used within loop  * @args $excerpt(string)       - incoming excerpt filter  * @args $post(obj)             - post excerpt comes from.  * @returns $excerpt(string)    - filtered excerpt  */ if ( !function_exists('custom_excerpt_filter')) {     function custom_excerpt_filter($excerpt='', $post=null) {           // fallback wp 4.4 , below, didn't pass $post         // assumes you're calling in loop         if (!$post) {             global $post;         }          // unfiltered excerpt if want it:         // if ( !empty($post->post_excerpt) ) {         //  $excerpt = $post->post_excerpt; // use excerpt if there one.         // } else {         //  $excerpt = get_extended($post->post_content)['main']; // use content before         //}           // can whatever string manipulations want $excerpt here          // filtering based on tags...         if ( has_tag('featured-music',$post) ) {             // apply additional filtering music page excerpts here             $excerpt .= ' has featured music tag';         }          // filtering based on parent...         $parent = get_page_by_path('featured-music'); // parent post via slug          if ( $post->post_parent == $parent->id ) {              $excerpt = ''; // ignore current excerpt we're making our own              // first grab content             $content = apply_filters( 'the_content',  $post->post_content); // apply content filters shortcodes applied              // audio             $audio = get_media_embedded_in_content($content, array('audio')); // returns array             if (!empty($audio)) {                  $audio = $audio[0]; // since want first audio embed, grab first element in array                 $excerpt .= $audio; // add audio excerpt             }               // video             $video = get_media_embedded_in_content($content, array('video', 'iframe'));             if (!empty($video)) {                 $video = $video[0];                 $excerpt .= $video; // add video excerpt             }              // first paragraph             // there's bunch of ways can this; need bear in mind there's media in content you'll need work around             // here's 1 possible way:              // first, strip out media...             $content = preg_replace("/<img[^>]+\>/i", "", $content);             $content = preg_replace("/<iframe[^>]+\>/i", "", $content);             $content = preg_replace("/<audio[^>]+\>/i", "", $content);              // ...here we're relying on fact wp doggedly wraps in <p> tags              // explode content array             $content = explode('</p>', $content);              $excerpt .= $content[0];         }          return apply_filters('the_excerpt', $excerpt, $post); // apply other filters can chained     } } add_filter('get_the_excerpt','custom_excerpt_filter', 10, 2); 

getting first paragraph

the last thing want wrap grab first paragraph. there's bunch of different ways this; above function includes 1 method. also:

  1. grabbing content before more tag using get_extended() , strip out media using wp_strip_all_tags().
  2. use separate excerpt field.

conclusion

by using wp_query , bit of logic inside filtering function, can simplify lot of templates, in turn should in turn working correctly. helps!


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -