• I want to create a page that lists all tags but only if the post has been published. Cause right now its showing tags from scheduled posts as well which I don’t want. Can someone help me fix this code?

    <?php
    /*
    Template Name: Tags List
    */
    
    	/* Process
    	================================================= */
    	$list = '';
    	$tags = get_terms( 'post_tag' );
    	$groups = array();
    	if( $tags && is_array( $tags ) ) {
    		foreach( $tags as $tag ) {
    			$first_letter = strtoupper( $tag->name[0] );
    			$groups[ $first_letter ][] = $tag;
    		}
    		if( !empty( $groups ) ) {
    			foreach( $groups as $letter => $tags ) {
    				$list .= "\n\t" . '<h2>' . apply_filters( 'the_title', $letter ) . '</h2>';
    				$list .= "\n\t" . '<ul>';
    				foreach( $tags as $tag ) {
    					$url = attribute_escape( get_tag_link( $tag->term_id ) );
    					$count = intval( $tag->count );
    					$name = apply_filters( 'the_title', $tag->name );
    					$list .= "\n\t\t" . '<li><a href="' . $url . '">' . $name . '</a> (' . $count . ')</li>';
    					}
    				$list .= "\n\t" . '</ul>';
    			}
    		}
    	}else $list .= "\n\t" . '<p>Sorry, but no tags were found</p>';
    
    	/* OUTPUT
    	============================= */
    	get_header();
    	?>
    	<div id="content">
        <div id="content-list">
        <h1>Tag List</h1>
    	<?php print $list; ?>
    	</div>
        </div>
    	<?php
    	get_footer();
    	?>
Viewing 1 replies (of 1 total)
  • Hey Ruriko,

    As I’m checking your code is working fine, and fetching only the tags of published post not for scheduled post. But if this is not working for you, you can use the following :

    <?php
    
    $tags = get_tags();
    $html = '<ul class="your_class">';
    foreach ( $tags as $tag ) {
        $tag_link = get_tag_link( $tag->term_id );
    
        $html .= "<li><a href='{$tag_link}' title='{$tag->name} Tag' class='{$tag->slug}'>";
        $html .= "{$tag->name}</a></li>";
    }
    $html .= '</ul>';
    echo $html;
    
    ?>

    Possibly this may help you !

Viewing 1 replies (of 1 total)
  • The topic ‘How to create a tag list index?’ is closed to new replies.