如何在wordpress中按类别显示自定义帖子类型帖子

如何在wordpress中按类别显示自定义帖子类型帖子,wordpress,Wordpress,我已经为展示计划创建了自定义的帖子类型,我想根据类别展示帖子。我正在functions.php中使用下面的代码为计划创建自定义帖子类型 add_action('init', 'create_post_types'); function create_post_types() { register_post_type( 'numbers_plan', array( 'labels' => array( 'name' => __( 'Num

我已经为展示计划创建了自定义的帖子类型,我想根据类别展示帖子。我正在functions.php中使用下面的代码为计划创建自定义帖子类型

add_action('init', 'create_post_types');
function create_post_types() {
    register_post_type( 'numbers_plan',
    array(
        'labels' => array(
        'name' => __( 'Numbers plan' ),
        'singular_name' => __( 'Numbers plan' )
    ),
    'public' => true,
    'has_archive' => true,
    'rewrite' => array('slug' => 'Numbers plan'),
     )
);

// Add new taxonomy(like categories)
$labels = array(
    'name'              => _x( 'PlanCat', 'numbers_plan', 'textdomain' ),
    'singular_name'     => _x( 'PlanCat', 'numbers_plan', 'textdomain' ),
    'search_items'      => __( 'Search PlanCat', 'numbers_plan' ),
    'all_items'         => __( 'All PlanCat', 'numbers_plan' ),
    'parent_item'       => __( 'Parent PlanCat', 'numbers_plan' ),
    'parent_item_colon' => __( 'Parent PlanCat:', 'numbers_plan' ),
    'edit_item'         => __( 'Edit PlanCat', 'numbers_plan' ),
    'update_item'       => __( 'Update PlanCat', 'numbers_plan' ),
    'add_new_item'      => __( 'Add New PlanCat', 'numbers_plan' ),
    'new_item_name'     => __( 'New PlanCat Name', 'numbers_plan' ),
    'menu_name'         => __( 'PlanCat', 'numbers_plan' ),
);

$args = array(
    'hierarchical'      => true,
    'labels'            => $labels,
    'show_ui'           => true,
    'show_admin_column' => true,
    'query_var'         => true,
    'rewrite'           => array( 'slug' => 'numbers_plan' ),
);

register_taxonomy( 'numbers_plans', array( 'numbers_plan' ), $args );
}
对于show plans,我使用blow代码创建了plan.php页面

<?php
    $plan_group = get_terms( 'numbers_plans' );
?>

<?php
foreach ( $plan_group as $plan_group_term ) {
$plan_group_query = new WP_Query( array(
    'post_type' => 'numbers_plan',
    'tax_query' => array(
        array(
            'taxonomy' => 'numbers_plans',
            'field' => 'slug',
            'terms' => array( $plan_group_term->slug ),
            'operator' => 'IN'
        )
    )
));
?>
<h2><?php echo $plan_group_term->name; ?></h2>
<ul>
<?php
if ( $plan_group_query->have_posts() ) : while ( $plan_group_query->have_posts() ) : $plan_group_query->the_post(); ?>
    <div>
        <div><?php echo the_title(); ?></div>
        <div><?php the_field('plan_minutes'); ?></div>
        <div><?php the_field('monthly_cost'); ?></div>
        <div><?php the_field('cost_of_additional_minutes'); ?></div>
        <?php echo do_shortcode("[ARForms_popup id=103 desc='Buy Now' type='link' height='540' width='800']"); ?>
        <br/>
    </div>
<?php endwhile; endif; ?>
</ul>
<?php
// Reset things, for good measure
$plan_group_query = null;
wp_reset_postdata();
}
?>


    从某个
    类别
    中选择帖子的一种方法是围绕帖子输出创建一个小的
    if语句
    。请参见下面的示例:

    <?php
        if (in_category('<category_name')) {
            /* the post */
        }
        else {
            /* Do nothing */
        }
    ?>
    
    
    
    一个完整的例子:

                 <?php if ( have_posts() ) :
                    if (in_category('Algemeen')) {
                        require_once('category-pages/Algemeen-category.php');
                    } elseif (in_category('Sport')) {
                        require_once('category-pages/Sport-category.php');
                    } elseif (in_category('Economie')) {
                        require_once('category-pages/Economie-category.php');
                    } elseif (in_category('Politiek')) {
                        require_once('category-pages/Politiek-category.php');
                    } elseif (in_category('Gemeente nieuws')) {
                        require_once('category-pages/Gemeente-category.php');
                    }
                ?>
    
    
    

    在本例中,我检查了每个帖子的类别,并为帖子加载了不同的模板文件,相应的类别排除了数组中的所有其他类别id

    <?php 
        $args = array('exclude'=> array("Enter Other categories ID here")); //which categories you dont want
        $$plan_group = get_terms('numbers_plans', $args);
    ?>
    
    
    
    我希望这将对您有所帮助

    
    
    <?php
         $catquery = new WP_Query( 'cat=3&posts_per_page=10' );
         while($catquery->have_posts()) : $catquery->the_post();
    ?>
    <ul>
     <li><h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php 
     the_title(); ?></a></h3>
    
    <ul><li><?php the_content(); ?></li>
    </ul>
    </li>
    </ul>
    <?php endwhile; ?>
    

    它正在工作,但任何创建新类别或删除类别显示新类别的帖子都将显示在页面中。所以我只想修复category post,或者只为$args数组指定特定的term_id