首页 > 解决方案 > Wordpress 在帖子页面中显示自定义帖子类型特定类别?

问题描述

我是新开发的。我手动创建了一个自定义帖子类型,如下面的代码所示。我无法在自定义帖子类型中获取帖子表单特定类别(徽标、网站)。帮我解决这个问题。

function my_custom_taxonomies(){
    $lables = array(
        'name' => 'Type',
        'singular_name' => 'my_custom_taxonomiesType',

        'all_items' => 'All Types',
        'add_new_item' => 'Add Type',
        'edit_item' => 'Edit Type',
        'search_item' => 'Search  Type',
        'parent_item' => 'Parent Type',
        'parent_item_colon' => 'Parent Type',
        'update_item' => 'Update Type',
        'new_item_name' => 'New Type Name',
        'menu_name' => 'Type'
    );

    $args = array(
        'labels' => $lables,
        'query_var' => ture,
        'rewrite' => array('slug' => 'type'),
        'hierarchical' => true,
        'show_ui' => true,
        'show_admin_column' => true,

    );
    register_taxonomy('type', array('portfolio'), $args);
}

add_action('init','my_custom_taxonomies');

我正在尝试此代码,但无法使其正常工作。

$args = array( 'post_type' => 'portfolio', 'category_name' => 'logo', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <ul>
        <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
    </ul>

    <?php echo '<div class="entry-content">';
    //the_content();
    echo '</div>';
endwhile;?>

标签: wordpresscustom-post-type

解决方案


只需使用它,因为您已经注册了分类法。您可以在此处参考 WP_Query

$query = new WP_Query( array(
    'post_type' => 'portfolio',
    'tax_query' => array(
        array (
            'taxonomy' => 'type',
            'field' => 'slug',
            'terms' => 'logo',
        )
    ),
) );

推荐阅读