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

时间:2019-04-21 14:49:02

标签: wordpress custom-post-type custom-wordpress-pages

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

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

让它正常工作!

这是我设置论点的方式。

            $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'
                )
            )
        );