The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

$all_tags adding extra h1 tag (6 posts)

  1. elanio
    Posted 4 years ago #

    I am trying to figure out why extra <h1 class="category-title"> are being inserted into the markup.

    here is the code/loop I am using inside of archive.php:

    elseif (is_archive('the_tag')) {
    	  if ($all_the_tags);
    	    $all_the_tags = get_the_tags();
    	    foreach($all_the_tags as $this_tag) {
    		    if ($this_tag->name == "test1" ) {
    		      echo '<div id="cat-title-logo" ><img alt="test1" src="' . get_bloginfo('template_url') . '/imgs/logo-test1.jpg"/></div><h1 class="category-title">';
    		    } else if ($this_tag->name == "test2" ) {
    		      echo '<div id="cat-title-logo" ><img alt="test2" src="' . get_bloginfo('template_url') . '/imgs/logo-test2.jpg"/></div><h1 class="category-title">';
    		    } else {
    			    // it's neither, do nothing
    		      echo '<h1 class="category-title">
      <!-- not tagged as one or the other -->';
    	    single_tag_title('Tag: ');?>

    The HTML markup looks like this:

    <h1 class="category-title"></h1>
    <h1 class="category-title">
        <div id="cat-title-logo"><img src="http://www.foobar.com/wp-content/themes/blah/imgs/logo-test1.jpg" alt="test1"></div>
    <h1 class="category-title">Tag: test1</h1>

    as you can see it's inserting 2 extra h1 tags, 1 empty one, and 1 wrapping one. Is this part of core functionality or something?!?!?!?

  2. Michael
    Forum Moderator
    Posted 4 years ago #

    please paste the full code of archive.php into a http://pastebin.com/ and post the link to it here - see http://codex.wordpress.org/Forum_Welcome#Posting_Code

  3. elanio
    Posted 4 years ago #

  4. Michael
    Forum Moderator
    Posted 4 years ago #

    you are obviously running a foreach loop; so the output will (randomly) depend on what tags the first (?) post in the tag archive has;

    also, single_cat_title() will print the tag name of the archive tag, but not the tag title of whatever is called in the foreach loop.

    to see what you are looping through, try to add var_dump($all_the_tags); before theforeach`.

    what exactly are you trying to achieve?

  5. elanio
    Posted 4 years ago #

    checking the tag array for a condition, if it's met do this.

    in the case of what I have in the pastebin, I am doing a check to see if the tag 'test1' exists, if it does, insert a <div> with the <img> above the <h1> tag.

    What I see happening is an empty <h1> tag an another <h1> tag wrapping the <div><img> tag.

  6. Michael
    Forum Moderator
    Posted 4 years ago #


    tha function has to be used in the loop, or the result is random.

    you could try and use get_query_var('tag'); instead of get_the_tags();
    the result of that might depend on the permalink settings.

    or try and use:



    $all_the_tags = get_term_by('name',single_tag_title('',false),'post_tag');


Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.