WordPress.org

Forums

[resolved] title for category links in a while loop to appear once (6 posts)

  1. gbaka
    Member
    Posted 4 years ago #

    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?

  2. gbaka
    Member
    Posted 4 years ago #

    <?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?

  3. peterwongpp
    Member
    Posted 4 years ago #

    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

  4. gbaka
    Member
    Posted 4 years ago #

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

  5. gbaka
    Member
    Posted 4 years ago #

    anyone?

  6. peterwongpp
    Member
    Posted 4 years ago #

    <?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

Topic Closed

This topic has been closed to new replies.

About this Topic