php使div类“标题广告”仅显示在wordpress主页上
在我的wordpress上有一个特定的div类叫做header ads,我如何才能使下面的HTML片段只显示在我的网站主页上,使用php,以及我应该将代码放在哪个主题文件中 由于使用display:nonecss属性是针对adsense策略的,所以我需要使用php。因为整个广告代码片段将从帖子的html中完全删除,假设发布者已经删除了它。 创建如下内容:php使div类“标题广告”仅显示在wordpress主页上,php,html,wordpress,Php,Html,Wordpress,在我的wordpress上有一个特定的div类叫做header ads,我如何才能使下面的HTML片段只显示在我的网站主页上,使用php,以及我应该将代码放在哪个主题文件中 由于使用display:nonecss属性是针对adsense策略的,所以我需要使用php。因为整个广告代码片段将从帖子的html中完全删除,假设发布者已经删除了它。 创建如下内容: # wp-content/plugins/homepage-ads # wp-content/plugins/homepage-ads/hom
# 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\u page\u ads,然后您可以创建一个模板文件,并像home\u page\u ads一样调用它,它将呈现创建一个函数,将其添加为一个操作,如果首页有可能重复此问题,则调用操作: