简体   繁体   English

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

[英]How to display blog in a section of a page in Wordpress?

I'm trying to display blog posts underneath the 'about us' paragraph on an about page by using the code below in a template part. 我试图通过使用模板部分中的以下代码在“关于”页面的“关于我们”段落下方显示博客文章。 However, it's only returning the title of the actual page and the date info as the date I've edited the page. 但是,它只是返回实际页面的标题和日期信息作为我编辑页面的日期。

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

What code do I need to pull my actual blog posts into this section? 我需要什么代码才能将我的实际博客文章纳入本节?

In your snippet the custom query for your posts is missing. 在您的代码段中,缺少关于帖子的自定义查询。 Try something like this: 尝试这样的事情:

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


    ?>

Here you can find an useful tool to generate a Wordpress Query: https://generatewp.com/wp_query/ 在这里您可以找到一个有用的工具来生成Wordpress查询: https : //generatewp.com/wp_query/

Here you can find permitted arguments for Wordpress Query: https://developer.wordpress.org/reference/classes/wp_query/ 在这里,您可以找到Wordpress查询的允许参数: https : //developer.wordpress.org/reference/classes/wp_query/

To use your custom query remember to call have_posts() and the_posts() methods with your query object ( $custom_query->have_posts() and $custom_query->the_post() in snippet), furthermore is important wp_reset_postdata() to restore main query. 要使用自定义查询,请记住使用查询对象( $custom_query->have_posts()$custom_query->the_post()调用have_posts()the_posts()方法,此外,重要的是wp_reset_postdata()可以还原主查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM