Reputation: 71
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();
?>
Upvotes: 0
Views: 690
Reputation: 637
<?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;
}
?>
Upvotes: 0
Reputation: 595
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();
?>
Upvotes: 0
Reputation: 343
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();
?>
Upvotes: 1