在 Wordpress 中显示具有特定日期和特定类别的 post 的文本

Show text for post with specific date and specific category in Wordpress

所以我试图在每个 post 的末尾显示文本,但仅从本月开始并且仅在一个类别中显示。我尝试了在这里找到的不同代码,但其中 none 确实有效或满足了我的需要。这是我喜欢做的事的例子。希望它有意义。

 if(in_category(tags) && date(from June 2015) )
    {
            ?>
        <p>See related news <a href="#">here</a>.</p>
       <p>Subscribe to our monthly e-newsletter <a href="#">here</a>.</p>

  <?php } ?>

如果我理解你的任务没有错的话你需要使用date_query along with tax_query

$args = array(
    'post_type'  => 'page', // 'post', 'page' or custom post type 'Name' Like `Products`
    'tax_query' => array(
                            array(
                                'taxonomy' => 'category_term', // your taxonomy name
                                'field' => 'slug', // slug or id 
                                'terms' => $catSlug
                            )
                        ),
    'date_query' => array(
            array(
                'year'  => date('Y'), // remove if you dont want
                'month' => date('m'),
                'day'   => date('d'), // remove if you dont want
            ),
        ),
);
$query = new WP_Query( $args );

echo '<pre>';print_r($query->posts);echo '</pre>';

要签入类别,您应该使用条件:

获取每个 post 日期的月份并将其与当前月份编号或硬编码月份编号进行比较,例如 June 使用 06 您可以检查 php 日期格式here

in_category 参数参见 this

if(in_category('cat_name') && date('m',strtotime($post->post_date)) == date('m'))
{

// your code to view post title 

}

检查下面的代码。在这里我使用了类别 'Uncategorized' 。你可以在那里使用你的标签。 希望这会有所帮助。在您的 functions.php

中添加此代码
add_filter( 'the_content', 'check_cat_date' );

function check_cat_date($content){

      $date    = '2015-06-01';
      $pub_Date =the_date( 'Y-m-d', '' ,'', $echo=false );
      $pub_Date = (string)$pub_Date; 

      $posted_date = strtotime($pub_Date);
      $flag_date   = strtotime($date);


if(in_category('Uncategorized')&&($posted_date) >= $flag_date)
    {


    $append = '<p>See related news <a href="#">here</a>.</p>
            <p>Subscribe to our monthly e-newsletter <a href="#">here</a>.</p>';
    $new_content = $content.$append;

    return $new_content;



    }
    else{
        return $content;
    }

}