Mohammed Ibrahim
Mohammed Ibrahim

Reputation: 533

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

In (dashboard->Reading) I set my homepage displays as a Static page and selected Home page as (home) and Post page as (posts).

In the index.php the code that's showing the posts in Posts page is :

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

Sorry I'm a beginner, but I need help. Thanks.

Upvotes: 0

Views: 407

Answers (1)

user8152078
user8152078

Reputation:

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.

Upvotes: 1

Related Questions