Как вытащить только одну категорию из пользовательского типа сообщения в шаблоне? - PullRequest
0 голосов
/ 21 апреля 2019

Я пытаюсь создать шаблон для пользовательского типа поста и извлекать только одну категорию из этого типа поста, называемого «заказано».Приведенный ниже код почему-то извлекает все сообщения из пользовательского типа.Что я делаю не так?

<?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 голосов
/ 22 апреля 2019

Работай!

Вот как я настраиваю свои аргументы.

            $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'
                )
            )
        );
0 голосов
/ 22 апреля 2019

Просто добавьте 'category_name' => 'commissioned' в массив $args (вместо cat => ...)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...