-1

did read alot on stackoverflow and wordpress documentation but nothing works iam using newest WP version

i createt a template named contact.php

<?php 
/*
 * Template Name: Contact Template
 * description: >-
  Page template 
 */
    get_header(); 
         ?>

everything works , you can choose the template in wp admin dashboard when creating a new site but the post loop dosent work. only on the index.php

this code i use in index. works fine. if i copy over to my template it will not load the posts

  <?php if (have_posts()) : ?>

                <?php while (have_posts()) : the_post(); ?>
                    <div class="col-12 mt-4 mb-4 justify-content-center text-center">
                        <a href="">
                            <h1><a href="<?php the_permalink(); ?>">
                                    <?php the_title(); ?> </a></h1>
                        </a>
                        <hr>
                        <p> <?php the_excerpt(); ?> <br> </p>

                        <style>
                            img {
                                height: 200px;
                                width: 200px;
                                border-radius: 20%;
                            }
                        </style>

                        <?php if (has_post_thumbnail()) : ?>
                            <?php the_post_thumbnail(); ?>
                        <?php endif; ?><br>
                        <p> <a href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>"><?php the_author(); ?> <?php the_date(); ?><br></a> </p>
                        <p></p>
                    </div>



                <?php endwhile; ?>
                <!--end the while loop-->

            <?php else : ?>
                <!-- if no posts are found then: -->

                <p>No posts found</p> <!-- no posts found displayed -->
            <?php endif; ?>
            <!-- end if -->
        </div>
  • That's not how you loop through posts in a template. A page template displays the content of that page only. What you want is to create a template that acts like an archive page. Take a look at the archive.php in your current theme. Also, the way this works hasn't really changes in years, so the old answers here still apply in the vast majority of cases. – FluffyKitten Aug 12 '20 at 09:58
  • Does this answer your question? [Wordpress - List all posts (with proper\_pagination)](https://stackoverflow.com/questions/4794622/wordpress-list-all-posts-with-proper-pagination) – FluffyKitten Aug 12 '20 at 09:59

1 Answers1

0

Actually when you create a new template into Wordpress theme folder directory you should copy from the one that is used from your template to render the post list.

As you can see here from wordpress documentation index.php is just the deepest level wordpress reach if it doesn't find any other suitable template.

If you are unsure of what template to use i suggest you to install query monitor and see from the wpadmin top bar in your frontend which is the template for a standard post loop page.

If you copied from the correct template there should be no standard reason of why that should fail. If it doesn't work for you what i can suggest is to see if the problem still happens with default template (like twentytwenty) or disabling all your plugins.

Diego
  • 1,610
  • 1
  • 14
  • 26