Ottenere custom post type per categoria in un template di pagina

16 lug 2011, 11:09:06
Visualizzazioni: 25.4K
Voti: 4

Ho creato un custom post type con categorie e sottocategorie, ho bisogno di elencare i titoli dei post e le immagini per una determinata sottocategoria o categoria in un template di pagina.

Sono riuscito ad ottenere tutti gli elementi elencati nel custom post type, ma non sono sicuro di come procedere oltre... ogni aiuto è apprezzato.

<?php 
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
?>

La funzione che crea il custom post type e la tassonomia è questa:-

<?php

// CUSTOM POST TYPE 1
add_action('init', 'portfolio_register');

function portfolio_register() {
    $args = array(
        'label' => __('Portfolio'),
        'singular_label' => __('Portfolio'),
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => true,
        'supports' => array('title', 'editor', 'thumbnail')
    );

    register_taxonomy("galleries", array("portfolio"), array(
        "hierarchical" => true, 
        "label" => "Gallerie", 
        "singular_label" => "Gallerie", 
        "rewrite" => true)
    );

    register_post_type( 'portfolio' , $args );
}


add_action("admin_init", "admin_init");
add_action('save_post', 'save_portfolio_options');
add_action('save_post', 'save_portfolio_single_options');

function admin_init(){
    add_meta_box("gallerymeta", "Opzioni Galleria", "portfolio_meta_options", "portfolio", "normal", "low");
    add_meta_box("portfoliometa", "Opzioni Elemento Portfolio", "portfolio_single_meta_options", "portfolio", "side", "low");
}

function portfolio_meta_options(){
    global $post;
    $custom = get_post_custom($post->ID);
    $excerpt = $custom["excerpt"][0];
    $info = $custom["info"][0];
    $linkto = $custom["linkto"][0];
?>
1
Commenti

Per tua informazione, register_post_type ha un argomento register_meta_box_cb che accetta una funzione di callback per registrare metabox applicabili a quel tipo (quindi non è necessario registrarle sull'azione init, che sarebbe comunque l'azione sbagliata su cui registrarle)..

t31os t31os
16 lug 2011 15:28:55
Tutte le risposte alla domanda 4
0

Questa è una versione di una funzione che sto utilizzando nel framework su cui sto lavorando, con un esempio HTML che sostituisce un'altra funzione che contiene qualcosa di simile.

// Loop Personalizzato

function arrr_custom_loop( $r_type = 'post', $r_post_num, $r_tax = 'category', $r_terms = 'featured' )  {
$args = array( 
    'showposts' => $r_post_num, 
    'tax_query' => array( 
        array( 
            'post_type' => $r_type,
            'taxonomy' => $r_tax, 
            'field' => 'slug', 
            'terms' => array( 
                $r_terms 
            ), 
        )
    )
);
query_posts( $args );
if (have_posts())
while ( have_posts() ) : the_post();
$more = 0;
?>
<article>
                <header class="pagetitle">
<?php if ( is_singular() )  { ?>
                    <h1><?php the_title(); ?></h1>
<?php } else { ?>
                    <h2 class="entry"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php } ?>
                </header>
                <div class="content_wrapper">
                    <div class="content">
<?php the_content(); ?>
                    </div>
                </div>
<?php if ( comments_open() && ! post_password_required() )  { ?>
                <div class="comments_wrapper">
                    <div class="comments">
<?php comments_template(); ?>
                    </div>
                </div>
<?php } ?>
        </article>
<?php endwhile;
wp_reset_query();
}

Quindi potresti semplicemente utilizzare la funzione con gli argomenti creati:

arrr_custom_loop( 'portfolio', 10, 'galleries', 'pirates' );
22 lug 2011 23:36:17
1

Puoi ottenerlo se hai/conosci lo slug della categoria/sottocategoria...

Basta passarlo nell'array args per query_posts.

$args = array( 'post_type' => 'portfolio', 'posts_per_page' => 10 ,'taxonomy_name'=>'slug della categoria/sottocategoria');

query_posts($args); 
16 lug 2011 11:19:49
Commenti

Non sembra funzionare, conosco gli slug, ma questo mostra tutti i post all'interno di portfolio. Ho pubblicato la funzione che crea il custom post type sopra, se può essere d'aiuto. Le categorie sono galleries e quella che sto cercando di estrarre è aerial.

toomanyairmiles toomanyairmiles
16 lug 2011 11:47:29
1

Ho usato questo codice in passato per fare più di ciò che ti serve, ma se rimuovi la seconda tassonomia elencherà tutti i post in una determinata categoria:

<?php
                    // imposta le tassonomie
                    $tax_one = 'project_category';
                    $tax_two = 'brand';
                    $post_type = 'project';

                    $categories = get_categories( array(
                        'type'                     => $post_type,
                        'orderby'                  => 'name',
                        'order'                    => 'ASC',
                        'hide_empty'               => 0,
                        'hierarchical'             => 1,
                        'taxonomy'                 => $tax_one ));

                        foreach ( $categories as $category ) : // elenca tutte le categorie

                            echo '<li><a href="'.get_term_link( $category->slug, $tax_one ).'">'.$category->name.'</a><ul>'?>

                                    <?php $terms = get_terms( $tax_two, array( 'hide_empty' => 0 ) );

                                            foreach ( $terms as $term ) :  // elenca tutti i brand in ogni categoria

                                                $myquery['tax_query'] = array(
                                                    array(
                                                        'taxonomy' => $tax_one,
                                                        'terms' => array($category->slug),
                                                        'field' => 'slug',
                                                    ),
                                                    array(
                                                        'taxonomy' => $tax_two,
                                                        'terms' => array($term->slug),
                                                        'field' => 'slug',
                                                    )
                                                        );
                                                $the_posts = new WP_Query($myquery);

                                                if ( $the_posts->have_posts() ) : // se ci sono post nel brand e categoria corrente, visualizzali

                                                    echo '<li><a href="'.get_term_link( $term->slug, $tax_two ).'">'.$term->name.'</a></li>';

                                                endif;

                                            endforeach; ?>

                            <?php echo '</ul></li>';

                        endforeach; ?>

Quindi adattandolo alla tua situazione:

<?php
                    // imposta le tassonomie
                    $tax_one = 'category';
                    $post_type = 'portfolio';

                    $categories = get_categories( array(
                        'type'                     => $post_type,
                        'orderby'                  => 'name',
                        'order'                    => 'ASC',
                        'hide_empty'               => 0,
                        'hierarchical'             => 1,
                        'taxonomy'                 => $tax_one ));

                        foreach ( $categories as $category ) : // elenca tutte le categorie

                            echo '<li><a href="'.get_term_link( $category->slug, $tax_one ).'">'.$category->name.'</a><ul>'?>

                                    <?php $the_posts = new WP_Query($myquery);

                                                if ( $the_posts->have_posts() ) : // se ci sono post nella categoria corrente, visualizzali

                                                foreach($the_posts as $post) :

                                                    echo '<li>Il tuo contenuto del post qui</li>';
                                                endforeach;                                    
19 lug 2011 09:12:42
Commenti

Mi dispiace, ma niente da fare.

toomanyairmiles toomanyairmiles
20 lug 2011 00:26:33
0

La risposta di Rajeev ha funzionato per me utilizzando il tuo codice. In un'installazione pulita, ho fatto quanto segue:

  1. creato un file functions.php contenente solo la tua funzione portfolio_register e l'hook add_action che la richiama
  2. creato due gallerie - "aerea" e "non aerea"
  3. creato quattro portfolio - "aerea 1", "aerea 2", "non aerea" e "nessuna galleria", assegnandoli alle gallerie come suggeriscono i nomi (o a nessuna, nel caso di "nessuna galleria")
  4. creato index.php contenente solo il primo blocco di codice che hai incollato. L'unica modifica che ho fatto è stata aggiungere 'galleries'=>'aerea' alla tua variabile $args:

$args = array( 'post_type' => 'portfolio', 'posts_per_page' => 10, 'galleries'=>'aerea' );

Sono stati visualizzati solo "aerea 1" e "aerea 2".

C'è la possibilità che il tuo codice non venga chiamato (forse un template diverso)?

21 lug 2011 19:42:42