在WordPress的Astra主题中去除标签之间的逗号。

huangapple go评论109阅读模式
英文:

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/

Screenshot attached

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 divs 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-containerdiv的内容,看它们是否是文本节点而不是链接。然后删除这些文本节点,因此您最终将只保留链接而不保留文本节点。"

英文:

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 divs 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(&#39;premium-blog-post-tags-container&#39;);
for(let i = 0; i &lt; categoryLinks.length; i++){
	let childNodes = categoryLinks[i].childNodes;
	for(let c = 0; c &lt; childNodes.length; c++){
		if(childNodes[c].nodeName === &quot;#text&quot;){
			categoryLinks[i].removeChild(childNodes[c]);
		}
	}
}

<!-- language: lang-html -->

&lt;div class=&quot;premium-blog-post-tags-container&quot;&gt;
	&lt;i class=&quot;fa fa-tags fa-fw&quot; aria-hidden=&quot;true&quot;&gt;&lt;/i&gt;
	&lt;a&gt;article writing services&lt;/a&gt;, &lt;a&gt;article&lt;/a&gt;, , , , , , , , , , 						
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月3日 23:23:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76606142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定