Display ads in articles published for more than 15 days in WordPress

Time:2020-2-25

Step one:

The function is very simple. Open the functions.php file of the current template with the PHP editor.

Copy the following code in:

Copy code

The code is as follows:

function is_old_post($post_id=null){
$days = 15;
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;
}

The second step:

Open the single.php file and copy in the following code:

Copy code

The code is as follows:

< ?php if(is_old_post()){ ?>
INSERT AD CODE HERE
< ?php } ?>

After the modification, the advertisement will only be displayed in the log with a release date of more than 15 days, which can be modified. In the code copied to functions.php, there is a sentence: $days = 15; here is $days = num; the value of num can be set at will.

Recommended Today

Laravel service container must know

The article was forwarded from the professional laravel developer community. Original link: https://learnku.com/laravel/t To learn how to build an application with laravel is not only to learn how to use different classes and components in the framework, but also to remember allartisanCommand or all helper functions (we have Google). Learning to code with laravel is […]