过滤页眉中显示的博客标题

时间:2016-11-08 作者:Iurie

我想过滤标题中显示的博客标题,以将不同的CSS样式应用于不同的标题部分/单词,因此我在我(WP 4.7)的217个子主题的函数中添加了以下函数。php,这非常有效。问题是此函数还将CSS代码添加到标题栏中显示的元标题中。如何修复此问题?

/** Format the site title parts **/
add_filter( \'bloginfo\', \'format_site_title_parts\', 10, 2 );
function format_site_title_parts( $text, $show ){
   if (\'name\' == $show) {
      $text = "<span class=\'info-style\'>Info</span>" . "<span class=\'psi-style\'>Psi</span>" . "<span class=\'md-style\'>.md</span>";
   }
   return $text;
}

title bar

2 个回复
最合适的回答,由SO网友:jgraup 整理而成

删除该过滤器/函数,并在PHP模板/页面文件中应用标记。如果需要帮助,请在输出标题的位置张贴。

下面是我如何使用类设置它的:

if ( ! class_exists( \'ThemeCustomizations\' ) ) {
    class ThemeCustomizations {
        static $inBody = false;

        public static function set_in_body_true() {
            static::$inBody = true;
        }

        public static function set_in_body_false() {
            static::$inBody = false;
        }

        public static function filter_bloginfo( $name, $show = null ) {
            if ( \'name\' == $show && static::$inBody ) {
                $name = "<span class=\'info-style\'>Info</span>" . "<span class=\'psi-style\'>Psi</span>" . "<span class=\'md-style\'>.md</span>";
                return "$name";
            } else {
                return $name;
            }
        }
    }
}

add_action( \'wp_head\',   array ( \'ThemeCustomizations\', \'set_in_body_true\' ), PHP_INT_MAX );
add_action( \'wp_footer\', array ( \'ThemeCustomizations\', \'set_in_body_false\' ), 0 );
add_action( \'bloginfo\',  array ( \'ThemeCustomizations\', \'filter_bloginfo\' ), 10, 2 );
静态,并使用函数的静态范围变量:

function prefix__is_in_body( $isTrue = null ) {

    // static initializer is false
    static $inBody = false;

    // only overwrite if boolean supplied
    if ( is_bool( $isTrue ) ) {
        $inBody = $isTrue;
    } 

    // return regardless of getter/setter
    return $inBody;
}

add_action( \'wp_head\',   function(){ prefix__is_in_body(true); }, PHP_INT_MAX );
add_action( \'wp_footer\', function(){ prefix__is_in_body(false); }, 0 );
add_action( \'bloginfo\',  function($name, $show = null){
    if ( \'name\' == $show && prefix__is_in_body() ) {
        $name = "<span class=\'info-style\'>Info</span>" . "<span class=\'psi-style\'>Psi</span>" . "<span class=\'md-style\'>.md</span>";
        return "$name";
    } else {
        return $name;
    }
}, 10, 2 );
魔法Singleton &;Factory 模式+PHP魔术方法。

if ( ! class_exists( \'Magic\' ) ) {
    class Magic {
        private static $__ = array ();
        public         $_  = array ();

        function __construct( $args = null ) {
            if ( is_array( $args ) ) {
                foreach ( $args as $k => $v ) {
                    $this->{$k} = $v;
                }
            }
            return $this;
        }

        public static function instance( $id = \'\', $args = null ) {
            if ( ! isset( self::$__[ $id ] ) ) {
                self::$__[ $id ] = new Magic($args);
            }
            return self::$__[ $id ];
        }

        public function __get( $k ) {
            return isset( $this->_[ $k ] ) ? $this->_[ $k ] : null;
        }

        public function __set( $k, $v ) {
            return $this->_[ $k ] = $v;
        }

        public function __call( $k, $a ) {
            if ( isset($this->_[ $k ]) && is_callable( $this->_[ $k ] ) ) {
                return call_user_func_array( $this->_[ $k ], $a );
            }
        }
    }
}
现在使用Magic 课堂上你可以这样做;

$m = Magic::instance( \'\', array (
    \'isBody\'             => false,
    \'action_wp_head\'     => function() { Magic::instance()->isBody = true;  },
    \'action_wp_footer\'   => function() { Magic::instance()->isBody = false; },
    \'filter_wp_bloginfo\' => function( $output, $show ) {
        return ( \'name\' == $show && Magic::instance()->isBody ) ? \'<span class="info-style">Info</span><span class="psi-style">Psi</span><span class="md-style">.md</span>\' : $output;
    },
));

add_action( \'wp_head\',   array ( $m, \'action_wp_head\' ), PHP_INT_MAX );
add_action( \'wp_footer\', array ( $m, \'action_wp_footer\' ), 0 );
add_filter( \'bloginfo\',  array ( $m, \'filter_wp_bloginfo\' ), 10, 2 );

SO网友:Iurie

这就是我最后使用的Alexandros Georgiou). 我不知道这是否是一个好的解决方案,但我喜欢有一个公共位置(functions.php)来控制我的子主题。

$twentyseventeen_child_in_body = false;

function twentyseventeen_child_action_wp_head_finished() {
    global $twentyseventeen_child_in_body;
    $twentyseventeen_child_in_body = true;
}
add_action( \'wp_head\', \'twentyseventeen_child_action_wp_head_finished\', PHP_INT_MAX );

function twentyseventeen_child_action_wp_footer_started() {
    global $twentyseventeen_child_in_body;
    $twentyseventeen_child_in_body = false;
}
add_action( \'wp_footer\', \'twentyseventeen_child_action_wp_footer_started\', 0 );

function twentyseventeen_child_filter_bloginfo( $name, $show = null ) {
    global $twentyseventeen_child_in_body;
    if ( \'name\' == $show && $twentyseventeen_child_in_body ) {
        $name = "<span class=\'info-style\'>Info</span>" . "<span class=\'psi-style\'>Psi</span>" . "<span class=\'md-style\'>.md</span>";
        return "$name";
    } else {
        return $name;
    }
}
add_filter( \'bloginfo\', \'twentyseventeen_child_filter_bloginfo\', 10, 2 );