Cum să folosești get_posts() în loc de query_posts() pentru articole personalizate?

11 nov. 2010, 23:37:02
Vizualizări: 24.5K
Voturi: 6

În prezent folosesc query_posts pentru a afișa aceste articole personalizate, dar sunt sigur că ar trebui să folosesc get_posts() pentru a scrie codul corect.

<?php query_posts( array( 'type-mario' => 'games', 'showposts' => 10 ) ); ?>
<p>Jocuri 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(); ?>

Mulțumesc pentru sfaturi.

0
Toate răspunsurile la întrebare 2
3
12

Salut Elium2009:

Folosind codul tău, cred că asta căutai? (reține că WP_Query() este doar versiunea mai directă a get_posts()):

<?php $posts = WP_Query(array( 
   'taxonomy' => 'type-mario'
   'term' => 'games',
   'posts_per_page' => 10 
)); ?>
<p>Jocuri 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(); ?>

Sper că te ajută?

12 nov. 2010 01:34:02
Comentarii

MULȚUMESC !!!!!!!!!!!!!!!!! Exact ce aveam nevoie :)

User User
12 nov. 2010 03:24:59

@Elium2009 - Cu plăcere. :)

MikeSchinkel MikeSchinkel
12 nov. 2010 10:25:31

new WP_Query(array(

fdrv fdrv
13 iul. 2020 22:13:23
3

Puteți folosi oricare dintre ele, dar dacă doriți să utilizați get_posts, iată cum se face:

<?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
Comentarii

Mulțumesc, dar aș dori să o obțin din taxonomia personalizată asociată postării mele personalizate. Aici, type-mario este taxonomia mea asociată unei postări personalizate, iar games este termenul. Cum ați face asta?

User User
12 nov. 2010 00:34:46

Ai încercat să adaugi taxonomia și termenul în codul de mai sus?<?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

Din documentația pentru query_posts: Notă: Această funcție va suprascrie complet interogarea principală și nu este destinată utilizării de plugin-uri sau teme.

Gavin Gavin
6 apr. 2020 14:57:30