在一组帖子前显示年份值

时间:2015-12-07 作者:gil hamer

这就是我正在努力实现的目标:

我有一个页面模板,显示“案例研究”,其中“案例研究”是一个自定义的帖子类型。页面模板名为: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 &nbsp;<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 例如,在显示当前年份的列表中?非常感谢您的帮助谢谢

1 个回复
最合适的回答,由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

相关推荐

JqueryUi对话框给出未捕获的TypeError:This._addClass不是函数错误

我有一个网站,我们需要一些自定义php编码来连接到外部数据库,以获取几个销售我们产品的供应商的产品评论URL。我们试图实现的基本想法是让用户注册他们的产品,然后如果他们愿意留下评论,就延长保修期。我正在使用XYZScript。com的“插入PHP”插件来实现这一点。该主题最初只加载了jQuery,以避免创建子主题,我们正在php脚本中加载jQueryUI。因此,我们将jQuery加载到文档的标题中,将jQueryUI加载到文档的正文中。我不太确定这是因为加载脚本的顺序造成的,还是其他一些冲突的javasc