Visualizzare il nome della categoria di un tipo di post personalizzato

22 mag 2015, 06:08:18
Visualizzazioni: 81.9K
Voti: 5

Ho una query personalizzata, in cui sto visualizzando alcuni post risultanti da un tipo di post personalizzato chiamato "staff". Questo custom post type è collegato a una tassonomia personalizzata chiamata "department". Riesco a visualizzare i risultati, ma non riesco a stampare la categoria collegata a ciascun post.

Questo è il mio codice:

        <?php
          $args = array(
            'post_type' => 'staff', 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC',
            'tax_query' => array(
              array(
                'taxonomy' => 'departments',
                'field' => 'slug',
                'terms' => 'board'
              )
            )
          );
          $loop = new WP_Query( $args );
        ?>

        <?php if( $loop->have_posts() ): ?>

            <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

                    <p class="text-center name"><?php the_title(); ?></p>
                    <?php the_category(' '); ?>

            <?php endwhile; ?>        

        <?php endif; ?>

Penso che il problema sia che sto usando the_category ma non sono sicuro.

Qualche idea su cosa potrebbe essere sbagliato?

Grazie!

1
Commenti

I tipi di post personalizzati non hanno categorie. Sembra che tu voglia recuperare i termini personalizzati della tassonomia "departments". Tuttavia la tua query è già impostata per selezionare solo il dipartimento "board", quindi non ha senso recuperarlo. Se vuoi comunque recuperarlo, dai un'occhiata alla funzione get_the_terms https://codex.wordpress.org/Function_Reference/get_the_terms

gdaniel gdaniel
22 mag 2015 06:39:34
Tutte le risposte alla domanda 4
1
15

Quindi questo è ciò di cui avevo bisogno:

<?php
$terms = get_the_terms( $post->ID , 'board' );
foreach ( $terms as $term ) {
echo $term->name;
}
?>
22 mag 2015 06:42:13
Commenti

GRAZIE! Mi hai guidato a scoprire qual era il mio problema. Molto apprezzato.

Debbie Kurth Debbie Kurth
4 lug 2019 19:55:36
0

utilizza termini come questo:

$terms = get_the_terms($post->ID, 'Inserisci_la_tua_tassonomia_qui' );
if ($terms && ! is_wp_error($terms)) :
    $tslugs_arr = array();
    foreach ($terms as $term) {
        $tslugs_arr[] = $term->slug;
    }
    $terms_slug_str = join( " ", $tslugs_arr);
endif;
echo $terms_slug_str;
27 dic 2016 00:51:14
1

Nel caso qualcuno stia cercando questa soluzione nel 2019. Con questo codice otterrai il nome del CUSTOM POST TYPE con l'URL

$terms = wp_get_post_terms( $post->ID, 'INSERISCI-QUI-LA-TUA-TASSONOMIA');
foreach ( $terms as $term ) {
  $term_link = get_term_link( $term );
  echo '<a href="' . $term_link . '">' . $term->name . '</a>' . ' ';
}
21 apr 2019 07:50:40
Commenti

Questa dovrebbe essere la risposta corretta.

Nuno Sarmento Nuno Sarmento
20 nov 2020 00:48:35
0

$categories = get_terms(array( 'taxonomy' => 'course_categories', 'hide_empty' => false, ));

        $output .= '<select name="category">';
        $output .= '<option value="">Seleziona Categoria</option>';
        foreach ($categories as $category) {
            
            $output .= '<option value="' . $category->slug . '">' . $category->name . '</option>';
            
        }

if (!empty($category_data)) { // Se è selezionata una categoria, filtra per quella categoria $post['tax_query'] = array( array( 'taxonomy' => 'course_categories', 'field' => 'slug', 'terms' => $category_data, 'operator' => 'IN', ), ); }

6 dic 2024 12:36:41