Come richiamare un custom post type con WP_Query

27 giu 2017, 09:29:24
Visualizzazioni: 31K
Voti: 1

Come posso richiamare un custom post type con WP_Query?

Questo è il mio custom post type. Come posso visualizzarlo nel mio codice?

<?php
// Collegamento della nostra funzione al setup del tema
add_action( 'init', 'create_post_type' );

add_theme_support('post-thumbnails');
function setup_types() {
    register_post_type('mytype', array(
        'label' => __('Il mio tipo'),
        'supports' => array( 'title', 'editor', 'thumbnail', 'revisions' ),
        'show_ui' => true,
    ));
}
add_action('init', 'setup_types');
?>
0
Tutte le risposte alla domanda 2
0
$args = array('post_type' => 'mytype' );                                              
$the_query = new WP_Query( $args );

// Il Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
    /* Ripristina i dati originali del Post */
    wp_reset_postdata();
} else {
    // nessun post trovato
}

Maggiori informazioni puoi trovarle qui: https://codex.wordpress.org/Class_Reference/WP_Query

27 giu 2017 09:42:56
0

Spero che questo ti possa essere d'aiuto.

$args = array(
 'post_type'        => 'il tuo custom post type qui', // Sostituisci con il tuo custom post type
'posts_per_page'   => 5, // Numero di post da mostrare
'category'         => '', // Categoria specifica (lasciare vuoto per tutte)
);
$query = new WP_Query( $args ); 
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post(); 

} // fine while
} // fine if
wp_reset_query();
27 giu 2017 10:45:58