Come chiamare un custom post con get_posts() invece di query_posts()?

11 nov 2010, 23:37:02
Visualizzazioni: 24.5K
Voti: 6

Attualmente uso query_posts per mostrare questi custom post ma sono abbastanza sicuro che dovrei usare get_posts() per scriverlo correttamente.

<?php query_posts( array( 'type-mario' => 'games', 'showposts' => 10 ) ); ?>
<p>Giochi Mario</p>
<?php while ( have_posts() ) : the_post(); ?>
 <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
 <h2><?php the_title(); ?></h2>
 </div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>

Grazie per il vostro consiglio.

0
Tutte le risposte alla domanda 2
3
12

Ciao Elium2009:

Utilizzando il tuo codice, penso che questo sia quello che stavi cercando? (nota che WP_Query() è semplicemente la versione più diretta di get_posts()):

<?php $posts = WP_Query(array( 
   'taxonomy' => 'type-mario'
   'term' => 'games',
   'posts_per_page' => 10 
)); ?>
<p>Giochi di Mario</p>
<?php while ( $posts->have_posts() ) : $posts->the_post(); ?>
  <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <h2><?php the_title(); ?></h2>
  </div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>

Spero sia utile?

12 nov 2010 01:34:02
Commenti

GRAZIE !!!!!!!!!!!!!!!!! Esattamente ciò di cui avevo bisogno :)

User User
12 nov 2010 03:24:59

@Elium2009 - Benvenuto. :)

MikeSchinkel MikeSchinkel
12 nov 2010 10:25:31

new WP_Query(array(

fdrv fdrv
13 lug 2020 22:13:23
3

Puoi utilizzare entrambi, ma se vuoi usare get_posts ecco come si fa:

<?php query_posts('post_type=games&posts_per_page=10'); ?>
<?php if(have_posts()) : while (have_posts() ) : the_post(); ?>
...
<?php endwhile; endif; ?>
12 nov 2010 00:22:35
Commenti

Grazie, ma vorrei ottenerlo dalla mia tassonomia personalizzata associata al mio custom-post, qui type-mario è la mia tassonomia associata a un custom-post e games è il termine. Come lo faresti?

User User
12 nov 2010 00:34:46

Hai provato ad aggiungere la tassonomia e il termine nel codice sopra?<?php query_posts('post_type=games&posts_per_page=10&taxonomy=type_mario&term=games'); ?> <?php if(have_posts()) : while (have_posts() ) : the_post(); ?> ... <?php endwhile; endif; ?>

Drai Drai
5 apr 2012 07:00:49

Dalla documentazione di query_posts: Nota: Questa funzione sovrascriverà completamente la query principale e non è pensata per l'uso da parte di plugin o temi.

Gavin Gavin
6 apr 2020 14:57:30