doubianxian6557 2016-08-17 04:12
浏览 25

Wordpress自定义摘录添加类

I use this custom excerpt from another question that lets you control the amount of paragraphs you wanted to display instead of the length of characters. It also includes "Read More" link. Upon inspecting the code, I found out that this link is surrounded by the <p> tag. However, I wanted to add a class to this particular <p> tag surrounding the "Read More" link.

Here's the code in question.

if ( ! function_exists( 'custom_wp_trim_excerpt' ) ) : 

function custom_wp_trim_excerpt($blogo_excerpt) {
global $post;
$raw_excerpt = $blogo_excerpt;
if ( '' == $blogo_excerpt ) {

$blogo_excerpt = get_the_content('');
$blogo_excerpt = strip_shortcodes( $blogo_excerpt );
$blogo_excerpt = apply_filters('the_content', $blogo_excerpt);
// Here we choose how many paragraphs do we want to cutthe excerpt at, This part thanks to Clément Malet
$blogo_excerpt = "<p>$blogo_excerpt</p>";
    $wanted_number_of_paragraph = 1;
    $tmp = explode ('</p>', $blogo_excerpt);
    for ($i = 0; $i < $wanted_number_of_paragraph; ++$i) {
       if (isset($tmp[$i]) && $tmp[$i] != '') {
           $tmp_to_add[$i] = $tmp[$i];
       }
    }
$blogo_excerpt = implode('</p>', $tmp_to_add) . '</p>';

$blogo_excerpt = str_replace(']]>', ']]&gt;', $blogo_excerpt);

$excerpt_end = '<a href="'. esc_url( get_permalink() ) . '">Read More</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
$blogo_excerpt .= $excerpt_end;

return $blogo_excerpt;

}
return apply_filters('custom_wp_trim_excerpt', $blogo_excerpt, $raw_excerpt);
}

endif; 

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

Additionally, I'm trying to understand some lines in this code. For example, I learned that "explode" break up the string into array. Thus, in this code, "explode" break up the whole content into each paragraph and put each paragraph in an array. Also, "implode" returns the array back into string. In other words, "implode" put each paragraph all back together to make up whole content.

Lastly, I think I might know where the code should be edited to add the class to the <p> tag that surrounds "Read More" link

// After the content
$blogo_excerpt .= $excerpt_end;

because "$excerpt_end" is the "Read more" link and ".=" appends the "Read more" link to the post excerpt.

However, after doing some trial and errors, I am still unable to figure out how to add a class to the <p> tag that surrounds the "Read more" link.

Please help!

  • 写回答

1条回答 默认 最新

  • duanli9001 2016-08-17 04:27
    关注

    If the reason you want to add a class to first p tag is css reasons, you could select the first p in div with first-of-type selector.

    Example:

    .div-around-p-tags-class p:first-of-type {
      font-size: 1.5em;
    }
    

    This will select the first p tag inside a div with that specific class "div-around-p-tags-class" , but you could set to your class around the p tags.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向