php - 为什么我的页面内容在此 WordPress 页面中的帖子上方缺失?
问题描述
我创建了一个新的页面模板来显示帖子;但是,不是页面内容没有显示在页面上。
我已经尝试更新页面中的 php,但无法弄清楚。
...
<div class="container">
<div class="row responsive">
<div class="col-md-9 col-sm-12 col-xs-12 content main-container">
<?php
$wp_query = new WP_Query( array(
'post_type' => 'podcast',
'posts_per_page' => 12,
) );
?>
<?php while ( have_posts() ) : the_post(); ?>
<?php echo get_post_field('post_content', $post->ID); ?>
<?php the_content();
wp_link_pages(); ?>
<div class="podcast">
<div class="podcast-image">
<a href="<?php echo get_post_meta($post->ID, 'podcast_website', true); ?>" target="_blank" rel="noopener noreferrer"><img title="<?php the_title(); ?>" src="<?php echo get_post_meta($post->ID, 'podcast_image', true); ?>" alt="<?php the_title(); ?>" /></a>
<div class="podcast-text">
<h4><?php the_title(); ?></h4>
<h6><a title="Listen" href="<?php echo get_post_meta($post->ID, 'podcast_listen', true); ?>" target="_blank" rel="noopener noreferrer">Listen</a></h6>
</div><!--podcast-text-->
</div><!--podcast-image-->
</div><!--podcast-->
<?php endwhile; ?>
</div>
...
解决方案
您实际上应该在循环中包含您创建的查询。
<?php
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
} ?>
这里可以理解$the_query->have_posts()
至于你的代码:
<div class="container">
<div class="row responsive">
<div class="col-md-9 col-sm-12 col-xs-12 content main-container">
<?php
$wp_query = new WP_Query( array(
'post_type' => 'podcast',
'posts_per_page' => 12,
) );
?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<?php echo get_post_field('post_content', $post->ID); ?>
<?php the_content();
wp_link_pages(); ?>
<div class="podcast">
<div class="podcast-image">
<a href="<?php echo get_post_meta($post->ID, 'podcast_website', true); ?>" target="_blank" rel="noopener noreferrer"><img title="<?php the_title(); ?>" src="<?php echo get_post_meta($post->ID, 'podcast_image', true); ?>" alt="<?php the_title(); ?>" /></a>
<div class="podcast-text">
<h4><?php the_title(); ?></h4>
<h6><a title="Listen" href="<?php echo get_post_meta($post->ID, 'podcast_listen', true); ?>" target="_blank" rel="noopener noreferrer">Listen</a></h6>
</div><!--podcast-text-->
</div><!--podcast-image-->
</div><!--podcast-->
<?php endwhile; ?>
</div>
推荐阅读
- c++ - 找出一个进程被其进程 ID 占用的所有内存地址
- android - 应用程序终止时未收到后台 FCM 通知
- ruby - 无法安装 macos Mojave 版本 10.14 ruby 2.7 无法安装
- php - 您如何做到只有登录用户才能看到他们创建的数据?
- php - 如何使用 between 子句遍历每个行实例以删除数据
- xcode - Ionic Cordova - xcodebuild:错误:无法找到与提供的目标说明符匹配的目标:
- amazon-web-services - 裸域和 http 到 https 重定向
- java - 如何在 Aerospike 中获取垃圾箱的大小?
- algorithm - 使用 Base 62 创建 URL 缩短器?
- java - 从 JSON Android 获取 Int 值