英文:
Removing Commas between Hashtags on WordPress on Astra theme
问题
我已尝试了所有可能的方法来删除我的WordPress网站上标签之间的逗号,但我没有任何好运。
URL: https://www.content-whale.com/blog/
PHP
add_filter('the_tags', 'remove_tag_commas', 10, 2);
function remove_tag_commas($tags, $post_id) {
$tags_string = implode(', ', $tags); // 将标签数组转换为逗号分隔的字符串
$tags_string = str_replace(', ,', ',', $tags_string); // 删除连续的逗号
// 将修改后的字符串重新转换为标签数组
$modified_tags = explode(', ', $tags_string);
return $modified_tags;
}
或者
Javascript
const container = document.querySelector('.premium-blog-post-tags-container');
const tags = Array.from(container.querySelectorAll('.premium-blog-post-tags-container a[rel]'));
tags.forEach((tag, index) => {
tag.textContent = tag.textContent.replace(/,\s*/g, '');
if (index !== tags.length - 1) {
const comma = tag.nextSibling;
if (comma && comma.nodeType === Node.TEXT_NODE && comma.textContent.trim() === ',') {
comma.remove();
}
}
});
英文:
I have tried all possible ways to remove the (commas ,) between the tags on my WordPress website but I haven't had any luck.
URL: https://www.content-whale.com/blog/
PHP
add_filter('the_tags', 'remove_tag_commas', 10, 2);
function remove_tag_commas($tags, $post_id) {
$tags_string = implode(', ', $tags); // Convert tags array to a comma-separated string
$tags_string = str_replace(', ,', ',', $tags_string); // Remove consecutive commas
// Convert the modified string back to an array of tags
$modified_tags = explode(', ', $tags_string);
return $modified_tags;
}
OR
Javascript
const container = document.querySelector('.premium-blog-post-tags-container');
const tags = Array.from(container.querySelectorAll('.premium-blog-post-tags-container a[rel]'));
tags.forEach((tag, index) => {
tag.textContent = tag.textContent.replace(/,\s*/g, '');
if (index !== tags.length - 1) {
const comma = tag.nextSibling;
if (comma && comma.nodeType === Node.TEXT_NODE && comma.textContent.trim() === ',') {
comma.remove();
}
}
});
答案1
得分: 0
以下是您要翻译的内容:
"Since you don't seem to have control over template files, but you did try to use Javscript, you could add the following Javascript. It checks the content of the .premium-blog-post-tags-container
div
s whether they are text nodes and not links. Then removes those text nodes, so you will end up with only the links and not the text nodes."
"由于您似乎无法控制模板文件,但尝试使用JavaScript,您可以添加以下JavaScript。它检查.premium-blog-post-tags-container
的div
的内容,看它们是否是文本节点而不是链接。然后删除这些文本节点,因此您最终将只保留链接而不保留文本节点。"
英文:
Since you don't seem to have control over template files, but you did try to use Javscript, you could add the following Javascript. It checks the content of the .premium-blog-post-tags-container
div
s whether they are text nodes and not links. Then removes those text nodes, so you will end up with only the links and not the text nodes.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let categoryLinks = document.getElementsByClassName('premium-blog-post-tags-container');
for(let i = 0; i < categoryLinks.length; i++){
let childNodes = categoryLinks[i].childNodes;
for(let c = 0; c < childNodes.length; c++){
if(childNodes[c].nodeName === "#text"){
categoryLinks[i].removeChild(childNodes[c]);
}
}
}
<!-- language: lang-html -->
<div class="premium-blog-post-tags-container">
<i class="fa fa-tags fa-fw" aria-hidden="true"></i>
<a>article writing services</a>, <a>article</a>, , , , , , , , , ,
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论