-2

I have created a page template:

<?php /* Template Name: homedefault */ ?>

But Instead of text coming from here i.e. wp editor:

enter image description here

I want it to come from the loop:

   <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <?php $post_id = get_the_ID(); ?>
        <?php get_template_part('content','home'); ?>        
    <?php endwhile; ?>
    <?php endif; ?>

But the loop doesn't seem to be working. Is it possible at all?

P.S. → loop is for the posts →

enter image description here

6
  • Are you aware of the home.php template?
    – Tom J Nowell
    Commented Feb 10, 2018 at 11:41
  • Yes, and the home.php(and front-page.php) template is working fine with no issues, but I am creating them for some other purpose.
    – WordCent
    Commented Feb 10, 2018 at 11:44
  • Someone have downvoted me? May I know the reason why the post is downvoted.
    – WordCent
    Commented Feb 10, 2018 at 15:49
  • even moderators don't know who downvoted, but nobody is obligated to explain, it would be better to ask on the meta site why a question would be downvoted and how to improve it
    – Tom J Nowell
    Commented Feb 11, 2018 at 2:37
  • 1
    That's what the meta site is for, comments are for clarifying questions, by trying to discuss downvoting here you're hurting your own question and derailing. If you insist on it you must use the meta site, that is where discussion of the site and how to improve questions takes place. Not here. What you're asking could even be grounds for downvoting
    – Tom J Nowell
    Commented Feb 11, 2018 at 14:05

1 Answer 1

5

On your custom page template, the default loop is working and that's why the loop is fetching content from the page. You need a custom query in this case. Here's the code.

<?php
$query = new WP_Query( array(
    'post_type' => 'post',
    'post_status' => 'publish',
) );
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        get_template_part( 'content', 'home' ); 
    }
}
4
  • Okies thanks for the input. Let me try that query I will update you.
    – WordCent
    Commented Feb 10, 2018 at 11:27
  • Thanks, I have accepted the answer. Now I will remember how to generate post loop on a static page. All the best for your WP journey.
    – WordCent
    Commented Feb 10, 2018 at 12:00
  • Is it you who have downvoted me?
    – WordCent
    Commented Feb 10, 2018 at 15:51
  • 2
    You accepted my answer and I got 3 upvotes and 1 upvote in a comment, then why would I downvote someone who helped me to get all these! I didn't do that man -_-
    – obiPlabon
    Commented Feb 10, 2018 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.