admin管理员组文章数量:1279147
I'm trying to get my posts from my category.php file.
The loop works fine, it's showing my custom post types, but I'd like to store the posts from my category to reorder them in a certain way.
How do I do that ? Tried to use get_posts()
, the it return the 3 first post of the 'post' post type.
<?php
if (have_posts()) : ?>
<section class="playlist_row row">
<div class="col-12 owl-container" id="<?= "";?>">
<div class="owl-carousel owl-theme">
<?php
while (have_posts()) : the_post(); ?>
<?php get_template_part("template-parts/card/playlist", "card"); ?>
<?php endwhile; ?>
</div>
</div>
</section>
<?php
else: ?>
<p>Aucun résultat.</p>
<?php endif; ?>
I'm trying to get my posts from my category.php file.
The loop works fine, it's showing my custom post types, but I'd like to store the posts from my category to reorder them in a certain way.
How do I do that ? Tried to use get_posts()
, the it return the 3 first post of the 'post' post type.
<?php
if (have_posts()) : ?>
<section class="playlist_row row">
<div class="col-12 owl-container" id="<?= "";?>">
<div class="owl-carousel owl-theme">
<?php
while (have_posts()) : the_post(); ?>
<?php get_template_part("template-parts/card/playlist", "card"); ?>
<?php endwhile; ?>
</div>
</div>
</section>
<?php
else: ?>
<p>Aucun résultat.</p>
<?php endif; ?>
Share
Improve this question
asked Sep 25, 2021 at 11:22
lucrecelucrece
1248 bronze badges
2
- 1 Can you show your query? – Howard E Commented Sep 25, 2021 at 11:47
- I'm using the default wordpress query for the category.php file – lucrece Commented Sep 28, 2021 at 19:29
1 Answer
Reset to default 1As Howard E said, without knowing more about what and how you want to reorder it's difficult to give you useful advice. Here's a good breakdown of the options and, here are some places to start.
- If you want an array of posts to order your own way, you can use the native function
get_posts()
in order to create an array of posts based on selected arguments. More info here. - If you want to modify the existing loop on your archive page and order youor posts with custom arguments, you can use
pre_get_posts()
- If you want to write use existing WordPress arguments to order your posts, you can write a custom query using
WP_Query
and the ordering arguments it already supports.
Update
Since you want to sort your posts by a custom ACF field, here's how to do that with get_posts()
:
// get posts
$posts = get_posts(array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value',
'order' => 'DESC'
));
ACF has an entire page dedicated to sorting by custom field. You shouldn't need to put the posts back in an array in order to sort them.
Good luck!
本文标签: Stores category posts in an array
版权声明:本文标题:Stores category posts in an array 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741294027a2370711.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论