使用the_content()在首页显示摘录;和ACF

使用the_content()在首页显示摘录;和ACF

问题描述:

我需要在我的主页上显示摘录。我有标准的帖子和一个自定义的帖子类型'网络研讨会'。 CPT的'网络研讨会'有一个自定义字段,使用ACF的'webinar_description',相当于普通帖子中的'description'。使用the_content()在首页显示摘录;和ACF

我能够通过添加一个过滤器的这样的“webinar_description”同时显示,:

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

现在都在我文章类型显示,但它显示了整个“说明”或“webinar_description”领域。我需要用40字来修剪它,然后添加一个'...阅读更多内容',其中'阅读更多'是本文的链接。

我已经试过了,但它仅适用于正常“后”类型字段“描述”它不会对我的“研讨会”自定义后类型的工作 - >自定义字段“研讨会,说明”

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

如何创建将限制为400(或其他)字符并添加链接的过滤器或函数?

不知道这是否会帮助任何人在类似的情况,但这是我如何解决它。首先,在functions.php中进行自定义后类型的可用

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; 
} 

然后要显示它,使用如果立足岗位类型,并相应显示/ else语句(本例中是从静态的头版) 。

<?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 } ?>