2024年10月31日 建站教程
wordpress如何只在发布超过30天的文章中显示广告,30天内的文章中不显示广告,下面web建站小编给大家详细介绍一下具体代码!
1、打开当前主题functions.php文件,新增以下代码:
function is_old_post($post_id=null){
$days = 30; //天数
global $wp_query;
if(is_single() || is_page()) {
if(!$post_id) {
$post_id = $wp_query->post->ID;
}
$current_date = time();
$offset = $days *60*60*24;
$post_id = get_post($post_id);
$post_date = mysql2date('U',$post_id->post_date);
$cunning_math = $post_date + $offset;
$test = $current_date - $cunning_math;
if($test > 0){
$return = true;
}else{
$return = false;
}
}else{
$return = false;
}
return $return;
}
2、打开文章模板single.php,新增以下代码:
<?php if(is_old_post()){ ?>
//在此处插入广告代码
<?php } ?>
本文链接:http://so.lmcjl.com/news/16619/