Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Woocommerce产品标题中添加换行符_Php_Wordpress_Woocommerce_Title_Product - Fatal编程技术网

Php 在Woocommerce产品标题中添加换行符

Php 在Woocommerce产品标题中添加换行符,php,wordpress,woocommerce,title,product,Php,Wordpress,Woocommerce,Title,Product,假设我的产品名称是: 棕色皮鞋 但根据我的列宽,标题如下所示: 棕色皮革 鞋 我知道可以进行字符替换,以便后端中的“|”变为换行符。但我不知道怎么做 我希望它看起来像这样 棕色 皮鞋 我发现这些参考资料: 是否可以在WC产品的长标题中添加换行符?2020修订版-仍适用于WoodCommerce 4.4.1版本的Wordpress 5.5.1 使用链接在过滤器钩子中的此自定义函数将完成此任务(将管道字符替换为产品标题中的): add_filter('the_title','custom_p

假设我的产品名称是:

棕色皮鞋

但根据我的列宽,标题如下所示:

棕色皮革

我知道可以进行字符替换,以便后端中的
“|”
变为换行符

。但我不知道怎么做

我希望它看起来像这样

棕色
皮鞋

我发现这些参考资料:


是否可以在WC产品的长标题中添加换行符?

2020修订版-仍适用于WoodCommerce 4.4.1版本的Wordpress 5.5.1

使用链接在
过滤器钩子中的此自定义函数将完成此任务(将管道字符
替换为产品标题中的

):

add_filter('the_title','custom_product_title',10,2);
功能自定义\u产品\u标题($title,$post\u id){
$post\u type=get\u post\u字段('post\u type',$post\u id,true);
if(在数组中($post\u类型,数组('product','product\u变体')){
$title=str|u replace('|','
',$title);//我们将“|”替换为“
” } 返回$title; }
代码位于活动子主题(或主题)的function.php文件或任何插件文件中

代码在Woocommerce 3+上进行了测试,可以正常工作

现在,您可以使用WC条件标签将不同的产品页面作为目标,如
is_product()
is_shop()
is_product_category()
is_product_tag()
(以及许多其他内容)


2020修订版-仍适用于WoodPress 5.5.1和WooCommerce 4.4.1

使用链接在
过滤器钩子中的此自定义函数将完成此任务(将管道字符
替换为产品标题中的

):

add_filter('the_title','custom_product_title',10,2);
功能自定义\u产品\u标题($title,$post\u id){
$post\u type=get\u post\u字段('post\u type',$post\u id,true);
if(在数组中($post\u类型,数组('product','product\u变体')){
$title=str|u replace('|','
',$title);//我们将“|”替换为“
” } 返回$title; }
代码位于活动子主题(或主题)的function.php文件或任何插件文件中

代码在Woocommerce 3+上进行了测试,可以正常工作

现在,您可以使用WC条件标签将不同的产品页面作为目标,如
is_product()
is_shop()
is_product_category()
is_product_tag()
(以及许多其他内容)


截至2019年11月,Wordpress 5.2.4的完整答案如下:

//ADD LINE BREAK 
add_filter( 'the_title', 'custom_the_title', 10, 2 );
function custom_the_title( $title, $post_id ) {
    $post_type = get_post_field( 'post_type', $post_id, true );
    if( $post_type == 'product' || $post_type == 'product_variation' )
        $title = str_replace( '|', '<br/>', $title ); // we replace '|' by '<br>'
    return $title;
}
//添加换行符
添加过滤器(“标题”、“自定义标题”10,2);
函数自定义\u标题($title,$post\u id){
$post\u type=get\u post\u字段('post\u type',$post\u id,true);
如果($post_类型=='product'| |$post_类型=='product_VARIANCE')
$title=str|u replace('|','
',$title);//我们用'
'替换'|' 返回$title; }

LoicTheAztec和Coes的学分

截至2019年11月,Wordpress 5.2.4的完整答案:

//ADD LINE BREAK 
add_filter( 'the_title', 'custom_the_title', 10, 2 );
function custom_the_title( $title, $post_id ) {
    $post_type = get_post_field( 'post_type', $post_id, true );
    if( $post_type == 'product' || $post_type == 'product_variation' )
        $title = str_replace( '|', '<br/>', $title ); // we replace '|' by '<br>'
    return $title;
}
//添加换行符
添加过滤器(“标题”、“自定义标题”10,2);
函数自定义\u标题($title,$post\u id){
$post\u type=get\u post\u字段('post\u type',$post\u id,true);
如果($post_类型=='product'| |$post_类型=='product_VARIANCE')
$title=str|u replace('|','
',$title);//我们用'
'替换'|' 返回$title; }

LoicTheAztec和Coes的信用

代码(即使是2020年版本)在产品页面中工作不正常,显示br标签而不是换行符。

代码(即使是2020年版本)在产品页面中工作不正常,显示br标签而不是换行符。

否。我一定是做错了什么:(添加到我的孩子主题中顺便说一句):(是的,我看到它对你有用。你看到我的截图有什么问题吗?谢谢你花时间回答。好的,我做了一些事情!分析错误:语法错误,意外的“”
“”(T_常量\u封装的\u字符串),预期为“”,或“”)'在/home/../../functions.php第450行,我知道了!问题是代码中有一个字符。正确答案是
//在标题($title,$post\u id){$post\u type=get\u post\u字段('post\u type',$post\u id,true);if($post\u type=='product'.$post\u type=='product\u variation'))$title=str|replace('|','
',$title);//我们用'
':return$title;}
谢谢谢谢!!当我复制你的代码时,它创建了一个OBJ字符。这里没有。我一定是做错了什么:(添加到我的子主题中顺便说一下):(是的,我看到它对你有用。你看到我的截图有什么问题吗?谢谢你花时间回答。好的,我做了一些事情!解析错误:语法错误,意外的“”
“”(T_常量\u包装的字符串),预期为“”,或“”)'在/home/../../functions.php第450行,我知道了!问题是代码中有一个字符。正确答案是
//在标题($title,$post\u id){$post\u type=get\u post\u字段('post\u type',$post\u id,true);if($post\u type=='product'.$post\u type=='product\u variation'))$title=str|replace('|','
',$title);//我们用'
':return$title;}
谢谢谢谢!!当我复制你的代码时,它创建了一个OBJ字符。这里