• Resolved vajnabotond

    (@vajnabotond)


    I have this script:

    <?php
    $args = array('post_type' => 'echipa');
                $echipa = new WP_Query( $args );
                if( $echipa->have_posts() ) {
                    while( $echipa->have_posts() ) {
                        $echipa->the_post();
                        ?>
    			<div class='echipa_color'>
                            <h1><?php the_title() ?></h1>
                            <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    			<?php the_content() ?>
                            </div>
                            </div>
                    <?php
    }
            }
            else {
                echo 'Oh ohm no pages!';
            }
        ?>

    I need first post white background, second post gray background, third white again, then gray again and so on,
    so I should have in the html instead: <div class=’echipa_color’>:

    <div class='echipa_white'>page1<div>
    <div class='echipa_gray'>page2<div>
    <div class='echipa_white'>page3<div>
    <div class='echipa_gray'>page4<div>

    … and so on

Viewing 8 replies - 1 through 8 (of 8 total)
  • What theme are you using? Where did you download it from?

    Thread Starter vajnabotond

    (@vajnabotond)

    I am using a twentythirteen

    Do not edit the Twenty Thirteen theme. It is the current default WordPress theme and having access to an original, unedited, copy of the theme is vital in many situations. First create a child theme for your changes. Or install a custom CSS plugin.

    Thread Starter vajnabotond

    (@vajnabotond)

    sorry, I am using a child theme for the Twenty Thirteen ??

    Try something like:

    $echipa = new WP_Query( $args );
    $c = 0;
    if( $echipa->have_posts() ) {
    	while( $echipa->have_posts() ) {
    		echipa->the_post();
    		$c++;
    		if( $a % 2  == 0 ) $style="echipa_color grey'; // even post
    		else $style="echipa_color white'; // odd post
    		?>
    		<div <?php post_class($style);?>>
    		<h1><?php the_title() ?></h1>
    		<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    		<?php the_content() ?>
    		</div>
    		</div>
    		<?php
    	}
    }
    else echo 'Oh ohm no pages!';?>

    That should give you 2 new classes – .white on odd posts and .grey on even posts.

    Thread Starter vajnabotond

    (@vajnabotond)

    thank you, but something is wrong, probably you have made some small mistakes bechouse your scrip does not work at all, I have a blank screen, but I have copied only the changes from your script, now it is better, but they are all grey

    <?php
    $args = array('post_type' => 'echipa');
                $echipa = new WP_Query( $args );
                $c = 0;
                if( $echipa->have_posts() ) {
                    while( $echipa->have_posts() ) {
                        $echipa->the_post();
                        $c++;
                        if( $a % 2  == 0 ) $style="echipa_color grey";
                        else $style="echipa_color white";
                        ?>
    			<div <?php post_class($style);?>>
                            <h1><?php the_title() ?></h1>
                            <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    			<?php the_content() ?>
                            </div>
                            </div>
                    <?php
    }
            }
            else {
                echo 'Oh ohm no pages!';
            }
        ?>
    Thread Starter vajnabotond

    (@vajnabotond)

    <div class="post-29 echipa type-echipa status-publish format-standard hentry echipa_color grey"> … </div><div class="post-28 echipa type-echipa status-publish format-standard hentry echipa_color grey"> … </div><div class="post-27 echipa type-echipa status-publish format-standard hentry echipa_color grey"> … </div><div class="post-26 echipa type-echipa status-publish format-standard hentry echipa_color grey"> … </div><div class="post-25 echipa type-echipa status-publish format-standard hentry echipa_color grey"> … </div><div class="post-9 echipa type-echipa status-publish format-standard hentry echipa_color grey"> … </div><div class="post-24 echipa type-echipa status-publish format-standard hentry echipa_color grey">

    Thread Starter vajnabotond

    (@vajnabotond)

    ok I fund it we must have $c not $a at the if ??

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘different background colors in the loop’ is closed to new replies.