WordPress.org

Support

Support » Themes and Templates » [Resolved] title for category links in a while loop to appear once

[Resolved] title for category links in a while loop to appear once

  • I’m trying to put a title in a while loop on the category page for my category links but i want to put inside the while loop but i want it to only appear once above all my links is there a way to do this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • <?php
    if ($all_the_tags);
    $all_the_tags = get_the_tags();
    foreach($all_the_tags as $this_tag) {
    	if ($this_tag->name == "BOOKS" ) {
    ?>
    
    <table width="635px" class="category_posts">
    <tbody>
    			<tr style="border-left:1px solid #D1D1D1; border-top:1px solid #D1D1D1; border-right:1px solid #D1D1D1;">
    
    				<td class="category_td" style="width:15%; color: #34A9D6; font-size: 13px; text-align: center;"><strong>Volume</strong></td>
    				<td class="category_td" style="width:73%; color: #34A9D6; font-size: 13px; text-align: center;"><strong>Title</strong></td>
    				<td class="category_td" style="width:12%; color: #34A9D6; font-size: 13px; text-align: center;"><strong>Views</strong></td>
    
    <tr>
    </tbody>
    </table>
    
    		<?php while (have_posts()) : the_post(); ?>
    
    <table width="635px" class="category_posts2">
    <tbody>
    
    <td class="category_td1" style="width:15%;"><span class="vol"><a href="<?php the_permalink() ?>" rel="bookmark" >Volume <?php the_title(); ?></a></span></td>
    <td class="category_td2" style="width:73%;"><span class="subtitle"><a href="<?php the_permalink() ?>" rel="bookmark" ><?php echo ShortenTitle(the_subtitle()); ?></a></span></td>
    <td class="category_td3" style="width:12%;"><?php the_views(); ?></td>
    </tr>
    </tbody>
    </table>
    
    <?php endwhile; ?>
    
    <?php 	} else {
    		// it's neither, do nothing
    ?>
    		<!-- not tagged as one or the other -->
    <?
    	}
    }
    
    ?>

    heres the code I keep getting a Invalid argument supplied for foreach() error, it works if i take the while loop outside the foreach but then the table headers will repeat as well and i don’t want the headers outside the if else statement anyone have a clue to how to get this to work?

    put the while loop outside the foreach and add a counter to keep track the first loop.

    ie. $i = 0; outside the while loop
    inside the while loop, check if $i == 0, then output the title and increment $i.

    since the looping involve not so many steps, the overhead won’t too large

    how exactly am I suppose to implement this? sorry still not that great with php…

    anyone?

    <?php
    
    $i=0;
    <?php while (have_posts()) : the_post(); ?>
    <?php
    if($i == 0) {
    	$i++;
    	?>
    	<!-- title table or add checking if this is "BOOKS" -->
    	<?php
    } else { ?>
    <!-- Original content inside the loop -- >
    <?php } ?>
    
    ......

    As $i is incremented by $i++, the part for output the title table will only be executed once.
    If you need checking if the category is BOOKS or not so that the title table will appear only in BOOKS, do checking after the $i++

    Hope that helps you

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] title for category links in a while loop to appear once’ is closed to new replies.
Skip to toolbar