PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 8, 2022

[FIXED] How to Get Posts in Multiple Places in Wordpress theme?

 July 08, 2022     blogs, frontpage, posts, wordpress     No comments   

Issue

I'm really newbie to wordpress theme development and I want to get recent 10 posts inside front-page.php and all posts inside the index.php (I have no idea if a better way is there) followed by pagination.

Update : I want to have a home page that shows 10 posts and a articles page that shows all posts.

Is this approach right? if it is, how do I do that?


Solution

after some research I found a solution to do that.

at first I created 2 pages called home and articles, then I changed Setting -> Reading :

Front Page -> home

and

Posts Page -> articles.

then I put the following in the index.php as articles page :

<?php
    if (have_posts()) : while (have_posts()) : the_post();
        get_template_part('content', get_post_format());
    endwhile; endif;
?>

then inside of front-page.php :

<?php
// the query
$wpb_all_query = new WP_Query(array('post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 10)); ?>

<?php if ($wpb_all_query->have_posts()) : ?>

    <!-- the loop -->
    <?php while ($wpb_all_query->have_posts()) : $wpb_all_query->the_post(); ?>
        <div class="post">
            <div class="col-xs-12 col-lg-6">
                <div class="panel panel-default ">
                    <div class="panel-body">
                        <div class="col-xs-4">
                            <a href="<?php the_permalink(); ?>">
                                <?php if (has_post_thumbnail()) : ?>
                                    <img src="<?php the_post_thumbnail_url(); ?>" alt="">
                                <?php else: ?>
                                    <img
                                        src="<?php bloginfo('template_directory'); ?>/assets/image/placeholder.jpg"
                                        alt="">
                                <?php endif; ?>
                            </a>
                        </div>
                        <div class="col-xs-8">
                            <div class="title">
                                <a href="<?php the_permalink(); ?>">
                                    <h2><?php the_title(); ?></h2>
                                </a>
                            </div>
                            <div class="content">
                                <p><?php echo substr(get_the_content(), 0, 320); ?>...</p></div>
                            <div class="read-more">
                                <a href="<?php the_permalink(); ?>">Read More</a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    <?php endwhile; ?>
    <!-- end of the loop -->


    <?php wp_reset_postdata(); ?>

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

finally it works in the way exactly what I wanted.



Answered By - Hooman
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing