marya marya - 2 months ago 14
PHP Question

Excluding specific multiple tags in the tag list in WordPress

I want to exclude 2 tags from the tag list in the following script:

$tags = get_tags();
$html = '<ul>';
foreach ( $tags as $tag ) {
if($tag->slug != "test1"){
$tag_link = get_tag_link( $tag->term_id );
$html .= "<li><a href='{$tag_link}' class='{$tag->slug}'>";
$html .= "{$tag->name}</a></li>";
}}
$html .= '</ul>';
echo $html;


The script works correctly excluding
test1
tag.
How do I edit the code in order to also exclude another tag named
test2
?

Answer

As per DOCS:

'exclude' Default is an empty string. A comma- or space-delimited string of term ids to exclude from the return array. If 'include' is non-empty, 'exclude' is ignored.

You need to change 1,2 with tag ids of test1 & test2.

$args = array('exclude' => '1,2' );
$tags = get_tags($args);
// .... Rest of your code goes here

Don't forget to remove if check if($tag->slug != "test1"){ as it would be useless with exclude in $args :)