Php Wordpress功能:Can';t向返回值添加类

Php Wordpress功能:Can';t向返回值添加类,php,wordpress,function,Php,Wordpress,Function,我正在使用一个函数返回“product”类型的特定文章的内容。但是,我想检查内容的长度,如果它小于某个长度,我想添加一个类以使内容居中(默认情况下,我将其左对齐)。我了解如何编写IF语句来检查长度(因此我将其排除在示例之外)。但是我在课堂上返回内容时遇到了一个问题。下面是函数 function post_product_description() { $product_title = get_the_title(); $the_query = new WP_Query( array(

我正在使用一个函数返回“product”类型的特定文章的内容。但是,我想检查内容的长度,如果它小于某个长度,我想添加一个类以使内容居中(默认情况下,我将其左对齐)。我了解如何编写IF语句来检查长度(因此我将其排除在示例之外)。但是我在课堂上返回内容时遇到了一个问题。下面是函数

function post_product_description() {
   $product_title = get_the_title();
   $the_query = new WP_Query( array(
    'post_type' => 'product',
    'name' => $product_title
   ) ); 
   while ( $the_query->have_posts() ) : $the_query->the_post();
     $content = get_the_content();
     $content = apply_filters( 'the_content', $content );
     //$content = '<span class="centered">'.$content.'</span>';
   endwhile;
   wp_reset_postdata();
   return $content;
}
add_shortcode('product_description', 'post_product_description');
但是,如果内容的长度超过某个数字,我希望
标记围绕文本输出,如下所示:

 <p>the content text here</p>
<p><span class="centered"></span></p>
<p>the content outputs here</p>
<p></p>
此处的内容文本

问题是,如果我尝试将
标记附加到$content变量,就像我在原始代码的注释行中所做的那样,wordpress输出如下:

 <p>the content text here</p>
<p><span class="centered"></span></p>
<p>the content outputs here</p>
<p></p>

内容输出在这里


我如何返回$content而不添加所有额外的
标记,并在内容周围包装
标记?

也许您正在寻找类似的内容:

 <p>the content text here</p>
<p><span class="centered"></span></p>
<p>the content outputs here</p>
<p></p>
functions.php

add_filter('the_content','my_the_content_filter',20);
函数“我的内容”过滤器($content){
如果(is_single()):
$length=strlen($content);
如果($length>500)//
{
$content=sprintf(“.$content.”);
}
endif;
//返回内容。
返回$content;
}
此代码将输出包装在span class=“custom class”中的内容。您可以在Wordpress codex上查看更多信息:


也许您正在寻找这样的产品:

 <p>the content text here</p>
<p><span class="centered"></span></p>
<p>the content outputs here</p>
<p></p>
functions.php

add_filter('the_content','my_the_content_filter',20);
函数“我的内容”过滤器($content){
如果(is_single()):
$length=strlen($content);
如果($length>500)//
{
$content=sprintf(“.$content.”);
}
endif;
//返回内容。
返回$content;
}
此代码将输出包装在span class=“custom class”中的内容。您可以在Wordpress codex上查看更多信息:


这就是你要找的吗?我不这么认为。我试图在$content返回的文本周围添加标记。但是,如果我这样做,会添加额外的
标记集,并且跨度不会环绕内容。这就是您要找的吗?我不这么认为。我试图在$content返回的文本周围添加标记。但是,如果我这样做,会添加额外的
标记集,并且跨度不会环绕内容。我尝试了,但仍然无法使其工作。但是,我做了以下工作:
$content=''.get_the_content()'我不知道为什么它突然开始工作,因为它似乎和我在原来的帖子中做的是一样的。我试过了,但还是没能让它工作。但是,我做了以下工作:
$content=''.get_the_content()'