'daki her kategoriden sadece 1 gönderi alıyorum nasıl bir haber ve içinde birçok alt kategori adlı bir kategorim var. Yapmak istediğim, bu alt kategorilerin her birine (kategori başlığı, başlık, ek imajı dahil) sadece 1 mesaj (en yenisi) almaktır. Herhangi bir öneriniz var mı? Tüm dünya çapında arkadaşlarımıza birkaç saat ve teşekkürler, bu yüzden bile şablonları gidip yeni sorgular ve döngüler oluşturmak gerekmez ana sorguyu değiştirmek mümkün oldum sonraWordPress
cevap
<?php
$news_cat_ID = get_cat_ID('News');
$news_cats = get_categories("parent=$news_cat_ID");
$news_query = new WP_Query;
foreach ($news_cats as $news_cat) :
$news_query->query(array(
'cat' => $news_cat->term_id,
'posts_per_page' => 1,
'no_found_rows' => true,
'ignore_sticky_posts' => true,
));
?>
<h2><?php echo esc_html($news_cat->name) ?></h2>
<?php while ($news_query->have_posts()) : $news_query->the_post() ?>
<div class="post">
<?php the_title() ?>
<!-- do whatever you else you want that you can do in a normal loop -->
</div>
<?php endwhile ?>
<?php endforeach ?>
Teşekkürler THEDeadMedic. Çalıştı ve bana başka birisine yardım edebilir misin? (aynı kodu uzat) Yalnızca görüntü eki olan ve tarihe göre sıralanmış olan bu gönderiyi nasıl gösterebilirim – sonill
bunun için teşekkürler, tam olarak aynı şeyi arıyordu! +1 – Tarun
Bana bir ton zaman kazandırdı, teşekkürler @TheDeadMedic –
..
// My function to modify the main query object
function grouped_by_taxonomy_main_query($query) {
if ($query->is_home() && $query->is_main_query()) { // Run only on the homepage
$post_ids = array();
$terms = get_terms('formato');
foreach ($terms as $term) {
$post_ids = array_merge($post_ids, get_posts(array(
'posts_per_page' => 4, // as you wish...
'post_type' => 'video', // If needed... Default is posts
'fields' => 'ids', // we only want the ids to use later in 'post__in'
'tax_query' => array(array('taxonomy' => $term->taxonomy, 'field' => 'term_id', 'terms' => $term->term_id,)))) // getting posts in the current term
);
}
$query->query_vars['post_type'] = 'video'; // Again, if needed... Default is posts
$query->query_vars['posts_per_page'] = 16; // If needed...
$query->query_vars['post__in'] = $post_ids; // Filtering with the post ids we've obtained above
$query->query_vars['orderby'] = 'post__in'; // Here we keep the order we generated in the terms loop
$query->query_vars['ignore_sticky_posts'] = 1; // If you dont want your sticky posts to change the order
}
}
// Hook my above function to the pre_get_posts action
add_action('pre_get_posts', 'grouped_by_taxonomy_main_query');
- 1. WordPress WordPress yüklemiş yenisi
- 2. Wordpress header.php içinde Wordpress
- 3. wordpress
- 4. Wordpress:
- 5. Wordpress
- 6. WordPress
- 7. WordPress -
- 8. wordpress
- 9. Wordpress
- 10. htaccess WordPress içinde RewriteRule WordPress
- 11. WordPress için WordPress Editor Image
- 12. WordPress ifadesi doğru yankılamıyorsa WordPress
- 13. Wordpress için kötü amaçlı komut WordPress
- 14. WordPress Ajax Çağrı - WordPress Kullanıcı Kimliği
- 15. WordPress eklentisinde bulunan Wordpress veritabanından veri alın.
- 16. WordPress ile Azure üzerinde WordPress ile Hosting
- 17. Wordpress get_avatar_url
- 18. WordPress TEMPLATEPATH
- 19. Wordpress add_rewrite_rule
- 20. WordPress get_next_post
- 21. Wordpress: url
- 22. wordpress sayfalarda
- 23. wp_get_attachment_image() wordpress
- 24. Wordpress: diffrent wordpress sitesinden gelen son mesajları göster (alt alanlar)
- 25. WordPress Alt sorgu özel Wordpress eklenti tarafından SELECT'in
- 26. Wordpress Etiketlerini Düzenleme
- 27. Wordpress Tüm Mesajları Göster
- 28. WordPress öğesinde önizleme teması
- 29. Eklentiden Wordpress özel url
- 30. wordpress klasöründeki dizin .htaccess
Bu sizin SO'nuzdaki 6. sorunuzdur ve hala daha önceki herhangi bir cevabı kabul etmediniz. Herhangi bir yararlı cevap varsa lütfen bunu yapın, bu sizin karmaınızı geliştirecektir :) – FelipeAls