1

I am currently trying to add a list of categories that a custom post is in (only have 3 categories). by using the code below, I have managed to output ALL categories into a list but what am I missing to filter just the categories that that post is in... Been stuck for days!

Here is the link to better explain - http://mgmtphdjobs.com/manage-jobs/ As you see... Below each post ALL 3 categories are listed, I need only to show the categories the post is in and not the others

Thanks

<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 ) ); ?>

  • Can you post the code for the get_job_listing_categories() function please? – Steve North Oct 13 '16 at 13:22
  • I have no idea where that would be... – Aaron Bradford Oct 13 '16 at 13:25
  • I originally took the code from this thread - wordpress.stackexchange.com/questions/10387/… and edited it – Aaron Bradford Oct 13 '16 at 13:26
  • ignore that... function get_job_listing_categories() { if ( ! get_option( 'job_manager_enable_categories' ) ) { return array(); } return get_terms( "job_listing_category", array( 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => false, ) ); } – Aaron Bradford Oct 13 '16 at 13:28
1

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 ) ); ?>
|improve this answer|||||
  • can i add $post_id = get_the_ID(); to the 'ID IN HERE' ? – Aaron Bradford Oct 13 '16 at 13:51
  • Yeah or you can just do it like so: get_job_listing_categories(get_the_id()); – Steve North Oct 13 '16 at 13:56
  • returns nothing... – Aaron Bradford Oct 13 '16 at 13:59
  • Are you using this inside the loop? Try this and copy what it returns... var_dump(get_the_id()); – Steve North Oct 13 '16 at 14:04
  • Use this inside the function: $job->ID instead of get_the_id – Steve North Oct 13 '16 at 14:07
2

It doesn't matter that you're trying to pull taxonomy from a CPT, you can use wp_get_post_terms

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

foreach ( $terms as $term ) {
     echo $term->name . '<br />';
}

In your case, $taxonomy being 'category'.

This should pull only the taxonomy terms for that post, or in this case, job listing.

|improve this answer|||||
  • Comments are not for extended discussion; this conversation has been moved to chat. – fuxia Oct 13 '16 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.