如何在Wordpress的页面部分显示博客?

时间:2017-04-08 06:48:21

标签: php wordpress

我试图在'关于我们'下面显示博客文章。通过在模板部分中使用以下代码在about页面上显示段落。但是,它只返回实际页面的标题和日期信息作为我编辑页面的日期。

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   <article class="post">
        <header>
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <div class="post-details">
                <i class="fa fa-user"></i><?php the_author_posts_link(); ?>
                <i class="fa fa-calendar"></i> <?php the_time( 'F jS, Y' ); ?>
                <i class="fa fa-folder-open-o"></i> <a href=""><?php the_category( ', ' ); ?></a>
                <i class="fa fa-comments"></i><a href=""><?php comments_popup_link( 'No Comments »', '1 Comment »', '% Comments »' ); ?></a>

            </div><!-- post details -->
        </header>

        <div class="post-excerpt">
            <p><?php the_excerpt(); ?> <a href="post.html">continue reading</a></p>
        </div><!-- post-excerpt -->

        <hr>

    </article><!-- end article -->
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

将实际的博文发布到此部分需要哪些代码?

1 个答案:

答案 0 :(得分:1)

在您的代码段中,您的帖子的自定义查询丢失了。尝试这样的事情:

    // WP_Query arguments
    $args = array(
    'post_type' => 'post',
    'post_status' => 'publish'
    );
    $custom_query = new WP_Query( $args );
    <?php if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
           <article class="post">
                <header>
                    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                    <div class="post-details">
                        <i class="fa fa-user"></i><?php the_author_posts_link(); ?>
                        <i class="fa fa-calendar"></i> <?php the_time( 'F jS, Y' ); ?>
                        <i class="fa fa-folder-open-o"></i> <a href=""><?php the_category( ', ' ); ?></a>
                        <i class="fa fa-comments"></i><a href=""><?php comments_popup_link( 'No Comments »', '1 Comment »', '% Comments »' ); ?></a>

                    </div><!-- post details -->
                </header>

                <div class="post-excerpt">
                    <p><?php the_excerpt(); ?> <a href="post.html">continue reading</a></p>
                </div><!-- post-excerpt -->

                <hr>

            </article><!-- end article -->
        <?php endwhile; else : ?>
        <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
        <?php 
  // Restore original Post Data
    wp_reset_postdata();

        endif; 


    ?>

在这里,您可以找到生成Wordpress查询的有用工具: https://generatewp.com/wp_query/

在这里您可以找到Wordpress Query的允许参数: https://developer.wordpress.org/reference/classes/wp_query/

要使用自定义查询,请记得使用您的查询对象(代码段中的have_posts()the_posts())调用$custom_query->have_posts()$custom_query->the_post()方法,此外还很重要{{1}恢复主要查询。

相关问题