如何使用functions.php修剪WP类别标题

如何使用functions.php修剪WP类别标题,php,wordpress,Php,Wordpress,我对WP函数相当陌生,所以希望有人能对这个问题有所了解。我有一个功能,可以显示特定类别的五篇文章,然后使用一个短代码在某些WP文章/页面上显示结果。我很难包含wp\u trim\u单词来获取标题()。我想把标题限制在5个字以内,并以“…”结尾。我在这里看到了很多例子,但没有一个适合我的函数。有人能帮忙吗 函数wpb_postsbycategory(){ //询问 $the_query=new WP_query(数组('category_name'=>'3dgames','posts_per_p

我对WP函数相当陌生,所以希望有人能对这个问题有所了解。我有一个功能,可以显示特定类别的五篇文章,然后使用一个短代码在某些WP文章/页面上显示结果。我很难包含wp\u trim\u单词来获取标题()。我想把标题限制在5个字以内,并以“…”结尾。我在这里看到了很多例子,但没有一个适合我的函数。有人能帮忙吗

函数wpb_postsbycategory(){
//询问
$the_query=new WP_query(数组('category_name'=>'3dgames','posts_per_page'=>5));
//环路
if($the\u query->have\u posts()){
$string.='
    ”; while($the\u query->have\u posts()){ $the_query->the_post(); 如果(具有\u post\u缩略图()){ $string.='

    '; }否则{ //如果没有找到特征图像 $string.='
  • '; } } }否则{ //没有找到帖子 } $string.='
'; 返回$string; /*恢复原始Post数据*/ wp_reset_postdata(); } //添加一个短代码 添加_快捷码('3dgames','wpb_postsbycategory'); //在文本小部件中启用短代码
添加_过滤器('widget_text','do_shortcode')你应该使用wp\u trim\u words()并将文章标题放在里面:
wp\u trim\u单词(获取标题(),5,“…”)

在您的情况下,应该是这样的:

if ( has_post_thumbnail() ) {
    $string .= '<p><a href="' . get_the_permalink() .'" style="color:#FFF;" rel="bookmark">' . get_the_post_thumbnail($post_id, array( 100, 50) ) . (wp_trim_words(get_the_title(), 5, '...')) .'</a><br></p>';
} else { 
    // if no featured image is found
    $string .= '<li><a href="' . get_the_permalink() .'" style="color:#FFF;" rel="bookmark">' . (wp_trim_words(get_the_title(), 5, '...')) .'</a></li>';
}
if(具有\u post\u缩略图()){
$string.='

'; }否则{ //如果没有找到特征图像 $string.='
  • '; }
    有关
    wp\u trim\u words()
    工作原理的更多详细信息,请访问官方文档: