每月获取 WordPress 帖子 [英] Get WordPress Post with each month

查看:26
本文介绍了每月获取 WordPress 帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能每个月从我的自定义帖子类型中获取所有帖子?

这是我现在的做法,但它只显示所有标题.

<?php $news = new WP_Query( array( 'post_type' => 'notice', 'posts_per_page' => 999, 'order' => 'ASC'));?><?php while ( $news->have_posts() ) : $news->the_post();?><div class="testi-archive-list"><a href="<?php the_permalink()?>"/><h1><?php the_title()?></h1></a><p><?php the_field('sub_title_noticias')?></p>

<?php endwhile ?>

我想要的是这样的:

一月:标题 1标题 2

二月:标题 1标题 2

等等.有人可以帮忙吗?

解决方案

为此,您需要做的就是从当前帖子的发布日期获取当前月份和年份,然后将其与上一个帖子的帖子进行比较日期,月份,然后如果飞蛾不匹配则显示日期,或者如果匹配则不显示

要做到这一点,您需要:

从当前帖子的发布日期获取月份.为此,请使用 get_the_time( 'F' )

使用 $wp_query->posts['this will be current post -1 ']->post 获取循环中的上一篇文章.

获取并比较两个帖子之间的月份

根据对比显示或不显示日期

像这样

 array('notice'),'posts_per_page' => 999, 'order' => 'ASC'));if( $news->have_posts() ) {while( $news->have_posts() ) {$news->the_post();$current_month = get_the_date('F');if( $news->current_post === 0 ) {the_date('F Y');}别的{$f = $news->current_post - 1;$old_date = mysql2date('F', $news->posts[$f]->post_date);如果($current_month != $old_date){the_date('F Y');}}//输出帖子的数据?><div class="testi-archive-list"><a href="<?php the_permalink(); ?>"/><h2><?php the_title();?></h2></a><p><?php the_field('sub_title_noticias');?></p>

<?php}//结束时}?>

How can i get all post from my custom post type with each month ?

Here is how i'm doing it now, but it's only showing all titles.

<?php $news = new WP_Query( array( 'post_type' => 'notice', 'posts_per_page' => 999, 'order' => 'ASC' ) ); ?>

<?php while ( $news->have_posts() ) : $news->the_post(); ?>
    <div class="testi-archive-list">
        <a href="<?php the_permalink()?>"/> <h1><?php the_title()?></h1></a>
        <p><?php the_field('sub_title_noticias')?></p>
    </div>

<?php endwhile ?>

What i want is something like this:

January: Title 1 Title 2

February: Title 1 Title 2

And so on. Can anyone help ?

解决方案

To do this, all you need to do is to get the current month and year from the current post's post date, and then comparing that with the previous post's post date, month and then either displaying the date if the moths don't match or not display it if they match

To accomplish this, you need to:

Get the month from the current post's post date. To achieve this, use get_the_time( 'F' )

Get the previous post in the loop with $wp_query->posts['this will be current post -1 ']->post.

Get and compare the months between the two posts

Display or do not display the date according to the comparison

Like this

<?php

$news = new WP_Query( array('post_type' => array( 'notice' ),'posts_per_page' => 999, 'order' => 'ASC' ));

if( $news->have_posts() ) {
    while( $news->have_posts() ) {

        $news->the_post();

        $current_month = get_the_date('F');

        if( $news->current_post === 0 ) { 
           the_date( 'F Y' );
        }else{ 

            $f = $news->current_post - 1;       
            $old_date =   mysql2date( 'F', $news->posts[$f]->post_date ); 

            if($current_month != $old_date) {
                the_date( 'F Y' );
            }

        }
        // output data for the post
        ?>
        <div class="testi-archive-list">
          <a href="<?php the_permalink(); ?>"/> <h2><?php the_title(); ?></h2></a>
          <p><?php the_field('sub_title_noticias'); ?></p>
        </div>
        <?php

    }//End while
 }
?>

这篇关于每月获取 WordPress 帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆