SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trying to alternate row colors

    This is what I currently have. But I am getting an error

    PHP Code:
             <?php if ($the_ID() % == 0); ?>
         <div class="post" id="post-<?php the_ID(); ?>">
             <?php else ?>
         <div class="postalt" id="post-<?php the_ID(); ?>">
             <?php endif; ?>
    Its barfing on the ELSE part

  2. #2
    _ silver trophy ses5909's Avatar
    Join Date
    Jul 2003
    Location
    NoVa
    Posts
    5,467
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Perhaps you need to add a colon after the if statement and your else
    Sara

  3. #3
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    should be
    PHP Code:
    <?php if ($the_ID() % == 0) { ?>
        <div class="post" id="post-<?php the_ID(); ?>">
            <?php } else { ?>
        <div class="postalt" id="post-<?php the_ID(); ?>">
            <?php ?>
    Not sure what you mean by $the_ID() either, that should throw up an error too... it should be either a variable ($the_ID) or a function (the_ID())

  4. #4
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gavwvin
    should be
    PHP Code:
    <?php if ($the_ID() % == 0) { ?>
        <div class="post" id="post-<?php the_ID(); ?>">
            <?php } else { ?>
        <div class="postalt" id="post-<?php the_ID(); ?>">
            <?php ?>
    Not sure what you mean by $the_ID() either, that should throw up an error too... it should be either a variable ($the_ID) or a function (the_ID())
    the_ID(); could easily be a function

    PHP Code:
    function the_ID()
    {
         return 
    5;

    Dylan

  5. #5
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    Actually, I had to remove the $ Its for Word Press.

    For some reason, now its placing a number for each row, but I am not displaying it anywhere else but the <div>

    PHP Code:
     <?php if (the_ID() % == 0) { ?>
         <div class="post" id="post-<?php the_ID(); ?>">
             <?php } else { ?>
         <div class="postalt" id="post-<?php the_ID(); ?>">
     <?php ?>

  6. #6
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh, and its not alternating the class's

  7. #7
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dylan B
    the_ID(); could easily be a function

    PHP Code:
    function the_ID()
    {
         return 
    5;

    Dylan
    Yes, but there was a dollar sign out front.

  8. #8
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess WP is pulling out the post ID from the database. I figured that would be a good variable to use if I want to alternate row colors

  9. #9
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm... The post ID? The problem is if you delete a post. Then you might have posts 1, 2, 4, 5, 7, 8, 9, 10, etc. So you'd have odd, even, even, odd, odd, even, odd, even, etc.

    The best way would be to use an iterator variable (or whatever it's called) in a for loop:

    PHP Code:
    for($i 0$row mysql_fetch_assoc($result); $i++)
    {
         
    $bg = ($i 2) ? '#fff' '#def';
         
    // etc.


  10. #10
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks

    So what would I put in $result?

  11. #11
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No idea... Whatever the MySQL result is.

    You can post the Wordpress code if you'd like.

  12. #12
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
     <?php get_header(); ?>
     
         <h2>Latest Blog Postings</h2>
         <?php if (have_posts()) : ?>
             
             <?php while (have_posts()) : the_post(); ?>
             
         <div class="post" id="post-<?php the_ID(); ?>">
             
         <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h4>
         <span class="blogcategory"><?php the_category(', '?> - <?php the_time('F jS, Y'?></span>
         
             <div class="entry">
             <?php the_excerpt(); ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">Read more on <?php the_title(); ?></a>
             </div>
         </div>    
     
             <?php endwhile; ?>
                 <?php else : ?>
     
             <h2>Not Found</h2>
             <p>Sorry, but you are looking for something that isn't here.</p>
             <?php include (TEMPLATEPATH "/searchform.php"); ?>
             
             <?php endif; ?>
     <?php get_footer(); ?>
    Where it says <div class="post" is where I want to alternate the color of the bg of the div

  13. #13
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay. Try this:

    PHP Code:
    <?php get_header(); ?>

        <h2>Latest Blog Postings</h2>
        <?php if (have_posts()) : ?>
            
            <?php for ($i 0have_posts(); $i++) : the_post(); ?>
            
        <div class="post<?php if ($i 2) echo 'alt' ?>" id="post-<?php the_ID(); ?>">
            
        <h4><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h4>
        <span class="blogcategory"><?php the_category(', '?> - <?php the_time('F jS, Y'?></span>
        
            <div class="entry">
            <?php the_excerpt(); ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">Read more on <?php the_title(); ?></a>
            </div>
        </div>    

            <?php endwhile; ?>
                <?php else : ?>

            <h2>Not Found</h2>
            <p>Sorry, but you are looking for something that isn't here.</p>
            <?php include (TEMPLATEPATH "/searchform.php"); ?>
            
            <?php endif; ?>
    <?php get_footer
    (); ?>

  14. #14
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Young Twig
    Yes, but there was a dollar sign out front.
    Good call, Twiggy.

  15. #15
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks.

    It didn't like your ENDWHILE on line 18, right above the PHP else

  16. #16
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nevermind, I figured the EVEN part out.

  17. #17
    SitePoint Wizard jag5311's Avatar
    Join Date
    Jan 2003
    Location
    Somewhere in Indiana
    Posts
    3,082
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    btw - this was the error

    PHP Code:
     Parse errorparse errorunexpected T_ENDWHILE in c:\domains\somethingspecialgiftbaskets.com\wwwroot\blog\wp-content\themes\ssgb\index.php on line 18 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •