首页 > 解决方案 > 为什么我的页面内容在此 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>

...

标签: phpwordpress

解决方案


您实际上应该在循环中包含您创建的查询。

<?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()


类参考/WP 查询- #用法

至于你的代码:

<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>

推荐阅读