admin管理员组

文章数量:1297047

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.

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.

Share Improve this question edited May 12, 2016 at 15:58 DTR asked May 12, 2016 at 15:09 DTRDTR 1114 bronze badges 4
  • 1 1. never use query_posts, this subject is covered extensively on this site. 2. you're using the_date, which echoes a value by default, in comparison and assignment, which will not work. – Milo Commented May 12, 2016 at 15:13
  • @Milo Thanks for that, I've modified the code to use WP_Query and get_the_date() instead, but I'm still having the same problem with posts not showing up in the page. – DTR Commented May 12, 2016 at 15:28
  • Have you doublechecked that the posts are really published? – Tobias Beuving Commented May 12, 2016 at 15:41
  • @TobiasBeuving Yes, the posts are both public and published. – DTR Commented May 12, 2016 at 15:44
Add a comment  | 

1 Answer 1

Reset to default 2

First off, ensure that the category is exactly the same name as the page ( slug and all ). Secondly, the parameters you're passing to WP_Query is incorrect.

  • category_name expects a slug. For example, Page 1 would have a slug of page-1.
  • get_the_title() pulls the current page title - so you're passing Page 1 to a parameter that expects a slug page-1.

What you can do is sanitize_title( get_the_title() ) or preferably you could declare global $post at the top of your document and pass instead $post->post_name.

本文标签: phpCustom category code not showing all posts