【问题标题】:php make div class 'header-ads' only display on wordpress homepagephp 使 div 类 'header-ads' 仅显示在 wordpress 主页上
【发布时间】:2018-07-11 20:04:25
【问题描述】:

在我的wordpress blog 上有一个名为header-ads 的特定div 类我怎样才能使下面的HTML sn-p 仅使用php 显示在我的网站主页上,以及我应该将代码放入哪个主题文件。

由于使用 display:none css 属性违反了 adsense 策略,我需要使用 php。因为假设发布者已将其删除,则整个广告代码 sn-p 将从帖子的 html 中完全删除。

<div class="header-ads">

<!--Ad Code-->

</div>

【问题讨论】:

标签: php html wordpress


【解决方案1】:

像这样创建:

# 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()一样调用它,它会渲染

【讨论】:

    猜你喜欢
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多