修改函数以包含段落 [英] Modify function to include paragraphs

查看:27
本文介绍了修改函数以包含段落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个摘录功能,可以剪切第一段之后的内容(在第一个 </p> 标签之后.

I have this excerpt function that cuts the content after the first paragraph (after the first </p> tag.

我需要修改它,以便我可以剪切第二或第三段之后的内容,即第二或第三个 </p> 标签.我对 php 的了解不是很多,所以任何帮助将不胜感激.

I need to modify this so I can cut the content after the second or third paragraph, that is the second or third </p> tag. My knowledge of php is not great, so any help will be appreciated.

这是我的功能

if ( ! function_exists( 'wpse0001_custom_wp_trim_excerpt' ) ) : 

function wpse0001_custom_wp_trim_excerpt($wpse0001_excerpt) {
global $post;
$raw_excerpt = $wpse0001_excerpt;
if ( '' == $wpse0001_excerpt ) {

$wpse0001_excerpt = get_the_content('');
$wpse0001_excerpt = strip_shortcodes( $wpse0001_excerpt );
$wpse0001_excerpt = apply_filters('the_content', $wpse0001_excerpt);
$wpse0001_excerpt = substr( $wpse0001_excerpt, 0, strpos( $wpse0001_excerpt, '</p>' ) + 4 );
$wpse0001_excerpt = str_replace(']]>', ']]&gt;', $wpse0001_excerpt);

$excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . '&nbsp;&raquo;&nbsp;' . sprintf(__( 'Read more about: %s &nbsp;&raquo;', 'pietergoosen' ), get_the_title()) . '</a>'; 
$excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end); 

//$pos = strrpos($wpse0001_excerpt, '</');
//if ($pos !== false)
// Inside last HTML tag
//$wpse0001_excerpt = substr_replace($wpse0001_excerpt, $excerpt_end, $pos, 0);
//else
// After the content
$wpse0001_excerpt .= $excerpt_end;

return $wpse0001_excerpt;

}
return apply_filters('wpse0001_custom_wp_trim_excerpt', $wpse0001_excerpt, $raw_excerpt);
}

endif; 

remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wpse0001_custom_wp_trim_excerpt');

编辑

这是我需要修改的代码部分,但不确定如何

This is the section of code I need to modify, but not sure how

$wpse0001_excerpt = substr( $wpse0001_excerpt, 0, strpos( $wpse0001_excerpt, '</p>' ) + 4 );

推荐答案

$wpse0001_excerpt = "<p>1</p><p>2</p><p>3</p><p>4</p><p>5</p>";

$wanted_number_of_paragraph = 3;

$tmp = explode ('</p>', $wpse0001_excerpt);
for ($i = 0; $i < $wanted_number_of_paragraph; ++$i) {
   if (isset($tmp[$i]) && $tmp[$i] != '') {
       $tmp_to_add[$i] = $tmp[$i];
   }
}
$wpse0001_excerpt = implode('</p>', $tmp_to_add) . '</p>';

echo $wpse0001_excerpt;
// Output : <p>1</p><p>2</p><p>3</p>

这篇关于修改函数以包含段落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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