Declan Watt Declan Watt - 3 months ago 12
PHP Question

Wordpress Custom Page, posts not displaying

Hi i'm trying to create a custom page (custom-page.php) that contains some of my blog posts, but when I test the loop i'm not seeing any posts. Its only returning the name of my custom page template as the h2.

I have two posts already published but they are not showing up.

I have a front-page.php which users land on when they first come to my site and I have not changed any settings under the reading tab.

I've read over the Wordpress codex and can't find any solutions so far.



<?php
get_header();
?>

<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">


<h1>BLOG INDEX PAGE

BLOG INDEX PAGE</h1>


<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php the_content(); ?>
<?php endwhile; else : ?>

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

<?php endif; ?>

</main><!-- #main -->
</div><!-- #primary -->


<?php
get_footer();
?>

Answer

Please follow this code.

$newsQuery = newWP_Query('post_type=post','post_status=publish');
if ( $newsQuery->have_posts() ) {
    while ($newsQuery->have_posts()) {
        $newsQuery->the_post();
        echo get_the_title();
        echo get_the_excerpt();
    }
}

and Your complete template will be like this.

<?php
get_header();
?>

<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">


<h1>BLOG INDEX PAGE 

BLOG INDEX PAGE</h1>


   <?php 
    $newsQuery = new WP_Query('post_type=post','post_status=publish');
    if ( $newsQuery->have_posts() ) : while ( $newsQuery->have_posts() ) : $newsQuery->the_post(); ?>

    <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent  Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
     <?php the_content(); ?>
    <?php endwhile; else : ?>

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

<?php endif; ?>

</main><!-- #main -->
</div><!-- #primary -->


<?php
get_footer();
?>