【问题标题】:How to add posts in the home page same as blog page with me selecting static page for each?如何在主页中添加与博客页面相同的帖子,我为每个页面选择静态页面?
【发布时间】:2019-11-02 13:10:49
【问题描述】:

我是 WordPress 新手,我创建了一个自定义模板,当我尝试将帖子提取到首页(主页)时遇到问题。

在(仪表板->阅读)中,我将主页显示设置为静态页面,并选择主页为(主页)和发布页面为(帖子)。

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

它在帖子页面中工作正常,但是当我在 page-home.php 文件中复制相同的代码时它不起作用并且根本没有获取任何帖子。

对不起,我是初学者,但我需要帮助。 谢谢。

【问题讨论】:

    标签: php wordpress custom-wordpress-pages


    【解决方案1】:

    试试这个

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

    应该能解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      • 2015-12-15
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多