任何建议。
我可以显示函数中的内容吗。php或哪个文件负责静态首页。我正在使用自定义主题
默认情况下,WordPress将查找front-page.php
作为自定义主页。为便于将来参考,您需要将WordPress Theme Hierarchy 页它包括WordPress将按顺序查找哪些文件的流程图。
在您的front-page.php
文件,或可有条件调用的includepage.php
, 您需要使用WP_Query
班说你想要的类别有slugnews
:
$args = array (
\'category_name\' => \'news\'
);
$front_page_query = new WP_Query( $args );
这段代码要做的是查询WordPress数据库并获取所有分类在下面的帖子news
. 您可以通过使用类别的ID来实现这一点,但slug可能是IMO最好的方法,因为它不需要一直在表中查找ID,如果其他人也使用此代码,它会更有意义。从这里开始,您只需使用循环,就像使用任何其他页面一样,只需添加一个内容。由于使用了自定义查询,您已经更改了$post
全局变量,因此需要重置它。这是通过使用wp_reset_postdata()
函数正好位于else
在您的if/then
陈述所以你的循环看起来像这样。。。
<?php if ( $front_page_query->have_posts() ) : ?>
<?php while ( $front_page_query->have_posts() ) : $front_page_query->the_post(); ?>
// Code for displaying the post
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
// No Posts Found Code
<?php endif; ?>
您可以添加其他参数来优化查询,例如限制其返回的帖子数量、跳过任何受密码保护的帖子等。只需将它们添加到$args
大堆我使用get_posts() 使用以下参数调用:array(6) { [\"include\"]=> string(14) \"1633,1634,1635\" [\"post_status\"]=> string(3) \"any\" [\"post_type\"]=> string(10) \"attachment\" [\"post_mime_type\"]=> string(5) \"image\"