Wordpress:类别页面

时间:2015-05-21 07:56:54

标签: wordpress categories custom-post-type

我有两种自定义帖子类型,例如“汽车”和“自行车”。我使用Wordpress的默认类别来分类两种帖子类型的帖子。比方说,例如,类别是“红色”,“蓝色”和“黑色”。

我在这里想要实现的是,当我进入“红色”类别页面时,我希望看到“汽车”和“自行车”被归类为“红色”。我正在使用category.php,这是我正在尝试运行的查询:

$car_args = array(
                    'posts_per_page'   => -1,
                    'orderby'          => 'date',
                    'order'            => 'DESC',
                    'post_type'        => 'cars',
                    'post_status'      => 'publish',
                    'cat'              => $cat
                );

                // The Query
                $car_query = new WP_Query( $car_args );

                // The Loop
                if ( $car_query ->have_posts() ) {
                echo "<h3>Cars</h3>";
                    while ( $car_query->have_posts() ) {
                        $car_query->the_post();
                        echo get_post_type() . '<a href="'. get_permalink() .'">' . get_the_title() . '</a><br />';
                    }
                } else {
                    // no posts found
                }

$bikes_args = array(
                    'posts_per_page'   => -1,
                    'orderby'          => 'date',
                    'order'            => 'DESC',
                    'post_type'        => 'bikes',
                    'post_status'      => 'publish',
                    'cat'              => $cat
                );

                // The Query
                $bikes_query = new WP_Query( $bikes_args );

                // The Loop
                if ( $bikes_query ->have_posts() ) {
                echo "<h3>Bikes</h3>";
                    while ( $bikes_query->have_posts() ) {
                        $bikes_query->the_post();
                        echo get_post_type() . '<a href="'. get_permalink() .'">' . get_the_title() . '</a><br />';
                    }
                } else {
                    // no posts found
                }

                /* Restore original Post Data */
                wp_reset_postdata();

查询中的$ cat获取“红色”类别的类别ID。这两个查询都通过“红色”类别正确限制帖子,但“汽车”帖子类型和“自行车”帖子类型的帖子都显示,即使我已尝试按帖子类型进行限制。我在一些文章中读到Wordpress忽略了类别页面上的帖子类型args。这是真的,如果是,是否有解决方法?

1 个答案:

答案 0 :(得分:1)

您尝试执行的操作只能使用一个查询,并且只能使用主查询而不需要任何自定义查询。

首先,让我们首先将您的自定义帖子类型添加到您的类别页面。默认情况下,自定义活塞类型不包括在类别页面中。因此,我们需要通过pre_get_posts手动将其添加到主查询参数中。将以下内容添加到您的functions.php:( CAVEAT:未经测试,还需要PHP 5.3 +

add_action( 'pre_get_posts', function ( $q )
{
    if ( !is_admin() && $q->is_main_query() && $q->is_category() ) {
        $q->set( 'post_type', array( 'post', 'cars', 'bikes' ) ); // Change this according to your post types
        $q->set( 'nopaging', true ); // This will get all posts, same as posts_per_page=-1
    }
});

您现在应该已经点击了所有类别中的帖子类型。

接下来,我们需要整理你的循环。删除category.php中的所有自定义查询,并将其替换为默认循环。正如您希望按帖子类型排序的两个块,我们将使用rewind_posts(),因此我们可以运行两次循环

if ( have_posts() ) {
    while ( have_posts() ) {
    the_post();

        if ( $post->post_type == 'cars' ) { //Change accordingly to only show cars in this loop

            // Your loop

        }
    }

    rewind_posts();

    while ( have_posts() ) {
    the_post();

        if ( $post->post_type == 'bikes' ) { // Change accordingly to only show bikes

            // Your loop

        }
    }
}

现在应该按照帖子类型

显示两个块中的帖子
相关问题