简体   繁体   English

如何在主页中添加与博客页面相同的帖子,我为每个页面选择 static 页面?

[英]How to add posts in the home page same as blog page with me selecting static page for each?

I'm new to WordPress, and I've created a custom template, I'm facing a problem when I try to fetch the posts to the front page (homepage).我是 WordPress 的新手,我创建了一个自定义模板,当我尝试将帖子获取到首页(主页)时遇到问题。

In (dashboard->Reading) I set my homepage displays as a Static page and selected Home page as (home) and Post page as (posts).在(仪表板->阅读)中,我将主页显示设置为 Static 页面,并选择主页作为(主页)和发布页面作为(帖子)。

In the index.php the code that's showing the posts in Posts page is:在 index.php 中显示帖子页面中帖子的代码是:

<section class="recent-posts" id="recent-posts">
    <div class="container">
        <div class="title text-center">
            <h1 class="title-blue">posts</h1>
        </div>
        <div class="row">

                    <?php

                        while(have_posts()) {
                            the_post();

                    ?>

            <div class="col-lg-6">
                <div class="single-rpost d-sm-flex align-items-center" data-aos="fade-right"
                    data-aos-duration="800">
                    <div class="post-thumb">
                        <img class="img-fluid" src="<?php echo get_the_post_thumbnail_url(get_the_ID()); ?>" alt="post-1">
                    </div>
                    <div class="post-content text-sm-right">
                        <time datetime="<?php the_time('l, jS/n/Y'); ?>">
                                                    <?php the_time('l, jS/n/Y'); ?>
                                                </time>
                        <h3>
                                                    <a href="<?php the_permalink(); ?>">
                                                        <?php the_title(); ?>
                                                    </a>
                                                </h3>
                                                <p class="post-excerpt">
                                                    <?php echo wp_trim_words(get_the_excerpt(), 10); ?>
                                                </p>
                        <p>
                                                    <!-- <a href="#"> -->
                                                        <?php echo get_the_category_list(' ') ?>
                                                    <!-- </a> -->
                                                </p>
                        <a class="post-btn" href="<?php the_permalink(); ?>">
                                                    <i class="fa fa-arrow-left"></i>
                                                </a>
                    </div>
                </div>
                        </div>

                        <?php 
                            }
                            wp_reset_query(); 
                        ?>
        </div>
                <!-- Posts Navigation -->

        </div>
</section>

<!-- Paginate through pages -->
<div class="pagination">
    <?php echo paginate_links(); ?>
</div>

It works fine in posts page but when I copy the same code in page-home.php file it doesn't work and fetches no posts at all.它在帖子页面中工作正常,但是当我在 page-home.php 文件中复制相同的代码时,它不起作用并且根本没有获取任何帖子。

Sorry I'm a beginner, but I need help.对不起,我是初学者,但我需要帮助。 Thanks.谢谢。

Try this尝试这个

 <?php
global $paged, $wp_query, $wp;
$args = wp_parse_args($wp->matched_query);
if ( !empty ( $args['paged'] ) && 0 == $paged ) {
$wp_query->set('paged', $args['paged']);
$paged = $args['paged'];
}
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('paged='.$paged.'&showposts=10&cat='.get_option('prototype_blog_cat'));
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
            <div class="col-lg-6">
                <div class="single-rpost d-sm-flex align-items-center" data-aos="fade-right"
                    data-aos-duration="800">
                    <div class="post-thumb">
                        <img class="img-fluid" src="<?php echo get_the_post_thumbnail_url(get_the_ID()); ?>" alt="post-1">
                    </div>
                    <div class="post-content text-sm-right">
                        <time datetime="<?php the_time('l, jS/n/Y'); ?>">
                                                    <?php the_time('l, jS/n/Y'); ?>
                                                </time>
                        <h3>
                                                    <a href="<?php the_permalink(); ?>">
                                                        <?php the_title(); ?>
                                                    </a>
                                                </h3>
                                                <p class="post-excerpt">
                                                    <?php echo wp_trim_words(get_the_excerpt(), 10); ?>
                                                </p>
                        <p>
                                                    <!-- <a href="#"> -->
                                                        <?php echo get_the_category_list(' ') ?>
                                                    <!-- </a> -->
                                                </p>
                        <a class="post-btn" href="<?php the_permalink(); ?>">
                                                    <i class="fa fa-arrow-left"></i>
                                                </a>
                    </div>
                </div>
                        </div>

    <?php endwhile; ?>

    <span >
    <?php echo paginate_links( array(
  'prev_text' => '<span>Previous</span>',
  'next_text' => '<span>Next</span>'
)); ?>

It should solve the problem.它应该可以解决问题。

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

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