如何从模板中的自定义帖子类型中仅提取一个类别?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从模板中的自定义帖子类型中仅提取一个类别?相关的知识,希望对你有一定的参考价值。

我正在尝试为自定义帖子类型创建模板,并且只从该帖子类型中拉出一个名为“委托”的类别。由于某种原因,下面的代码从自定义帖子类型中提取所有帖子。我究竟做错了什么?

<?php
/**
 * Template Name: Portfolio Masonry Commissioned
 *
 * @package Ridge
 * @since 1.0
 */

get_header();

?>

    <div id="primary" class="content-area middle portfolio">
        <main id="main" class="site-main " role="main">

            <?php

            // Don't show the description for the front page
            if( is_page() ) {

                while ( have_posts() ) : the_post(); ?>

                    <div id="portfolio-content">
                        <header class="entry-header">
                            <h1 class="entry-title"><?php the_title(); ?></h1>
                        </header>
                        <?php the_content(); ?>
                    </div>

                <?php endwhile;
            } // if

            /**
             * Set up the skills and projects
             *
             * @see inc/template-tags.php
             */

            // Get the projects WP_Query object
            $args = array(  
                'post_type' => 'project',
                'post_status' => 'publish',
                'posts_per_page' => -1, 
                'orderby' => 'date', 
                'order' => 'ASC',
                'cat' => 'commissioned',
            );

            $loop = new WP_Query( $args );

            ?>

            <div id="projects" class="masonry">
                <div class="thumbs clearfix">
                    <?php  while ( $loop->have_posts()) : $loop->the_post();

                        global $ttrust_config;

                        // Get the skills for each project for the .js data attribute
                        $skills = ridge_get_tax( $post );

                        get_template_part( 'content', 'project-small-masonry' ); ?>

                    <?php endwhile; ?>

                    <?php wp_reset_postdata(); ?>

                </div><!-- .thumbs -->
            </div><!-- #projects -->

        </main><!-- .site-main -->
    </div><!-- .content-area -->

<?php get_footer(); ?>

更新:这是项目CPT的设置方式。

    public function project_init() {

    // Define the settings
    $settings = array(
        'labels'             => array(
            'name'               => __( 'Projects', $this->textdomain ),
            'singular_name'      => __( 'Project', $this->textdomain ),
            'add_new'            => __( 'Add New', $this->textdomain ),
            'add_new_item'       => __( 'Add New Project', $this->textdomain ),
            'edit'               => __( 'Edit', $this->textdomain ),
            'edit_item'          => __( 'Edit Project', $this->textdomain ),
            'new_item'           => __( 'New Project', $this->textdomain ),
            'view'               => __( 'View Project', $this->textdomain ),
            'view_item'          => __( 'View Project', $this->textdomain ),
            'search_items'       => __( 'Search Projects', $this->textdomain ),
            'not_found'          => __( 'No projects found', $this->textdomain ),
            'not_found_in_trash' => __( 'No projects found in Trash', $this->textdomain ),
            'parent'             => __( 'Parent Project', $this->textdomain ),
        ),
        'public'                 =>true,
        'publicly_queryable'     => true,
        'show_ui'                => true,
        'query_var'              => true,
        'capability_type'        => 'post',
        'hierarchical'           => false,
        'menu_position'          => null,
        'menu_icon'              => get_template_directory_uri(). '/img/blue-folder-stand.png',
        'taxonomies'             => array( 'skills' ),
        'supports'               => array( 'title', 'editor', 'thumbnail', 'comments', 'revisions', 'excerpt' ),
        'rewrite'                => array(
            'slug' => 'project'
        )
    ); // End $settings

更新:搞定了!

            $args = array(  
            'post_type' => 'project',
            'post_status' => 'publish',
            'posts_per_page' => -1, 
            'orderby' => 'date', 
            'order' => 'ASC',
            'tax_query' => array(
                array (
                    'taxonomy' => 'skill',
                    'field' => 'slug',
                    'terms' => 'commissioned'
                )
            )
        );
答案

只需将'category_name' => 'commissioned'添加到$args数组(而不是cat => ...

另一答案

搞定了!

这是我如何设置我的论点。

            $args = array(  
            'post_type' => 'project',
            'post_status' => 'publish',
            'posts_per_page' => -1, 
            'orderby' => 'date', 
            'order' => 'ASC',
            'tax_query' => array(
                array (
                    'taxonomy' => 'skill',
                    'field' => 'slug',
                    'terms' => 'commissioned'
                )
            )
        );

以上是关于如何从模板中的自定义帖子类型中仅提取一个类别?的主要内容,如果未能解决你的问题,请参考以下文章

如何:在wordpress中自定义帖子类型的自定义类别存档页面

php 通过acf选择字段查询类别中的自定义帖子类型

php 通过acf选择字段查询类别中的自定义帖子类型

从 Wordpress 中自定义帖子类型的类别中获取 ACF 文本字段值

如何在具有帖子类型的自定义页面上显示所有图像字段

php 基本WP_Query模板,用于显示每个类别的自定义帖子选择