[英]Multiple post loops in Wordpress
I've built a site for a health practitioner with CPT of: (single-injuries.php, single-services.php, single-testimonials.php, single-partners.php)我已经为 CPT 的健康从业者建立了一个网站:(single-injuries.php,single-services.php,single-testimonials.ZE1BFD762321E4096334AC0B6E8419single-partners.php)
and I have the appropriate (archive-injuries.php, archive-services, archive-testimonials, archive-partners) created with loops to display the relevant posts.我用循环创建了适当的(archive-injuries.php,archive-services,archive-testimonials,archive-partners)来显示相关帖子。
HOWEVER I now want to create a sitemap page that pulls ALL THE POSTS from ALL ARCHIVES and just displays the PAGE NAME and URL for each...但是我现在想创建一个站点地图页面,该页面从所有存档中提取所有帖子,并且只显示每个页面的页面名称和 URL...
How do I loop through multiple archives, do I nest a loop for each within a loop?如何循环遍历多个档案,是否在循环中为每个档案嵌套一个循环?
I suggest you should do this using database, or what archives mean at your post?我建议您应该使用数据库来执行此操作,或者存档在您的帖子中意味着什么? If you are using database just make a query that will select all from different tables, like this
如果您使用的是数据库,只需查询 select 都来自不同的表,就像这样
SELECT archive-injuries.*, archive-services.*, archive-testimonials.*, archive-partners.* FROM your data base
Then make a while loop that will display posts, while mysqli_fetch_assoc have some data然后做一个while循环来显示帖子,而mysqli_fetch_assoc有一些数据
You can use a custom query that queries all of your CPTs which you listed (put them into the post_type
array), similar to this (which lists all post titles found, each linked to its full post):您可以使用自定义查询来查询您列出的所有 CPT(将它们放入
post_type
数组),类似于此(列出找到的所有帖子标题,每个都链接到其完整帖子):
<?php
$args = array(
'post_type' => array('injuries', 'services', 'testimonials', 'partners' ),
'post_status' => 'publish',
);
$loop1 = new WP_Query($args);
if ( $loop1->have_posts() ) : while ( $loop1->have_posts() ) : $loop1->the_post();
$post_title = get_the_title();
?>
<div>
<p><a href='<?php echo get_the_permalink(); ?>'><?php echo post_title; ?></a></p>
</div>
<?php endwhile; else: ?>
<p>Nothing found</p>
<?php endif; ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.