سؤال

How do I rewrite the if (have_posts()) : ... while (have_posts()) : the_post(); style Wordpress loop below as a new_query style-loop using <?php $the_query = new WP_Query();? ( As shown in http://codex.wordpress.org/Class_Reference/WP_Query )

The current loop below alternates post classes to display a grid of posts two posts wide (see image below). But I need to rewrite it using a new query so I can: 1) add a post display count, and 2) a post count offset to it. The reason is I need to use it in the page template with another loop based on new query.

This loop alternates the class between "uneven post" and "post" to display the posts (see in the image below.) I need to retain that function in a new WP_Query loop.

<?php

    if (have_posts()) :
    $odd = false;
    while (have_posts()) : the_post();
    $odd = !$odd;
    ?>

    <div class="<?php if ($odd) echo 'uneven '; ?>post">

    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>

    <?php the_excerpt(); ?> 

    </div>

    <?php endwhile; else: endif; ?>

The loop above displays posts like this:

enter image description here

This is a standard new WP_Query loop:

<?php $my_query = new WP_Query('offset=5&showposts=10'); ?>

<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">

<?php the_title(); ?></a>

لا يوجد حل صحيح

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى wordpress.stackexchange
scroll top