Home  >  Article  >  CMS Tutorial  >  How does WordPress display ads on posts older than 15 days?

How does WordPress display ads on posts older than 15 days?

藏色散人
藏色散人Original
2020-01-03 09:36:552013browse

How does WordPress display ads on posts older than 15 days?

How does WordPress display ads on articles published more than 15 days ago?

WordPress displays ads in articles published more than 15 days ago

Recommended: "wordpress tutorial"

Assume that we need to display ads in articles published more than 15 days ago. We only need to simply set the code today to complete it.

Step one:

The function is very simple to implement. Use a PHP editor to open the functions.php file of the current template.

Copy the following code into it:

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; 
}

Step 2:

Open the single.php file and copy the following code into it:

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 logs with a release date of more than 15 days. Of course, this time 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 arbitrarily.

The above is the detailed content of How does WordPress display ads on posts older than 15 days?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn