如何根据wordpress中的类别显示自定义帖子类型的帖子

时间:2017-05-16 07:31:55

标签: wordpress

我为节目计划创建了自定义帖子类型,我想根据类别显示帖子。我在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 );
}

对于节目计划,我创建了带有密码的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();
}
?>

它显示所有类别的帖子,但我想只显示一个类别,它的帖子。请告诉我我该怎么做。

3 个答案:

答案 0 :(得分:0)

从某个category中选择帖子的一种方法是在帖子输出周围创建一个小if statement。请参阅以下示例:

<?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');
                }
            ?>

在此示例中,我检查其上的每个帖子的类别,并为具有相应类别的帖子加载不同的模板文件

答案 1 :(得分:0)

排除数组中所有其他类别的ID。

<?php 
    $args = array('exclude'=> array("Enter Other categories ID here")); //which categories you dont want
    $$plan_group = get_terms('numbers_plans', $args);
?>

我希望这会对你有所帮助

答案 2 :(得分:0)

    var backgroundImageName = ""
    switch UIScreen.main.bounds.height {
        case 480:
            //for iPhone4s,
            backgroundImageName = "background_iPhone4s"
            break;
        case 568:
            //iPhone SE, iPhone5, iPhone 5s
            backgroundImageName = "background_iPhone5"
            break;
        case 667:
            //iPhone 6, 6s, 7
            backgroundImageName = "background_iPhone6"
            break;
        case 736:
            //iPhone 6 plus, 6s plus, 7 plus
            backgroundImageName = "background_iPhonePlus"
            break;
        default:
            break;
    }
    backgroundImageView.image = UIImage(named: backgroundImageName)