有没有更好的方法在wordpress中使用循环?

时间:2013-03-14 14:35:10

标签: php wordpress

我在wordpress中有一个日历自定义帖子类型。我在我的模板中工作正常,但我知道有更好的方法来运行循环。我在本月(1月,2月,3月等)使用了自定义分类法,并在页面上逐月对帖子进行排序。目前,这意味着我必须运行12个独立的循环来获得我想要的功能,我确信这不是正确的方法。

以下是我正在使用的代码,非常感谢任何帮助。谢谢!

<div class="module">
<h2>January</h2>
<?php // Annual Events Posts
$calendar = new WP_Query('post_type=calendar&month=January'); 
while($calendar->have_posts()) : $calendar->the_post(); 
?>              
<div class="grid" style="background:none">

<div class="col-1">
    <h3>
        <?php 
            the_title();
        ?>
    </h3>
    <h4>
        <?php 
            $dateValue = get_post_meta( $post->ID, 'rhc_when', true );
            if($dateValue != '') {
                echo $dateValue;
            }; 
            $timeValue = get_post_meta( $post->ID, 'rhc_time', true );
            if($timeValue != '') { ?>, <?php
                echo $timeValue;
            }
         ?>                         
    </h4>
    <?php $content = the_content(); 
        if( $content !='') {
            echo $content;
        };
    ?>
</div>
</div>
<?php 
    endwhile;
    wp_reset_postdata();
?>
<hr>
<h2>February</h2>
<?php // Annual Events Posts
    $calendar = new WP_Query('post_type=calendar&month=February');
    while($calendar->have_posts()) : $calendar->the_post(); 
?>              
<div class="grid" style="background:none"> 
    <div class="col-1">
        <h3>
            <?php 
                the_title();
            ?>
        </h3>
        <h4>
            <?php 
                $dateValue = get_post_meta( $post->ID, 'rhc_when', true );
                if($dateValue != '') {
                    echo $dateValue;
                }; 
                $timeValue = get_post_meta( $post->ID, 'rhc_time', true );
                if($timeValue != '') { ?>, <?php
                    echo $timeValue;
                }
            ?>                          
        </h4>
        <?php $content = the_content(); 
            if( $content !='') {
                echo $content;
            };
        ?>
    </div>
</div>
<?php 
    endwhile;
    wp_reset_postdata();
?>

1 个答案:

答案 0 :(得分:1)

我能想到的唯一不正确的是你使用the_content()。如果要在确定是否要回显变量之前将Post Content设置为变量,则需要使用get_the_content。除此之外,一切都还可以,但我会更加浓缩一下:

<div class="module">
<?php
// Annual Events Posts
$months = array(
    'January',
    'February',
    'March',
    'April',
    'May',
    'June',
    'July',
    'August',
    'September',
    'October',
    'November',
    'December'
);
foreach($months as $month){
    $calendar = new WP_Query('post_type=calendar&month='.$month); 
    if($calendar->have_posts()) :
    ?>
    <h2><?php echo $month; ?></h2>
    <div class="grid" style="background:none">
    while($calendar->have_posts()) : $calendar->the_post(); 
    ?>
        <div class="col-1">
            <h3><?php the_title(); ?></h3>
            <h4>
            <?php 
            if($dateValue = get_post_meta( $post->ID, 'rhc_when', true ))
                echo "$dateValue, ";
            if($timeValue = get_post_meta( $post->ID, 'rhc_time', true ))
                echo $timeValue;
            ?>                         
            </h4>
            <?php
            if($content = get_the_content())
                echo $content;
            ?>
        </div>
    <?php 
    endwhile;
    ?>
    </div>
    <hr>
    <?php
    endif;
    wp_reset_postdata();
}
?>
</div>

另外,感谢您使用WP_Query而不是query_posts。

修改

我添加了一个条件来确定是否需要写入网格div和标题。总的来说,你的代码非常可靠。