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

问题描述 投票:0回答:2

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

<?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'
                )
            )
        );
wordpress custom-post-type custom-wordpress-pages
2个回答
0
投票

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


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'
                )
            )
        );
© www.soinside.com 2019 - 2024. All rights reserved.