我如何从一个wp-查询中显示分类信息?

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

我有一个wp-query,在我的网站的仪表板的一小部分上工作得很好--它显示了一个6周后到期的重复工作(CPT)的列表--我在显示与该职位相关的分类信息时遇到了问题。

到目前为止,我已经得到了

<?php 

// get posts
$before_date = date("Ymd", strtotime("+6 weeks"));

$posts = get_posts(array(
    'post_type'         => 'pre_jobs',
    'posts_per_page'        => -1,
    'meta_key'          => 'pre_job_due_date',
    'orderby'           => 'meta_value',
    'order'             => 'ASC',
   'meta_query' => array(
        'relation'      => 'AND',
        array(
     'key' => 'pre_job_due_date',
     'value' => $before_date, 
     'compare' => '<', 
     ),
   ),
    'tax_query' => array(
        array(
            'taxonomy' => 'pre_job_status',
            'field' => 'slug',
            'terms' => array( 'repeat' )
        ),
    ),
));

if( $posts ): ?>

    <hr>
    <div class="dashpanel">
    <div class="duedate-head">Due Date</div>
    <div class="jobnumber-head">Job Type</div>
    <div class="client-head">Client/Requestor</div>
    <div class="customer-head">Customer</div>
</div>
<hr>


    <?php foreach( $posts as $post ): 

        setup_postdata( $post )

        ?>
        <?php $job_type = get_field('pre_job_job_type', $client->ID ); ?>
        <?php $customer = get_field('pre_job_customer', $client->ID ); ?>
        <?php $job_client = get_field('pre_job_requestor', $client->ID ); ?>

        <div class="dashpanel">
    <a href="<?php the_permalink(); ?>">
    <div class="duedate"><?php the_field('pre_job_due_date'); ?></div>
    <div class="jobnumber"><?php echo $job_type[0]->post_title; ?></div>
    <div class="client"><?php echo $job_client[0]->post_title; ?></div>
    <div class="customer"><?php echo $customer[0]->post_title; ?></div></a>
    </div>
    <hr>


    <?php endforeach; ?>



    <?php wp_reset_postdata(); ?>
<?php else : ?>
<p>No upcoming jobs to book in.</p>
<?php endif; ?>

我不知道我需要在哪里放上

<?php 
$pre_job_type = get_field('pre_job_job_type');
if( $term ): ?>

代码--每次我添加这段代码时,它都会中断。还是我哪里做错了?

php wordpress advanced-custom-fields
1个回答
0
投票

你可以在打开后添加这段代码 if ($posts) 循环。它将显示这个出版物相关的分类学。 如果你愿意,你可以删除链接,只显示术语的标题与调用。$term->name

<?php
$terms = get_the_terms( $post->ID, 'YOUR_TAXONOMY_HERE' );
if ( $terms != null ) {
    foreach ( $terms as $term ) {
        $term_link = get_term_link( $term, 'YOUR_TAXONOMY_HERE' );
        echo '<li><a href="'.esc_url( site_url() ) . '/your-taxonomy-here/' . $term->slug . '">' . $term->name . ' ' . $term->term_id . ' ' . $term->count . '</a></li>';
        unset( $term );
    }
}
?>

0
投票

您是否创建了您所期望的分类系统?如果还没有,请先在您的function.php文件或您创建的自定义帖子类型中创建分类。

例子:

/**
 * Register a private 'Genre' taxonomy for post type 'book'.
 *
 * @see register_post_type() for registering post types.
 */
function wpdocs_register_taxonomy() {
    $args = array(
        'label'        => __( 'Job Type', 'textdomain' ),
        'public'       => true,
        'rewrite'      => true,
        'hierarchical' => true
    );

    register_taxonomy( 'pre_job_job_type', 'pre_jobs', $args );
}
add_action( 'init', 'wpdocs_register_taxonomy', 0 );

或OOP方法。

public function wpdocs_register_taxonomy() {
        $args = array(
            'label'        => __( 'Job Type', 'textdomain' ),
            'public'       => true,
            'rewrite'      => true,
            'hierarchical' => true
        );

        register_taxonomy( 'pre_job_job_type', 'pre_jobs', $args );
    }

/**
* When class is instantiated
*/

public function __construct() {

    add_action('init', array($this, 'wpdocs_register_taxonomy'));// Register texonomy

}

现在你的分类法是 可供展示. 把这段代码放在你想显示分类法的地方。

$pre_job_types = get_categories('taxonomy=pre_job_job_type&post_type=pre_jobs'); 
foreach ($pre_job_types as $job_type ) : ?>  
     <span> <?php echo $job_type->name; </span>
endforeach; 

如果你想用分类法查询你的自定义帖子,请按照以下步骤进行。

               $args = array(
                    'post_type' => 'pre_jobs',
                    'status' => 'published',
                    'tax_query' => array(
                      array(
                        'taxonomy' => 'pre_job_job_type',
                        'field' => 'name',
                        'terms' => array( 'repeat' )
                      ) 
                    ) 
                  );
                  $query = new WP_Query( $args );

然后继续正常查询 这个查询只显示分类法为 "repeat "的帖子。

© www.soinside.com 2019 - 2024. All rights reserved.