WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] php code instead of javascript for stripey tables (6 posts)

  1. pgb
    Member
    Posted 3 years ago #

    In a page template I'm looking to have some tables with alternate stripes. Javascript doesn't work. Does anyone know of some php to do this and where to enter it in my template?

  2. aphill70
    Member
    Posted 3 years ago #

    Please provide some more information and possibly the url of the site so that we can understand exactly what you attempting.

    are you building the table within the post or page or is this a piece of meta that you are trying to fill. this will decide what you can and can't do.

  3. pgb
    Member
    Posted 3 years ago #

    Thanks. I am working locally at the moment, so no url, sorry. I have a page template and want to have any amount of tables on any pages that use the template. I want every other tr to have a different color (stripes). Javascript doesn't work. I could use html and css classes which will get tedious with long tables. Is there any php which could cover this and where should I insert it in the template?

  4. elfin
    Moderator
    Posted 3 years ago #

    $x=1;
    while(...){
     if($x%2) $alt=' class="alt"';
     else $alt='';
     echo '<tr'.$alt.'>'
     ...
     $x++;
    }
  5. Digital Raindrops
    Member
    Posted 3 years ago #

    Hi pgb,
    I answered a topic the other day, which is almost what you want, as someone wanted a list like the forum posts here.

    It is the same concept as the reply from Rich, but has a working example as a 'twenty ten' child theme.

    There is a download, which is a child theme for twenty ten (3 files), inside the download there is the loop-index.php which has the code change.

    Or the code is here: lines 40 to 45, all it does is MOD counter and add a background style to the post element.

    Inside the loop change alternate backgrounds:

    <?php $counter++; $style=""; ?>
    <?php if($counter % 2) $style='style="background-color: #f5f5f5";'; ?>
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?> <?php echo $style; ?> >

    HTH

    David

  6. pgb
    Member
    Posted 3 years ago #

    Thanks very much, I'll give it a try.

Topic Closed

This topic has been closed to new replies.

About this Topic