在主页上显示多种帖子类型

时间:2011-05-01 作者:EddyR

我已经阅读了几个如何执行此操作的示例,但我仍然无法使其正常工作-每次使用get\\u posts()时,它似乎只保留并显示第一组结果。

环php

function home_index() {

global $post;

echo \'<ul>\';

$args = array(
    \'numberposts\'     => 3,
    \'offset\'          => 0,
    \'orderby\'         => \'post_date\',
    \'order\'           => \'DESC\',
    \'post_type\'       => \'gp_news\',
    \'post_status\'     => \'publish\',
    \'meta_key\'        => \'_thumbnail_id\',
    \'meta_value\'      => 1,
    \'meta_compare\'    => \'>=\'
);

$myposts = get_posts( $args );

foreach( $myposts as $post ) {
    setup_postdata($post);
    echo \'<li>\';

    if ( has_post_thumbnail() ) {
        the_post_thumbnail( \'homepage-thumbnail\' );
    }
    ?>
    <h1>news <a href="<?php the_permalink(); ?>" title="Permalink to <?php esc_attr(the_title()); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
    <?php the_excerpt(); ?>
    <a href="<?php the_permalink(); ?>">Continue...</a>
    <?php
    echo \'</li>\';
}

$args = array(
    \'numberposts\'     => 1,
    \'offset\'          => 0,
    \'orderby\'         => \'post_date\',
    \'order\'           => \'DESC\',
    \'post_type\'       => \'gp_competitions\',
    \'post_status\'     => \'publish\',
    \'meta_key\'        => \'_thumbnail_id\',
    \'meta_value\'      => 1,
    \'meta_compare\'    => \'>=\'
);

$myposts = get_posts( $args );

foreach( $myposts as $post ) {
    setup_postdata($post);
    echo \'<li>\';

    if ( has_post_thumbnail() ) {
        the_post_thumbnail( \'homepage-thumbnail\' );
    }
    ?>
    <h1>competition <a href="<?php the_permalink(); ?>" title="Permalink to <?php esc_attr(the_title()); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
    <?php the_excerpt(); ?>
    <a href="<?php the_permalink(); ?>">Continue...</a>
    <?php
    echo \'</li>\';
}

echo \'</ul>\';   

}

4 个回复
最合适的回答,由SO网友:EddyR 整理而成

所以我终于想出了两种方法。第一种方法是。。。

global $wp_query;

$args = array(
    array(
        \'numberposts\'     => 3,
        \'offset\'          => 0,
        \'orderby\'         => \'post_date\',
        \'order\'           => \'DESC\',
        \'post_type\'       => \'gp_news\',
        \'post_status\'     => \'publish\',
        \'meta_key\'        => \'_thumbnail_id\',
        \'meta_value\'      => 1,
        \'meta_compare\'    => \'>=\'
    ),
    array(
        \'numberposts\'     => 1,
        \'offset\'          => 0,
        \'orderby\'         => \'post_date\',
        \'order\'           => \'DESC\',
        \'post_type\'       => \'gp_competitions\',
        \'post_status\'     => \'publish\'
    )
);

echo \'<ul class="hp_minifeatured">\';
$temp_query = clone $wp_query;
for($index = 0; $index < count($args); $index++) {
    $myposts = new WP_Query( $args[$index] );

    if($myposts->have_posts()) {
        while($myposts->have_posts()) {
            $myposts->the_post();
            echo \'<li>\';

            if ( has_post_thumbnail() ) {
                the_post_thumbnail( \'homepage-thumbnail\' );
            }
            ?>
            <p><?php echo($args[$index][\'post_type\']); ?></p>
            <h1><a href="<?php the_permalink(); ?>" title="Permalink to <?php esc_attr(the_title()); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
            <?php the_excerpt(); ?>
            <a href="<?php the_permalink(); ?>">Continue...</a>
            <?php
            echo \'</li>\';
        }
    }
}
$wp_query = clone $temp_query;  
echo \'</ul><div class="clear"></div>\';
。。。但这会导致使用多个查询。

另一种方法是。。。

global $wpdb;
global $post;

$querystr = "(SELECT wp_posts.* FROM $wpdb->posts wp_posts, $wpdb->postmeta wp_postmeta WHERE wp_posts.ID = wp_postmeta.post_id and wp_posts.post_status = \'publish\' and wp_posts.post_type = \'gp_news\' and wp_postmeta.meta_key = \'_thumbnail_id\' and wp_postmeta.meta_value >= 1 ORDER BY wp_posts.post_date DESC LIMIT 3) union (SELECT wp_posts.* FROM $wpdb->posts wp_posts, $wpdb->postmeta wp_postmeta WHERE wp_posts.ID = wp_postmeta.post_id and wp_posts.post_status = \'publish\' and wp_posts.post_type = \'gp_competitions\' and wp_postmeta.meta_key = \'_thumbnail_id\' and wp_postmeta.meta_value >= 1 ORDER BY wp_posts.post_date DESC LIMIT 1)";
$pageposts = $wpdb->get_results($querystr, OBJECT);

if ($pageposts) {
    foreach ($pageposts as $post) {
        setup_postdata($post);
        echo \'<li>\';
        if ( has_post_thumbnail() ) {
            the_post_thumbnail( \'homepage-thumbnail\' );
        }
        ?>
        <h1><a href="<?php the_permalink(); ?>" title="Permalink to <?php esc_attr(the_title()); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
        <?php the_excerpt(); ?>
        <a href="<?php the_permalink(); ?>">Continue...</a>
        <?php
        echo \'</li>\';
    }
}

SO网友:Brittany

我可以使用此行查询两种自定义帖子类型:

AND wposts.post_type IN (\'custom_post_type_1\', \'custom_post_type_2\')

这适用于我的情况,每个自定义帖子类型都是按相同的meta\\u键排序的

SO网友:Norcross

你不应该这么做。下面的函数将允许您控制默认循环中显示的内容,而无需重建查询本身

function cpt_pre_get_posts_filter( $query ) {
  global $wp_query;

  if ( !is_preview() && !is_admin() && !is_singular() && !is_404() ) {
    if ($query->is_feed) {
    /* set your post types in the array below */
    } else {
      $my_post_type = get_query_var( \'post_type\' );
      if ( empty( $my_post_type ) )
        $query->set( \'post_type\' , \'my-post-type\', \'my-other-post-type\' );
    }
  }

  return $query;
}
add_filter( \'pre_get_posts\' , \'cpt_pre_get_posts_filter\' );

SO网友:Sean Hise
$args = aray(
\'post_type\' => array ( \'post\', \'page\',\'event\')
);
query_posts($args);

Source

结束

相关推荐

Pagination with custom loop

我的问题可能是Pagination not working with custom loop, 但有一种不同。我使用自定义循环来显示flash游戏。我想按类别在游戏页面上分页。类别php:<?php if ($cat) { $cols = 2; $rows = 4; $paged = ((\'paged\')) ? get_query_var(\'paged\') : 1; $post_per_page = $cols * $rows; // -1 s