WordPress.org

Ready to get started?Download WordPress

Forums

$all_tags adding extra h1 tag (6 posts)

  1. elanio
    Member
    Posted 1 year 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: ');?>
    
    	</h1>

    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>
    <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. alchymyth
    The Sweeper & Moderator
    Posted 1 year 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
    Member
    Posted 1 year ago #

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year 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
    Member
    Posted 1 year 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. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

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

    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();
    http://codex.wordpress.org/Function_Reference/get_query_var
    the result of that might depend on the permalink settings.

    or try and use:

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

    example:

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

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.