【问题标题】:Display 1st paragraph within Advanced Custom Fields在高级自定义字段中显示第一段
【发布时间】:2016-10-06 02:34:21
【问题描述】:

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

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

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

【问题讨论】:

  • 你在歌词字段中输入的内容。
  • 我正在使用所见即所得的高级自定义字段。每个歌词字段有 1 - 10 个段落。

标签: php wordpress advanced-custom-fields


【解决方案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(); ?>

【讨论】:

  • 对于选项二,我创建函数后如何在帖子中显示它?
  • 你得到 $text 作为返回值使用它
猜你喜欢
  • 2018-08-26
  • 2019-08-20
  • 1970-01-01
  • 2018-04-15
  • 2014-03-23
  • 2015-07-07
  • 2018-09-17
  • 2021-06-07
  • 2019-01-18
相关资源
最近更新 更多