【问题标题】:Modify function to include paragraphs修改函数以包含段落
【发布时间】:2014-07-19 18:18:18
【问题描述】:

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

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

这是我的功能

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');

编辑

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

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

【问题讨论】:

  • 如果有X个标签,你想在第X个之后剪切吧?
  • 不行,我需要在第三个&lt;/p&gt;标签之后剪切

标签: php wordpress


【解决方案1】:
$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>

【讨论】:

  • 谢谢,今天晚些时候试试
  • 谢谢。有效。不过稍微调整了一下。将发布
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-26
  • 1970-01-01
  • 2013-05-12
相关资源
最近更新 更多