在我的wordpress blog上,有一个名为with open('your_file') as fin:
data = list(fin)
r1 = random.choice(data)
r2 = random.choice(data)
# ...
的特定div类如何使下面的HTML代码段仅使用php显示在我的网站首页上,以及应该将哪个代码放在该主题文件中
由于使用header-ads
css属性违反了adsense政策,因此我需要使用php。假设发布者将其删除,则整个广告代码段将被从帖子的html中完全消除。
display:none
答案 0 :(得分:0)
创建如下:
# wp-content/plugins/homepage-ads
# wp-content/plugins/homepage-ads/homepage-ads.php
<?php
/**
* Plugin Name: Home Page Ads
* Description: Module to enable ads on home page
* Version: 1.0
* Author: Vendor
* License: GPL2
*/
class homepage_ads
{
private static $instance;
protected $templates;
private function __construct()
{
$this->templates = array();
if (version_compare(floatval(get_bloginfo('version')), '4.7', '<' )) {
add_filter('page_attributes_dropdown_pages_args', array($this, 'register_project_templates'));
} else {
add_filter('theme_page_templates', array($this, 'add_new_template'));
}
add_filter('wp_insert_post_data', array($this, 'register_project_templates'));
add_filter('template_include', array($this, 'view_project_template'));
}
public static function get_instance()
{
if (self::$instance == null) {
self::$instance = new homepage_ads();
}
return self::$instance;
}
# add templates added in __construct to wp
public function add_new_template($posts_templates)
{
$posts_templates = array_merge($posts_templates, $this->templates);
return $posts_templates;
}
public function register_project_templates($atts)
{
$cache_key = 'page_templates-'. md5(get_theme_root() .'/'. get_stylesheet());
$templates = wp_get_theme()->get_page_templates();
if (empty($templates)) {
$templates = array();
}
wp_cache_delete($cache_key , 'themes');
$templates = array_merge($templates, $this->templates);
wp_cache_add($cache_key, $templates, 'themes', 1800);
return $atts;
}
public function view_project_template($template)
{
if (is_search()) {
return $template;
}
global $post;
if (!$post) {
return $template;
}
if (!isset($this->templates[get_post_meta($post->ID, '_wp_page_template', true)])) {
return $template;
}
$file = plugin_dir_path( __FILE__ ). get_post_meta($post->ID, '_wp_page_template', true);
if (file_exists($file)) {
return $file;
} else {
echo $file;
}
# return template
return $template;
}
}
function add_home_page_ads()
{
$html = '<div class="ads">';
$html .= ' <img src="" />';
$html .= '</div>';
echo $html;
}
add_action('home_page_ads', 'add_home_page_ads');
现在您可以使用函数home_page_ads
,然后可以创建模板文件并像home_page_ads()
一样调用它,它将呈现