WORDPRESS 用 space 替换 $post->post_title 的连字符
WORDPRESS replace hyphens with space for $post->post_title
我在 functions.php 中使用此代码将“替代”文本设置为等于“标题”,因为缺少替代文本。
“标题”等于包含连字符的图像文件名。
在我尝试使用 str_replace('-', ' ', $string);
删除“title”和随后的“alt”中的连字符之前一直有效。
我是一个彻头彻尾的编码新手,这就是为什么我的尝试不仅没有奏效,而且可能还有很长的路要走。
这有可能实现吗?
add_filter('wp_get_attachment_image_attributes', 'change_attachement_image_attributes', 20, 2);
function change_attachement_image_attributes($attr, $attachment) {
global $post;
$product = wc_get_product( $post->ID );
if ($post->post_type == 'product') {
$title = $post->post_title;
$replace = str_replace("-", " ", $title);
$attr['alt'] = $attr['replace'];
}
return $attr;
}
你快成功了!
您的代码中有一个小错别字:
$attr['alt'] = $attr['replace'];
它应该在哪里:
$attr['alt'] = $replace;
这是因为 $attr['replace'] 不存在,而 $replace 是包含您要分配给 $attr['alt']
的更改
add_filter('wp_get_attachment_image_attributes', 'change_attachement_image_attributes', 20, 2);
function change_attachement_image_attributes($attr, $attachment) {
global $post;
$product = wc_get_product( $post->ID );
if ($post->post_type == 'product') {
$title = $post->post_title;
$replace = str_replace("-", " ", $title);
$attr['alt'] = $replace;
}
return $attr;
}
我在 functions.php 中使用此代码将“替代”文本设置为等于“标题”,因为缺少替代文本。
“标题”等于包含连字符的图像文件名。
在我尝试使用 str_replace('-', ' ', $string);
删除“title”和随后的“alt”中的连字符之前一直有效。
我是一个彻头彻尾的编码新手,这就是为什么我的尝试不仅没有奏效,而且可能还有很长的路要走。
这有可能实现吗?
add_filter('wp_get_attachment_image_attributes', 'change_attachement_image_attributes', 20, 2);
function change_attachement_image_attributes($attr, $attachment) {
global $post;
$product = wc_get_product( $post->ID );
if ($post->post_type == 'product') {
$title = $post->post_title;
$replace = str_replace("-", " ", $title);
$attr['alt'] = $attr['replace'];
}
return $attr;
}
你快成功了!
您的代码中有一个小错别字:
$attr['alt'] = $attr['replace'];
它应该在哪里:
$attr['alt'] = $replace;
这是因为 $attr['replace'] 不存在,而 $replace 是包含您要分配给 $attr['alt']
的更改add_filter('wp_get_attachment_image_attributes', 'change_attachement_image_attributes', 20, 2);
function change_attachement_image_attributes($attr, $attachment) {
global $post;
$product = wc_get_product( $post->ID );
if ($post->post_type == 'product') {
$title = $post->post_title;
$replace = str_replace("-", " ", $title);
$attr['alt'] = $replace;
}
return $attr;
}