在高级自定义字段中显示第1段

时间:2016-10-06 02:34:22

标签: php wordpress advanced-custom-fields

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

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

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

1 个答案:

答案 0 :(得分:1)

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

第三种选择: 您可以使用此功能:

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(); ?>