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 Answer
Reset to default 2First 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 ofpage-1
.get_the_title()
pulls the current page title - so you're passingPage 1
to a parameter that expects a slugpage-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
版权声明:本文标题:php - Custom category code not showing all posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741639836a2389852.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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. – Milo Commented May 12, 2016 at 15:13WP_Query
andget_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