I have a PHP template I am applying to several pages on my site so that they show a list of all posts in a category with the same name as the page. However, this is not working correctly in that posts from one category ('Events') do not show up at all on its page, while only one post from another category ('Results') is showing up on its page despite there being several posts in each.
I have checked that the category and page names are the same (end even that the posts only have that one category), so that is not the issue. Yet reading through the code, I can see no obvious errors.
I am working with the following code in my template file:
<?php /*
Template Name: List-all-posts-in-category
*/ ?>
<?php get_header(); ?>
<div id="main">
<?php get_sidebar(); ?>
<div id="content">
<div id="content-post">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="content-post-title">
<h1><?php the_title(); ?></h1>
</div>
<?php the_content(); ?>
<?php endwhile; else: endif; ?>
<?php $the_query = new WP_Query( array( 'category_name' => sanitize_title( get_the_title() ) ) ); ?>
<?php $yearNumber = 'YEARNOTSET'; ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if ($yearNumber != get_the_date($d = 'Y')): ?>
<?php $yearNumber = get_the_date($d = 'Y'); ?>
<div id="content-category-post-date">
<h1><?php the_date($d = 'Y'); ?></h1>
</div>
<?php endif; ?>
<div id="content-category-post-title">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
</div>
<?php endwhile; else: endif; ?>
</div>
</div>
</div>
<div id="delimiter">
</div>
<?php get_footer(); ?>
Any ideas what could be causing the issue?
Thanks in advance.
query_posts
, this subject is covered extensively on this site. 2. you're usingthe_date
, which echoes a value by default, in comparison and assignment, which will not work.WP_Query
andget_the_date()
instead, but I'm still having the same problem with posts not showing up in the page.