将get_posts更改为loop.php合适的版本

时间:2015-03-03 21:18:33

标签: php wordpress

我不知道get_posts不能与loop.php一起使用。我已经有了这个很棒的loop.php文件,我想用它。 我创建了这段代码:

$pageposts = get_posts(
    array(
        'relation' => 'AND',
        'post__in' => $postid,
        'post_type'   => 'event',
        'post_status' => 'publish',
        'meta_query'  => array(
            array(
                'key'    => 'st_date', 
                'value'  => array($todate_s, $frmdate_s),
                'compare'=> 'BETWEEN',
                'type'   => 'DATE'
              ),
        ),
    )
);
if ( have_posts() ) : ?>
<?php get_template_part('loop'); ?>
<?php else : ?>//etc

如何在代码末尾将get_posts(//etc)转换为与have_posts()get_template_part('loop')一起使用的内容? 这可能吗?

1 个答案:

答案 0 :(得分:1)

您在query_posts();get_posts();之间感到困惑,请尝试以下方法:

<?php
$pageposts = get_posts(
    array(
        'relation' => 'AND',
        'post__in' => $postid,
        'post_type'   => 'event',
        'post_status' => 'publish',
        'meta_query'  => array(
            array(
                'key'    => 'st_date', 
                'value'  => array($todate_s, $frmdate_s),
                'compare'=> 'BETWEEN',
                'type'   => 'DATE'
              ),
        ),
    )
);

foreach ( $pageposts as $post ) : setup_postdata( $post );

    get_template_part('loop');

endforeach;
wp_reset_postdata();

?>

http://codex.wordpress.org/Template_Tags/get_posts

<?php
query_posts(
    array(
        'relation' => 'AND',
        'post__in' => $postid,
        'post_type'   => 'event',
        'post_status' => 'publish',
        'meta_query'  => array(
            array(
                'key'    => 'st_date', 
                'value'  => array($todate_s, $frmdate_s),
                'compare'=> 'BETWEEN',
                'type'   => 'DATE'
              ),
        ),
    )
);

get_template_part('loop');

wp_reset_query();

?>

http://codex.wordpress.org/Function_Reference/query_posts