Visualizzare le categorie da un custom post type
18 feb 2011, 12:59:53
Visualizzazioni: 69.9K
Voti: 4
Ho creato un custom post type 'Portfolio' e sotto questo tipo di post ho creato delle categorie Portfolio con il seguente codice
functions.php - questo è il codice per il custom post type che ho definito nella pagina functions.php
function demo_register_post_type() {
register_post_type('Portfolio', array(
'labels' => array(
'name' => __('Portfolio'),
'singular_name' => __('Portfolio'),
'add_new' => 'Aggiungi Nuovo Portfolio',
'edit_item' => 'Modifica Portfolio',
'new_item' => 'Nuovo Portfolio',
'view_item' => 'Visualizza Portfolio',
'search_items' => 'Cerca Portfolio',
'not_found' => 'Nessun Portfolio trovato',
'not_found_in_trash' => 'Nessun Portfolio trovato nel Cestino',
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => true,
'query_var' => true
),
'menu_position' => 6,
'public' => true,
'supports' => array(
'title','editor','author','thumbnail','excerpt','comments','page-attributes'
),
'rewrite' => array( 'slug' => 'portfolio', 'with_front' => false ),
'taxonomy' => array('category', 'post_tag')
));
register_taxonomy( 'portfolio-category', 'Portfolio', array ('hierarchical' => true, 'label' => __('Categorie Portfolio'))); // categorie portfolio
Ora voglio recuperare tutte le categorie da questo tipo di post e visualizzare i post dalle categorie sulla pagina?
Per favore, fatemi sapere come posso farlo?
Grazie.

User
100
Commenti
Tutte le risposte alla domanda
3
0
Che ne dici di usare get_terms()?
Un esempio rapido:
$terms = get_terms('portfolio-category');
foreach ( $terms as $term ) {
echo $term->name.'<br />';
}

Kaaviar
506
18 feb 2011 13:54:03
0
Per ottenere i post del tuo custom post type devi interrogare post_type e puoi farlo così:
<?php query_posts(array( 'post_type' => 'Portfolio' )); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="post">
<!-- Mostra il Titolo come link al permalink del Post. -->
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Link Permanente a <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<!-- Mostra la data (formato 16 Novembre 2009) e un link agli altri post di questo autore. -->
<small><?php the_time('j F, Y') ?> di <?php the_author_posts_link() ?></small>
<div class="entry">
<?php the_content(); ?>
</div>
<p class="postmetadata">Pubblicato in <?php the_category(', '); ?></p>
</div> <!-- chiude il primo div box -->
<?php endwhile; else: ?>
<p>Spiacenti, nessun post corrisponde ai tuoi criteri.</p>
<?php endif; ?>
Se invece vuoi ottenere i post Portfolio di un termine specifico nella tua tassonomia personalizzata allora aggiungi l'argomento taxonomy all'array di query_posts in questo modo:
<?php query_posts(array( 'post_type' => 'Portfolio','portfolio-category' => 'nome-categoria' )); ?>
Spero sia utile.

Bainternet
67.7K
18 feb 2011 13:42:37
Domande correlate
8
risposte
1
risposte