0

on my newssite i have the first post in a full-width column, after that all older post are in three columns per row. But now i want that wordpress put after every three columns a and to keeps the elements into rows. I tried to adjust the following solutions: Solution 1 Solution 2 Solution 3

But nothing of that will work.

Here is my code to get my posts:

       <div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
   <div class=" entry-content">
   <?php if (have_posts()) : ?>
   <?php $post = $posts[0]; $c=0;?>
   <?php while (have_posts()) : the_post(); ?>

   <?php $c++;
   if( !$paged && $c == 1) :?>

        <?php get_template_part( 'template-parts/content', 'aktuelles-first' ); ?>

   <div class="accordion">
   <div class="row">

   <?php else :?>
    <?php get_template_part( 'template-parts/content', 'aktuelles' ); ?>


      <?php endif;?>

   <?php endwhile; ?>

   <?php endif; ?>

       </div></div>

    </main><!-- #main -->
       </div><!-- #primary -->

Is there any solution without changing my way to get the posts?

0

Here's an idea of what I think you could do if I understand the problem correctly:

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
        <div class=" entry-content">
        <?php
        if ( have_posts() ) {
            $post = $posts[0];
            $c = 0;
            $i = 0;

            while ( have_posts() ) {
                the_post();

                // start a new row for the first post and every three posts
                if ( ! $paged && ( 0 == $c || 0 == ( $c % 3 ) ) ) {
                    get_template_part( 'template-parts/content', 'aktuelles-first' );
                    echo my_code_open_row();
                } else {
                    get_template_part( 'template-parts/content', 'aktuelles' );
                }

                // close the opened divs above
                if ( $i > 2 ) {
                    echo my_code_close_row();
                    $i = 0; // Reset the row close counter
                }

                $c++;
                $i++;
            }

            // Close the row if we never get to the row closing
            // before we leave the loop
            if ( $i < 2 ) {
                echo my_code_close_row();
            }
        }
        ?>
        </div>
    </div>
    </main><!-- #main -->
</div><!-- #primary -->
<?php
function my_code_open_row() {
    return <<<HTML
    <div class="accordion">
        <div class="row">
HTML;
}

function my_code_close_row() {
    return <<<HTML
        </div>
    </div>
HTML;
}
  • 1
    Works! Thanks for your help and the explanations into your code! – SirPrice Oct 13 '15 at 9:45
0

I'm not sure I'm entirely understanding the question, however it looks like you're using $c to track which post you're on. I think what you're missing is you need to set it up so every 4th post is treated differently, correct? You can test for that using the modulus operator if ($c % 4 == 0) { - then add the special HTML for that row. And remember to test again and close the divs as need be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.