• Resolved formica

    (@formica)


    Anyone know how to modify the loop to bold up the first result?

    Thinking something along the lines of below but my php isn’t too hot. Thanks in advance.

    <?php $first = true;
    while ($row = mysql_fetch_array()) {
    if ($first) {
    // bold
    $first = false;
    } else {
    // normal
    }
    }?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • your php is correct, if you’re creating your own mysql command.

    Simple counter..

    <?php
    $i = 0;
    while( doing whatever you're doing ) {
       $i++;
       if( 1 == $i ) {
          // Do whatever you need to bold this result
       }
       else {
          // Else do whatever you do with any result not the first
       }
    }

    Thread Starter formica

    (@formica)

    Thanks Mark!

    That was exactly what I was looking for – much appreciated.

    for the record here’s the code I ended with:

    <?php $i = 0; if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    	<?php $i++; ?>
    	<?php if( 1 == $i ) { ?>
    		<li class="current"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    	<?php } else { ?>
    		<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    	<?php } ?>
    <?php endwhile; ?>

    Also found a way of doing using css2 though browser support is still a bit behind thanks to Curtiss for this one:

    https://www.ads-software.com/support/topic/bold-first-post-title-in-category

    The PHP method will always work, the CSS method won’t…

    There are actually a few ways in CSS you can do it, but they both suffer the same issue of not working across all browsers..

    Using the first-child selector.

    ul li:first-child { }

    Using the adjacent sibling selector.

    ul li { /* do styling for first item here */ }
    ul li+li { /* do override styling to restyle any not the first */ }

    Using nth-child selector.

    ul li:nth-child(1) { /* same effect as the :first-child, just a differer method  */ }

    Then there’s also the option of adding styling to the first child with jQuery, this has few browser limitations, but obviously requires upto date JS support(and of course JS must be enabled).

    jQuery(document).ready(function($){
       $('ul li:first-child').addClass('some_class_to_add_to_first');
    });

    The PHP approach removes any browser concerns.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘bold up first result of loop’ is closed to new replies.