0

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();
?>
3
  • Under which page you need to Show your custom template. Either in Landing Page or Blog Page ??? Commented Aug 26, 2016 at 6:49
  • neither, It's just a page that contains a snippet of some of my posts Commented Aug 26, 2016 at 6:52
  • Hope so you have to set the page in the Admin Panel to display it right:) Commented Aug 26, 2016 at 6:53

3 Answers 3

1

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();
?>
Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me, thank you! now i need to find that documentation!
0

Create a page named "Blog" from wp admin and then create a file in theme folder named page-blog.php and write the following code below.

<?php
get_header();
?>

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

    <h1>BLOG INDEX PAGE</h1>

    <?php
    $args = array(
        'post_type'     =>  'post',
        'post_status'   =>  'publish',
        'orderby' => 'id',
        'order' => 'desc'
    );  
    $loop = new WP_Query($args);
    if($loop->have_posts()) :
    while ( $loop->have_posts() ) : $loop->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();
?>

Comments

0
        <?php
            $args = array(
                    'post_type' => 'post',
                    'post_status' => 'publish',
                    'posts_per_page' => -1,
                    'offset' => 0
            );

            $the_query1 = new WP_Query( $args );

            if (count($the_query1->posts)>0) {
                while ( $the_query1->have_posts() ) : $the_query1->the_post();
                    get_template_part( 'loop-archive-template-location' );
                endwhile;


            }
        ?>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.