向php函数返回当前帖子的类别数组

时间:2016-10-13 作者:Aaron Bradford

我目前正在尝试添加自定义帖子所在的类别列表(只有3个类别)。通过使用下面的代码,我成功地将所有类别输出到一个列表中,但我缺少什么来过滤该帖子所在的类别。。。被困了好几天了!

以下链接可以更好地解释-http://mgmtphdjobs.com/manage-jobs/正如你所看到的。。。每个帖子下面都列出了所有3个类别,我只需要显示帖子所在的类别,而不需要显示其他类别

谢谢

<div id="job-manager-job-dashboard">
<h3><?php _e( \'Job listings are shown in the table below.\', \'wp-job-manager\'           ); ?></h3>
<table class="job-manager-jobs">
    <thead>
        <tr>
            <?php foreach ( $job_dashboard_columns as $key => $column ) : ?>
                <th class="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $column ); ?></th>
            <?php endforeach; ?>
        </tr>
    </thead>
    <tbody>
        <?php if ( ! $jobs ) : ?>
            <tr>
                <td colspan="6"><?php _e( \'You do not have any active   listings.\', \'wp-job-manager\' ); ?></td>
            </tr>
        <?php else : ?>
            <?php foreach ( $jobs as $job ) : ?>
                <tr>
                    <?php foreach ( $job_dashboard_columns as $key => $column ) : ?>
                        <td class="<?php echo esc_attr( $key ); ?>">
                            <?php if (\'job_title\' === $key ) : ?>
                                <?php if ( $job->post_status == \'publish\' ) : ?>
                          <a href="<?php echo get_permalink( $job->ID ); ?>"><?php echo $job->post_title; ?></a><br> Status: 

                        <br><?php $post_id = get_the_ID();

$terms = wp_get_post_terms( $post_id, \'category\' );

foreach ( $terms as $term ) {
 echo $term->name;
}
?>
<?php else : ?>

                                    <?php echo $job->post_title; ?> <small>  (<?php the_job_status( $job ); ?>)</small>
                                <?php endif; ?>
                                <ul class="job-dashboard-actions">
                                   <?php
                                        $actions = array();

                                        switch ( $job->post_status ) {
                                            case \'publish\' :
                                                $actions[\'edit\'] = array( \'label\' => __( \'Edit\', \'wp-job-manager\' ), \'nonce\' => false );


                                                if ( is_position_filled( $job ) ) {
                                                    $actions[\'mark_not_filled\'] = array( \'label\' => __( \'Not filled\', \'wp-job-manager\' ), \'nonce\' => true );
                                                } else {
                                                    $actions[\'mark_filled\'] = array( \'label\' => __( \'Filled\', \'wp-job-manager\' ), \'nonce\' => true );
                                                }


                                                break;
                                            case \'pending_payment\' :
                                            case \'pending\' :
                                                if ( job_manager_user_can_edit_pending_submissions() ) {
                                                    $actions[\'edit\'] = array( \'label\' => __( \'Edit\', \'wp-job-manager\' ), \'nonce\' => false );
                                                }
                                            break;
                                        }

                                        $actions[\'delete\'] = array( \'label\' => __( \'Delete\', \'wp-job-manager\' ), \'nonce\' => true );
                                        $actions           = apply_filters( \'job_manager_my_job_actions\', $actions, $job );

                                        foreach ( $actions as $action => $value ) {
                                            $action_url = add_query_arg( array( \'action\' => $action, \'job_id\' => $job->ID ) );
                                            if ( $value[\'nonce\'] ) {
                                                $action_url = wp_nonce_url(    $action_url, \'job_manager_my_job_actions\' );
                                            }
                                            echo \'<li><a href="\' . esc_url( $action_url ) . \'" class="job-dashboard-action-\' . esc_attr( $action ) . \'">\' . esc_html( $value[\'label\'] ) . \'</a></li>\';
                                        }
                                    ?>
                                </ul>
                            <?php elseif (\'date\' === $key ) : ?>
                                <?php echo date_i18n( get_option( \'date_format\' ), strtotime( $job->post_date ) ); ?>
                            <?php elseif (\'expires\' === $key ) : ?>
                                <?php echo $job->_job_expires ? date_i18n( get_option( \'date_format\' ), strtotime( $job->_job_expires ) ) : \'&ndash;\'; ?>
                            <?php elseif (\'filled\' === $key ) : ?>
                                <?php echo is_position_filled( $job ) ? \'&#10004;\' : \'&ndash;\'; ?>
                            <?php else : ?>
                                <?php do_action( \'job_manager_job_dashboard_column_\' . $key, $job ); ?>
                            <?php endif; ?>
                        </td>
                    <?php endforeach; ?>
                </tr>
            <?php endforeach; ?>
        <?php endif; ?>
    </tbody>
</table>
<?php get_job_manager_template( \'pagination.php\', array( \'max_num_pages\' => $max_num_pages ) ); ?>

2 个回复
最合适的回答,由SO网友:Steve 整理而成

EDITS:

<div id="job-manager-job-dashboard">
    <h3><?php _e( \'Job listings are shown in the table below.\', \'wp-job-manager\'           ); ?></h3>
    <table class="job-manager-jobs">
        <thead>
            <tr>
                <?php foreach ( $job_dashboard_columns as $key => $column ) : ?>
                    <th class="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $column ); ?></th>
                <?php endforeach; ?>
            </tr>
        </thead>
        <tbody>
            <?php if ( ! $jobs ) : ?>
                <tr>
                    <td colspan="6"><?php _e( \'You do not have any active   listings.\', \'wp-job-manager\' ); ?></td>
                </tr>
            <?php else : ?>
                <?php foreach ( $jobs as $job ) : ?>
                    <tr>
                        <?php foreach ( $job_dashboard_columns as $key => $column ) : ?>
                            <td class="<?php echo esc_attr( $key ); ?>">
                                <?php if (\'job_title\' === $key ) : ?>
                                    <?php if ( $job->post_status == \'publish\' ) : ?>
                              <a href="<?php echo get_permalink( $job->ID ); ?>"><?php echo $job->post_title; ?></a><br> Status: 

                            <br />
    <?php

    $terms = wp_get_post_terms( $job->ID, \'job_listing_category\' );

    foreach ( $terms as $term ) {
     echo $term->name;
    }
    ?>

    <?php else : ?>

                                    <?php echo $job->post_title; ?> <small>  (<?php the_job_status( $job ); ?>)</small>
                                <?php endif; ?>
                                <ul class="job-dashboard-actions">
                                   <?php
                                        $actions = array();

                                        switch ( $job->post_status ) {
                                            case \'publish\' :
                                                $actions[\'edit\'] = array( \'label\' => __( \'Edit\', \'wp-job-manager\' ), \'nonce\' => false );


                                                if ( is_position_filled( $job ) ) {
                                                    $actions[\'mark_not_filled\'] = array( \'label\' => __( \'Not filled\', \'wp-job-manager\' ), \'nonce\' => true );
                                                } else {
                                                    $actions[\'mark_filled\'] = array( \'label\' => __( \'Filled\', \'wp-job-manager\' ), \'nonce\' => true );
                                                }


                                                break;
                                            case \'pending_payment\' :
                                            case \'pending\' :
                                                if ( job_manager_user_can_edit_pending_submissions() ) {
                                                    $actions[\'edit\'] = array( \'label\' => __( \'Edit\', \'wp-job-manager\' ), \'nonce\' => false );
                                                }
                                            break;
                                        }

                                        $actions[\'delete\'] = array( \'label\' => __( \'Delete\', \'wp-job-manager\' ), \'nonce\' => true );
                                        $actions           = apply_filters( \'job_manager_my_job_actions\', $actions, $job );

                                        foreach ( $actions as $action => $value ) {
                                            $action_url = add_query_arg( array( \'action\' => $action, \'job_id\' => $job->ID ) );
                                            if ( $value[\'nonce\'] ) {
                                                $action_url = wp_nonce_url(    $action_url, \'job_manager_my_job_actions\' );
                                            }
                                            echo \'<li><a href="\' . esc_url( $action_url ) . \'" class="job-dashboard-action-\' . esc_attr( $action ) . \'">\' . esc_html( $value[\'label\'] ) . \'</a></li>\';
                                        }
                                    ?>
                                </ul>
                            <?php elseif (\'date\' === $key ) : ?>
                                <?php echo date_i18n( get_option( \'date_format\' ), strtotime( $job->post_date ) ); ?>
                            <?php elseif (\'expires\' === $key ) : ?>
                                <?php echo $job->_job_expires ? date_i18n( get_option( \'date_format\' ), strtotime( $job->_job_expires ) ) : \'&ndash;\'; ?>
                            <?php elseif (\'filled\' === $key ) : ?>
                                <?php echo is_position_filled( $job ) ? \'&#10004;\' : \'&ndash;\'; ?>
                            <?php else : ?>
                                <?php do_action( \'job_manager_job_dashboard_column_\' . $key, $job ); ?>
                            <?php endif; ?>
                        </td>
                    <?php endforeach; ?>
                </tr>
            <?php endforeach; ?>
        <?php endif; ?>
    </tbody>
</table>
<?php get_job_manager_template( \'pagination.php\', array( \'max_num_pages\' => $max_num_pages ) ); ?>
SO网友:socki03

无论您试图从CPT中提取分类法,都可以使用wp_get_post_terms

$terms = wp_get_post_terms( $job->ID, \'job_listing_category\' );

foreach ( $terms as $term ) {
     echo $term->name . \'<br />\';
}
在您的情况下,$分类法是“类别”。

这应该只提取该帖子的分类术语,或者在本例中是作业列表。