php - Show excerpt on front page using the_content(); and ACF -


i need show excerpt on home page. have standard post , custom post type 'webinar'. cpt 'webinar' has custom field, using acf 'webinar_description' equivalent of 'description' in normal posts.

i able display both, adding filter 'webinar_description' this:

add_filter('the_content','add_my_custom_field'); function add_my_custom_field($data) {     $data .= get_field('webinar_description');     return $data; } 

now have both post types displaying, displays entire 'description' or 'webinar_description' field. need trim @ 40 words , add '... read more' 'read more' being link article.

i have tried this, works on normal 'post' type field 'description' doesn't work on 'webinar' custom post type -> custom field 'webinar-description'

<?php $content = get_the_content(); echo mb_strimwidth($content, 0, 400, '<a href="' . get_permalink() . '">[read more]</a>');?> 

how can create filter or function limit both 400 (or whatever) characters , add link?

not sure if in similar situation, how solved it. first, in functions.php make custom post type available

function cpt_get_excerpt(){     $excerpt = get_field('webinar_description');     $excerpt = preg_replace(" (\[.*?\])",'',$excerpt);     $excerpt = strip_shortcodes($excerpt);     $excerpt = strip_tags($excerpt);     $excerpt = substr($excerpt, 0, 400);     $excerpt = substr($excerpt, 0, strripos($excerpt, " "));     $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));     $excerpt = $excerpt.'... <a class="c-drkgold" href="'.get_permalink($post->id).'">read more</a>';     return $excerpt; } 

then want display it, use if/else statement based on post type , display accordingly (this example static front page).

<?php      if( get_post_type() == 'post' ) {         ?><p class="l-nmb"><?php         $content = get_the_content(); echo mb_strimwidth($content, 0, 400, '... <a href="' . get_permalink() . '" class="c-drkgold">read more</a>'); ?> </p>     <?php } else {         ?><p class="l-nmb"><?php         $content = cpt_get_excerpt(); echo mb_strimwidth($content, 0, 400, '... <a href="' . get_permalink() . '" class="c-drkgold">read more</a>'); ?> </p>     <?php } ?> 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -