首页 > 解决方案 > 获取特定类别中的特定自定义类型帖子 - wordpress

问题描述

我有一个问题,我有一个名为 position 的自定义类型帖子(作者)和分类法(经理和团队)我需要让团队的所有作者都在一个页面上,并且需要经理在他们中的第一个

作者按姓名排序,但经理姓名以"T" 我如何处理这种情况开头。我的代码是

    <?php
    /**
     * The template for displaying auther custom type on custom taconomy position filterd By the page name and position name
     * Template Name: taxonomies
     *
     */
    
    defined( 'ABSPATH' ) || exit; // Exit if accessed directly
    
    get_header(); ?>
    
    <?php
    // echo the $slug name of the page
    $page_names = get_the_title(); ?>
    
    <section id="articles" class="articles">
            
        <div class="<?php echo $classcase ?>">
    <?php
    $loop = new WP_Query( array( 'post_type'=>'authors','posts_per_page' => '-1','orderby' => 'title','order'=>'ASC' ) );
    if ( $loop->have_posts() ) :
    while($loop->have_posts()): $loop->the_post(  );
                    $terms = get_the_terms( $post->ID, 'position' );
                    foreach ( $terms as $term ) {
                        if($term->name == $page_names) { ?>
                           <div class="tax-container">
                           <a href="<?php the_permalink(  );?>">
                            <div class="parent-before">
                                <img class="tax-img" src=" <?php the_post_thumbnail_url(  ); ?>  " data-tool-tip="<?php the_title(  );?>" />
                                
                <div class="tax-div-p"  data-tool-tip="<?php the_title(  );?>">
                <p class="tax-p"  data-tool-tip="<?php the_title(  );?>"></p>
                                </div>
                            </div>
                        </a>
                    </div>
    <?php   } } ?>
                    
    <?php endwhile; wp_reset_query(); endif; ?>
        
        </div>
    </section>
 
    <div class='endauthors'></div>

标签: phpwordpresscustom-post-type

解决方案


对于此任务,您需要使用 tax_query https://developer.wordpress.org/reference/classes/wp_tax_query/

首先我们按职称排序所有经理,然后是整个团队

试试这个代码

    <?php
    /**
     * The template for displaying auther custom type on custom taconomy position filterd By the page name and position name
     * Template Name: taxonomies
     *
     */
    
    defined( 'ABSPATH' ) || exit; // Exit if accessed directly
    
    get_header(); ?>
    
    <?php
    // echo the $slug name of the page
    $page_names = get_the_title(); ?>
    
    <section id="articles" class="articles">
            
        <div class="<?php echo $classcase ?>">
<?php
    $args = array(
    'post_type' => 'authors',
    'posts_per_page' => '-1',
    'orderby' => 'title',
    'order'=>'ASC'
    'tax_query' => array(
        array(
            'taxonomy' => 'position',
            'field'    => 'slug',
            'terms'    => array( 'manager' )
        )
    )
);
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) :
    while($loop->have_posts()): $loop->the_post(  );
                    $terms = get_the_terms( $post->ID, 'position' );
                    foreach ( $terms as $term ) {
                        if($term->name == $page_names) { ?>
                           <div class="tax-container">
                           <a href="<?php the_permalink(  );?>">
                            <div class="parent-before">
                                <img class="tax-img" src=" <?php the_post_thumbnail_url(  ); ?>  " data-tool-tip="<?php the_title(  );?>" />
                                
                <div class="tax-div-p"  data-tool-tip="<?php the_title(  );?>">
                <p class="tax-p"  data-tool-tip="<?php the_title(  );?>"></p>
                                </div>
                            </div>
                        </a>
                    </div>
    <?php   } } ?>
                    
    <?php endwhile; wp_reset_query(); endif; ?>
        
<?php
    $args = array(
    'post_type' => 'authors',
    'posts_per_page' => '-1',
    'orderby' => 'title',
    'order'=>'ASC'
    'tax_query' => array(
        array(
            'taxonomy' => 'position',
            'field'    => 'slug',
            'terms'    => array( 'team' )
        )
    )
);
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) :
    while($loop->have_posts()): $loop->the_post(  );
                    $terms = get_the_terms( $post->ID, 'position' );
                    foreach ( $terms as $term ) {
                        if($term->name == $page_names) { ?>
                           <div class="tax-container">
                           <a href="<?php the_permalink(  );?>">
                            <div class="parent-before">
                                <img class="tax-img" src=" <?php the_post_thumbnail_url(  ); ?>  " data-tool-tip="<?php the_title(  );?>" />
                                
                <div class="tax-div-p"  data-tool-tip="<?php the_title(  );?>">
                <p class="tax-p"  data-tool-tip="<?php the_title(  );?>"></p>
                                </div>
                            </div>
                        </a>
                    </div>
    <?php   } } ?>
                    
    <?php endwhile; wp_reset_query(); endif; ?>
        </div>
    </section>
 
    <div class='endauthors'></div>

推荐阅读