在“高级自定义字段”中显示第一段 [英] Display 1st paragraph within Advanced Custom Fields

查看:61
本文介绍了在“高级自定义字段”中显示第一段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何只显示高级自定义字段的第一段。

How do you display only the first paragraph of an advanced custom field.

  <?php the_field('lyrics'); ?>

以上是我用来显示全文的内容。

Above is what i use to display the full text.

推荐答案

add_filter( 'wp_trim_excerpt', 'my_custom_excerpt', 10, 2 );
function my_custom_excerpt($text, $raw_excerpt) {
    if( ! $raw_excerpt ) {
        $content = apply_filters( 'the_content', get_the_content() );
        $text = substr( $content, 0, strpos( $content, '</p>' ) + 4 );
    }
    return $text;
}

try this code will help you to show first 55 character of your first paragraph.

Grab the first paragraph of each post

第二个选项:

function custom_field_excerpt() {
global $post;
$text = get_field('news');
if ( '' != $text ) {
    $start = strpos($text, '<p>'); // Locate the first paragraph tag
    $end = strpos($text, '</p>', $start); // Locate the first paragraph closing tag
    $text = substr($text, $start, $end-$start+4); // Trim off everything after the closing paragraph tag
    $text = strip_shortcodes( $text );
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
}
return $text;}

第三种选择:
您可以使用以下函数:

third option : You can use this function:

function get_first_paragraph(){
global $post;
$str = wpautop( get_the_content() );
$str = substr( $str, 0, strpos( $str, '</p>' ) + 4 );
$str = strip_tags($str, '<a><strong><em>');
return '<p>' . $str . '</p>';}

,然后在循环中使用:

<?php echo get_first_paragraph(); ?>

这篇关于在“高级自定义字段”中显示第一段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆