类别.php中的多个自定义POST类型循环

时间:2014-10-22 作者:indietravel

我有多个使用相同类别分类法的自定义帖子类型。让我们把CPT称为“场所”和“故事”,并说我们已经将它们分别归类为“foo”。

我已经有了以下代码来确保它们是分类循环的一部分:

//make sure these custom post types are registered for taxonomy queries

add_filter(\'pre_get_posts\', \'query_post_type\');
function query_post_type($query) {
  if(is_category() || is_tag()) {
    $post_type = get_query_var(\'post_type\');
    if($post_type)
        $post_type = $post_type;
    else
        $post_type = array(\'nav_menu_item\', \'venue\', \'story\'); // don\'t forget nav_menu_item to allow menus to work!
    $query->set(\'post_type\',$post_type);
    return $query;
    }
}
当我访问示例时。com/类别/foo,类别。php已启动。我想看到的是:

页面开始并显示自定义图像、标题、前导句

标题“场馆”中的3个项目,foo类

标题“故事”中的6个项目,foo类

分页,页脚

这是分类。php:(另请访问:https://gist.github.com/anonymous/b747a2c51bc5bb7a58dd)

<?php
/**
 * The template for displaying category pages.
 *
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package DW Focus
 * @since DW Focus 1.0
 */

get_header(); ?>
    <?php
    // vars
    $queried_object = get_queried_object();
    $taxonomy       = $queried_object->taxonomy;
    $image          = get_field(\'category_image\', $queried_object);
    $lead           = get_field(\'category_lead\', $queried_object);
    ?>

<div id="primary" class="site-content category span12">

        <?php

            if( !empty($image) ): ?>
                <div id="featured_image_category" style="height:200px;">
                    <img src="<?php echo $image[\'url\']; ?>" alt="<?php echo $image[\'alt\']; ?>" />
                </div>
            <?php endif; ?>


        <div class="content-bar row-fluid">
            <h1 class="page-title"><?php printf( __( \'%s articles\', \'dw-focus\' ), \'<span>\' . single_cat_title( \'\', false ) . \'</span>\' ); ?></h1>
        </div>

        <?php if( !empty($lead) ): ?>
            <div class="lead"><p><?php echo $lead; ?></p></div>
        <?php endif; ?>

            <h2 style="text-align: center;">Luxury and Boutique Hotels in <?php printf( __( \'%s\', \'dw-focus\' ), \'<span>\' . single_cat_title( \'\', false ) . \'</span>\' ); ?></h2>

        <?php get_template_part( \'content\', \'venueloop\'); ?>

        <div class="clearfix"></div>

        <h2 style="text-align: center;">Explore <?php printf( __( \'%s\', \'dw-focus\' ), \'<span>\' . single_cat_title( \'\', false ) . \'</span>\' ); ?> with our unique travel experiences</h2>

        <?php get_template_part( \'content\', \'storyloop\' ); ?>

        <div class="clearfix"></div>
        <?php dw_focus_pagenavi(); ?>
</div>

<?php get_footer(); ?>
venueloop和story loop的内容部分(也在https://gist.github.com/indietravel/38ed27762fb844eee0a5):

<div class="content-inner">
        <?php

            if ( is_category() ) {
                $cat = get_query_var(\'cat\');
                $ourcat = get_category ($cat);
                $category_selected = $ourcat->slug;
            }

            $args = array (
                \'post_type\'              => \'story\',
                \'post_status\'            => \'publish\',
                \'posts_per_page\'         => \'6\',
                \'category_name\'          => $category_selected,
            );

            // The Query
            $cat_query = new WP_Query( $args );
        ?>
        <?php if ( $cat_query -> have_posts() ) : ?>
            <?php global $archive_i; $archive_i = 1 ?>
            <?php while ( $cat_query -> have_posts() ) : $cat_query -> the_post(); ?>
                <?php get_template_part(\'content\', \'archive\'); ?>
                <?php $archive_i; ?>
            <?php endwhile; ?>
        <?php endif;

        wp_reset_postdata(); // ADAPTED FOR CUSTOM POST TYPES
        wp_reset_query();

        ?>

    </div>
场馆环路:

    <?php

    // WP_Query arguments
    $args = array (
    \'post_type\'             => \'venue\',
    \'post_status\'           => \'publish\',
    \'pagination\'            => false,
    \'posts_per_page\'        => \'3\',
);

// The Query
$venue_query = new WP_Query( $args );

// The Loop
if ( $venue_query->have_posts() ) {
    while ( $venue_query->have_posts() ) {
        $venue_query->the_post();

        the_title();

    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();
?>
出于某种原因,结果是页面总是显示,

页面开始并显示自定义图像、标题、前导句

标题“场馆”中的3x个项目故事CPT,foo类

标题“场馆”中的3x个项目故事CPT,foo类

分页,页脚

2 个回复
SO网友:indietravel

解决方案:

//make sure these custom post types are registered for taxonomy queries

add_filter(\'pre_get_posts\', \'query_post_type\');
function query_post_type($query) {
  if(is_category() || is_tag()) {
    $post_type = get_query_var(\'post_type\');
    if($post_type)
        $post_type = $post_type;
    else
        $post_type = array(\'nav_menu_item\', \'venue\', \'story\'); // don\'t forget nav_menu_item to allow menus to work!
    $query->set(\'post_type\',$post_type);
    return $query;
    }
}
正在筛选结果(之前?之后?)模板内查询。因此,它完全删除了只使用请求的post类型的请求。更改为

//make sure these custom post types are registered for taxonomy queries

add_filter(\'pre_get_posts\', \'query_post_type\');
function query_post_type($query) {
  if( is_tag() ) {
    $post_type = get_query_var(\'post_type\');
    if($post_type)
        $post_type = $post_type;
    else
        $post_type = array(\'nav_menu_item\', \'venue\', \'story\'); // don\'t forget nav_menu_item to allow menus to work!
    $query->set(\'post_type\',$post_type);
    return $query;
    }
}
然后页面上的自定义查询开始工作。

SO网友:Muddasir

Try this

    <?php 
    $args = array ( \'post_type\' => array( \'nav_menu_item\', \'venue\', \'story\' )   );
    $query = new WP_Query( $args );
    while ( $query->have_posts() ) : $query->the_post();

    the_title();
    the_content();

    endwhile; 
    wp_reset_postdata(); 
    ?>
结束

相关推荐

Show Pages in Categories

通过将此代码添加到函数中,我创建了category函数。php:function page_category() { register_taxonomy_for_object_type(\'category\', \'page\'); } // Add to the admin_init hook of your theme functions.php file add_action( \'init\', \'page_category\' ); 但问