【问题标题】:Sort posts by ACF date and add title for each month按 ACF 日期对帖子进行排序并为每个月添加标题
【发布时间】:2021-06-13 19:37:58
【问题描述】:

我正在尝试为我的博客文章创建一个存档页面,该页面按通过 ACF 日期选择器字段添加的日期排序。

这是我目前所拥有的:

$args = array(
'post_type'         =>  'post',
'posts_per_page'    =>  -1,
'orderby'           =>  'meta_value',
'order'             =>  'DESC',
'meta_key'          =>  'datum_artikel',
'meta_value'        =>  date('Ymd',strtotime("today")),
'meta_compare'      =>  '<='
);

$query = new WP_Query( $args );

if ( $query->have_posts() ) {
    echo "<div class='posts'>";

        while ( $query->have_posts() ) {
            $query->the_post();

            $f = get_fields();
            $link = ($f['bericht_doorverwijzen'] ?? '' ? $f['bericht_doorverwijzen'] : get_the_permalink());
            $img_id = get_post_thumbnail_id();

            $date = $f['datum_artikel'];

            echo "<div class='post w-1/3'>";
                echo "<div class='postWrapper'>";

                    if(!empty($img_id)) {
                        $img = Images::get_image_resized($img_id, 397, 230, true);
                        echo "<a href='".$link."' title='Ga naar ".get_the_title()."'>";
                            echo '<picture class="img">';
                                echo '<img src="'.$img[0].'" alt="'.get_post_meta($img_id, '_wp_attachment_image_alt', true).'"/>';
                            echo '</picture>';
                        echo "</a>";
                    }

                    echo "<div class='content'>";
                        echo "<h2><a href='".$link."' title='Ga naar ".get_the_title()."'>".get_the_title()."</a></h2>";
                        if(!empty($date)) {
                            echo "<p class='date'>". $date ."</p>";
                        }
                        echo the_excerpt_max_charlength($charlength = 130);
                    echo "</div>";
                echo "</div>";
            echo "</div>";
        }
    echo "</div>";
}
wp_reset_postdata();

我在网上找到了一些示例,但似乎没有一个适用于我的代码。当月份发生变化时,如何在帖子之间添加月份名称,我可以创建一个包含所有月份和锚点的额外导航,以便跳转到正确的帖子?

【问题讨论】:

  • 查看上面的orderBy 字段,我发现您正在按降序使用meta_value。将meta_value 更改为datum_artikel 怎么样?
  • 帖子的顺序是我需要的,我唯一想要的是当两个帖子之间的月份变化时在不同帖子之间显示一个标题。
  • 那你要实现一个经典的control break。您只需将当前记录的标准值(月份,分别是年份和月份的组合)与前一个记录的值进行比较 - 如果它们不同,则首先输出月份,然后继续输出数据当前记录。
  • $date 是什么格式的?你能贴一个例子吗?
  • $date 的格式如下:j F Y

标签: php css wordpress advanced-custom-fields


【解决方案1】:

我在网上找到了另一个似乎可以满足我需要的示例:

$query = new WP_Query( array(
'post_type'     => 'post',
'post_status'   => 'publish',
'meta_key'      => 'datum_artikel',
'orderby'       => 'meta_value',
'order'         => 'DESC',
) );

if ( $query->have_posts() ) {
    $current_header = "";

    echo "<div class='posts'>";

        while ( $query->have_posts() ) {
            $query->the_post();
            $f = get_fields();
            $date = $f['datum_artikel'];
            $temp_date = get_post_meta( get_the_ID(), 'datum_artikel', true);
            $pretty_month = date_i18n("F Y", strtotime($temp_date));

            if ($pretty_month != $current_header) {
                $current_header = $pretty_month;
                echo "<h2>". $pretty_month ."</h2>";
            }


            $link = ($f['bericht_doorverwijzen'] ?? '' ? $f['bericht_doorverwijzen'] : get_the_permalink());
            $img_id = get_post_thumbnail_id();

            echo "<div class='post w-1/3'>";
                echo "<div class='postWrapper'>";

                    if(!empty($img_id)) {
                        $img = Images::get_image_resized($img_id, 397, 230, true);
                        echo "<a href='".$link."' title='Ga naar ".get_the_title()."'>";
                            echo '<picture class="img">';
                                echo '<img src="'.$img[0].'" alt="'.get_post_meta($img_id, '_wp_attachment_image_alt', true).'"/>';
                            echo '</picture>';
                        echo "</a>";
                    }

                    echo "<div class='content'>";
                        echo "<h2><a href='".$link."' title='Ga naar ".get_the_title()."'>".get_the_title()."</a></h2>";
                        if(!empty($date)) {
                            echo "<p class='date'>". $date ."</p>";
                        }
                        echo the_excerpt_max_charlength($charlength = 130);
                        echo "<a href='".$link."' title='Ga naar ".get_the_title()."' class='link'>Lees meer</a>";
                    echo "</div>";
                echo "</div>";
            echo "</div>";
        }
        echo "</div>";
}
wp_reset_postdata();

【讨论】:

    【解决方案2】:
    $today = date("Y/m/j");
    $args = (array(
        'post_type' => 'post',
        'posts_per_page'    =>  -1,
        'meta_key' => 'datum_artikel',
        'orderby' => 'meta_value',
        'order' => 'DESC',
        'meta_query' => array(
            array(
                'key' => 'datum_artikel',
                'value' => $today,
                'compare' => '<=',
                'type' => 'CHAR'
            )
        )
    ));
    $query = new WP_Query($args); 
    

    【讨论】:

      猜你喜欢
      • 2015-08-07
      • 1970-01-01
      • 2021-11-03
      • 2022-10-23
      • 1970-01-01
      • 2017-07-26
      • 2014-09-13
      • 2018-12-31
      • 1970-01-01
      相关资源
      最近更新 更多