Ottenere custom post type per tag

7 set 2011, 17:03:31
Visualizzazioni: 27.1K
Voti: 5

È possibile recuperare le voci dei custom post type tramite tag? Ho provato con il seguente codice, ma finisco in un loop infinito.

<aside class="supporting_images">
    <?php /*<?php if($post->ID == 241) : echo apply_filters('the_content', '[slideshow=3]'); endif;  ?>
    <?php the_post_thumbnail(); ?>*/?>

    <?php if($post->ID == 241) : ?>
        <?php
            $query = new WP_Query();
            $query->query('tag=branding');
        ?>
        <?php while ($query->have_posts()) : ?>
            ciao
        <?php endwhile; ?>
    <?php endif;?>

0
Tutte le risposte alla domanda 3
0

Dovrai configurare il post per la query modificando la seguente riga per evitare il loop infinito.

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

Se stai cercando un custom post type, dovrai specificarlo negli argomenti della query:

<?php $query = new WP_Query( array( "post-type" => "yourposttype", "tag" => "branding" ) ); ?>

Puoi vedere la maggior parte (se non tutti) i parametri della query nel codex. http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

7 set 2011 18:25:56
0

Utilizza tag_slug__in invece di tag (#REF)

Esempio:

$query = new WP_Query(array(
   'post_status' => 'publish', // stato del post
   'post_type' => 'your-posttype', // oppure 'any' per qualsiasi tipo
   'tag_slug__in' => 'your-tag', // slug del tag da filtrare
   'posts_per_page' => -1 // mostra tutti i post
));

Passaggio: Filtra per Tipo di Post prima poi filtra per Tag

Altre risposte suggeriscono "Filtra per Tipo di Post e filtra per Tag"

23 set 2015 14:17:08
0

Per evitare un loop infinito, è necessario iterare sui post risultanti:

<?php
$query = new WP_Query(
    array( "post_type" => "yourposttype", // non "post-type" !
           "tag" => "branding"
    ) );
while ($query->have_posts()) : $query->the_post(); ?>
    Ciao
<?php endwhile; ?>
8 nov 2011 11:44:35