WordPress.org

Support

Support » Plugins and Hacks » Hacks » [Resolved] PHP: Exclude Tag

[Resolved] PHP: Exclude Tag

  • I am using the following code to generate a list of most used tags:

    [moderator remark: the code below is partially broken; don’t use for copy/paste]

    <div id="tab3" class="tab_content">
    					<p class='tag_cloud'>
    					<?php
    					$tags = get_tags(array('orderby' => 'count', 'order' => 'DESC', 'number' => '28'));
    					foreach ((array) $tags as $tag) {
    					?>
    					<?php echo '<a>term_id) . '" rel="tag">' . $tag->name . '</a>';	?>
    					<?php } ?>
                                             </p>
    				</div>

    I want to exclude 1 tag, but I have no idea to do that (I tried using information from the internet but with no luck). Can someone please help me out?

    I use the code here: http://fastforwardandrewind.com

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator t-p

    @t-p

    Volunteer Moderator

    try adding this to the second line of your snippet:
    'exclude' => 'tag1,

    of course change the tag1 with your actual tag name.

    Michael

    @alchymyth

    Forum Moderator

    you will need to use the tag ID:

    http://codex.wordpress.org/Function_Reference/get_tags

    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.

    I am still not getting this to work (I must be doing something wrong).

    I would like to exclude the tag called ‘Featured’. So the code should be:

    ‘exclude’ => ‘Featured’,

    Right?

    Where should I put it? (I have tried multiple combinations, but couldn’t get it to work)

    <div id="tab3" class="tab_content">
    					<p class='tag_cloud'>
    					<?php
    					$tags = get_tags(array('orderby' => 'count', 'order' => 'DESC', 'number' => '28'));
    					foreach ((array) $tags as $tag) {
    					?>
    					<?php echo '<a href="' . get_tag_link ($tag->term_id) . '" rel="tag">' . $tag->name . '</a>';	?>
    					<?php } ?>
    					</p>
    				</div>

    Thanks in advance!

    Michael

    @alchymyth

    Forum Moderator

    have you read my reply with the link to the corresponding Codex chapter?

    Yes, but I don’t really understand it. Unfortunately there isn’t an example on the page where a tag is excluded.

    Moderator t-p

    @t-p

    Volunteer Moderator

    have tried adding 'exclude' => 'tag1' as I recommended in my post?

    As Alchymyth pointed out, the exclude parameter expects the term ID, not the slug or name.

    Find out the ID of the term(tag) you want to exclude and reference that in the exclude, eg.

    $args = array(
    	'orderby' => 'count',
    	'order' => 'DESC',
    	'number' => '28',
    	'exclude' => '1' // <-- update 1 to reflect the actual ID of the tag to exclude
    );
    $tags = get_tags( $args );

    I got it working! Thank you all! 🙂

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Resolved] PHP: Exclude Tag’ is closed to new replies.
Skip to toolbar