这就是我正在努力实现的目标:
我有一个页面模板,显示“案例研究”,其中“案例研究”是一个自定义的帖子类型。页面模板名为:page-case-studies.php
实际上,我使用非常简单的定制查询和分页。这是我的代码:
<div class="container">
<div class="heading"><h3 class="normalh3">2015’s Case Studies</h3></div>
<?php
// WP_Query arguments
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
$args = array (
\'order\' => \'DESC\',
\'posts_per_page\' => \'5\',
\'paged\' => $paged,
\'orderby\' => \'date\',
\'post_type\' => \'case-study\'
);
// The Query
$query_case_studies = new WP_Query( $args );
// The Loop
if ( $query_case_studies->have_posts() ) {
while ( $query_case_studies->have_posts() ) {
$query_case_studies->the_post(); ?>
<div class="two-col">
<div class="col2 first" >
<div class="text">
<h3><?php the_title(); ?></h3>
<p><?php $aa=get_the_excerpt();
echo substr($aa, 0, 245);?>...</p>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="btn">
More on how we won together <img src="<?php bloginfo(\'template_directory\'); ?>/images/button-arrow.png"/>
</a>
</div>
</div>
<div class="col2">
<div class="image">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php
// Must be inside a loop.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
else {
echo \'<img src="\' . get_bloginfo( \'template_directory\' ) . \'/images/placeholder1.jpg" />\';
}
?>
</a>
</div>
</div>
</div> <!--<two-col>-->
<?php }
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
</div>
请注意顶部的这一行:
<div class="heading"><h3 class="normalh3">2015’s Case Studies</h3></div>
我想显示与职位类型列表相关的年份。这意味着在2016年,我想展示“2016年的案例研究”,并继续列出“案例研究”
现在它是静态的,不工作。它甚至不在循环中,因此在2016年,除了继续所有案例研究之外,什么都不会发生。
无论如何,我可以添加标题H3 例如,在显示当前年份的列表中?非常感谢您的帮助谢谢
最合适的回答,由SO网友:Pieter Goosen 整理而成
您需要比较当前帖子和上一篇帖子之间的年份值,并根据该值显示h3标签和基于年份的内容
我不会复制你的代码,只复制重要的部分。请注意,我可能需要去掉一些标记才能添加我的代码部分
if ( $query_case_studies->have_posts() ) {
// Define the variable to hold the year value from the post date
$date_variable = \'\';
while ( $query_case_studies->have_posts() ) {
$query_case_studies->the_post();
// Get the post date, but only the year part
$year = get_the_date( \'Y\' );
// Now lets compare $year with $date_variable and display the year accordingly
if ( $date_variable != $year ) // Year values are not the same
echo \'<div class="heading"><h3 class="normalh3">\' . $year . \'@apos; Case Studies</h3></div>\';
// Set $date_variable to $year
$date_variable = $year;
// Rest of what you need to do