gpt4 book ai didi

php - 内容和标题不会显示在 Wordpress 的某些页面上

转载 作者:太空狗 更新时间:2023-10-29 15:13:27 31 4
gpt4 key购买 nike

我有一个“front-page.php”,它是一个静态的单侧页面。如果我使用 Wordpress 循环在 front-page.php 上查看我的最新帖子,它们都会显示出来。现在我想创建一个新闻页面,所以我创建了一个文件“page-news.php”。从首页删除循环代码并将其粘贴到页面新闻中。虽然,什么也没有发生。

循环代码:

<?php get_header();?>

<?php
if (have_posts()):
while (have_posts()): the_post();?>

<?php the_title();?>
<?php the_content();?>

<?php
endwhile;
else: echo '<p>no posts were found</p>';
endif;

?>

<?php get_footer();?>

我错过了什么?

最佳答案

你需要添加wp_Query主页被认为是一个博客页面,所以它有 Query default 。

$args = array (
/*'cat' => $catNum,*/
'post_type' => 'post',
'pagination' => false,
'posts_per_page' => '-1',
'ignore_sticky_posts' => false,
'order' => 'DESC',
'orderby' => 'date',
);

// The Query
$query = new WP_Query( $args );

你应该在之前添加这段代码

if (have_posts()):
while (have_posts()): the_post();?>

关于 have_posts() 的这部分将是

// The Loop
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) {
$query->the_post();

不要忘记在末尾添加 wp_reset_postdata(); 这样您就可以在一个页面中使用多个查询。

关于php - 内容和标题不会显示在 Wordpress 的某些页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34307967/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com