How to call a custom post with get_posts() instead of query_posts()?
-
16-10-2019 - |
Question
I currently use query_posts
to show these custom posts but I'm pretty sure that I should uses get_posts()
to write it correctly.
<?php query_posts( array( 'type-mario' => 'games', 'showposts' => 10 ) ); ?>
<p>Mario games</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(); ?>
Thanks for your advice.
Solution
Hi Elium2009:
Using your code, I think this is what you were looking for? (note that WP_Query()
is just the more direct version of get_posts()
):
<?php $posts = WP_Query(array(
'taxonomy' => 'type-mario'
'term' => 'games',
'posts_per_page' => 10
)); ?>
<p>Mario games</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(); ?>
Hope this helps?
OTHER TIPS
You can use either really, but if you want to use get_posts this is how it's done:
<?php query_posts('post_type=games&posts_per_page=10'); ?>
<?php if(have_posts()) : while (have_posts() ) : the_post(); ?>
...
<?php endwhile; endif; ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange