0

i want to display posts in wp page. to do that this is what i did i created a template named mypage-page.php and copied code from page.php to mypage-page.php

this is my mypage-page.php

      <main id="main" class="site-main" role="main">
               <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

                        <h2><?php the_title() ;?></h2>
                        <?php the_post_thumbnail(); ?>
                        <?php the_excerpt(); ?>

                <?php endwhile; else: ?>

                        <p>Sorry, no posts to list</p>

                <?php endif; ?>

    </main><!-- .site-main -->
   //   this has header and footer as well . 

now i created a page mytest using this template ,what i expect it should list posts but it does not, please help me to understand where i'm wrong . i'm just a beginner

1 Answer 1

0

You will want to use wp_query to query the database for the posts, which will be something like:

<?php
 $blog_args = array(
  'post_type' => 'post',
  'posts_per_page' => '-1',
  'order' => 'DESC'
 );
 $blog_query = new WP_Query($blog_args);
 while ( $blog_query->have_posts() ) {
  $blog_query->the_post();
  echo '<a href="'.get_permalink().'">'.get_the_title().'</a>';
 }
 wp_reset_postdata();
 ?>

That will display all posts with just the post title, you would need to add pagination and add more detail to the actual displayed element but should give you a head start

4
  • yes that oworked , can you tell me how can i make post title clickable to go to single.php against each post
    – Sikander
    Oct 11, 2015 at 21:33
  • try my edit which I have made Oct 11, 2015 at 21:37
  • Perfect thank you very much , earlier today you guided me to go for custom page templates and now helped me to implement it . I've really learned something imp from you
    – Sikander
    Oct 11, 2015 at 21:39
  • your welcome, I was in your shoes a few years ago so it's nice to help out others Oct 11, 2015 at 21:47

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.