WordPress.org

Ready to get started?Download WordPress

Forums

Show only every 2nd post on page (3 posts)

  1. mutoo
    Member
    Posted 1 year ago #

    Hello,
    for a project it is necessary, that I show only every second post in a column.
    I´ve written a loop with a counter. With the counter I generate a special class in every second post. With css (display:none) I set these class non visible in the frontend. BUT - the post is still in the DOM.
    I think it is better to write a loop with a query - but I don´t know how. I hope, someon can help me.

    Here´s the complete loop.

    <?php
    
    				global $post;
    				$array = array();
    				$tmp_post = $post;
    				$pic = aktion;
    				$zaehler =0;
    
    				if ($pic) {$args = array( 'numberposts' => 50,'category_name' => $pic );}
    				$myposts = get_posts( $args );
    				$counter = count($myposts);
    				foreach( $myposts as $post ) :
    				$zaehler ++;
    				setup_postdata($post);	
    
    ?>
    
    		<section <?php if ($zaehler % 2 == 0) { echo  'class='.'"'.'box'.'"'; } else { echo   'class='.'"'.'none'.'"'; }  ?> >
    			<div class="nofloat">
    				<div <?php if ($zaehler % 2 == 0) { echo  'class='.'"'.'logoright'.'"'; }; ?>>
    					<?php
    					if (has_post_thumbnail()) {the_post_thumbnail('category-thumb');
    					}
    					?>
    
    				</div>
    				<div <?php if ($zaehler % 2 == 0) { echo  'class='.'"'.'contentright'.'"'; } ?>>
    					<div class="datum"><?php $datum = get_post_meta($post -> ID, 'Aktionszeit', $single = true); echo $datum;?></div>
    					<h1><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1><?php the_excerpt(__('')); ?>
    					<a class="more" href="<?php echo get_permalink(); ?>"> weiterlesen   <span>+</span></a>
    				</div>
    			</div>
    		</section>
    
    <?php endforeach;  $post = $tmp_post; ?>
  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    $zaehler ++;
    				setup_postdata($post);
    
    if ($zaehler % 2 != 0) continue;
  3. mutoo
    Member
    Posted 1 year ago #

    Wow - very easy and it run´s great!!!

    Thank you for your help!!

    Toni

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags