首页 文章

如何从Wordpress插件向页面模板添加元标记?

提问于
浏览
1

我想添加一个像这样的元标记:

<meta name="key" content="value" />

到Wordpress中的某些页面 . 我知道,我可以将它添加到我的模板中,它会显示出来 . 但问题是, I am not allowed to even touch the template. It's totally template independent.

所以,我必须通过在我的插件代码中执行某些操作来添加元标记 . 我试过 wp_head 动作挂钩,但它不起作用 . 任何想法的解决方法或任何东西,以动态地获取页面的head标签内的元标记 .

我在做什么

我正在做的是有点不同 . 我的博客主要内容页面和摘要页面有两个页面 . 这两个页面都通过短代码获取数据 . 因此,主内容页面有一个短代码

[mainpage]

摘要页面中包含此短代码

[summarypage]

短代码已添加到主插件文件中

add_shortcode( 'mainpage', 'mainPage' );
add_shortcode( 'summarypage', 'summaryPage' );

现在,在我的插件目录中,我有两个名为 mainpage.phpsummarypage.php 的php文件,它们返回html内容 .

在mainpage.php中

function mainPage() {
    // Code which generates html content
    $mainpage .= 'content';
    return $mainpage;
}

同样,在summarypage.php中

function summaryPage() {
    // Code which generates HTML content
    $summarypage .= 'content';
    return $summarypage;
}

因为,$ mainpage和$ summarypage包含了进入页面textarea框内的所有内容 . 我不知道如何在主页面或摘要页面中添加一些元信息 . 在函数 mainPage()summaryPage() 中使用 wp_head 不起作用,这是正确的 . 那么,我怎样才能在页面的head部分中获得一个元标记?

2 回答

  • 3

    如果你向我们展示了你已经尝试过的东西,我们可以更好地帮助你 . 这是一个工作示例:

    <?php
    /*
    Plugin Name: No automagic phone numbers
    Description: Adds <meta> elements to the <head> to prevent the Skype toolbar and the iPhone from autolinking.
    Version: 0.1
    Author: Thomas Scholz
    Author URI: http://toscho.de
    Created: 01.04.2010
    */
    
    if ( ! function_exists('no_automagic_phone_numbers') )
    {
        function no_automagic_phone_numbers()
        {
            /* Prevent the Skype plugin and the iPhone from randomly parsing numbers
             * as phone numbers: */
            ?>
    <meta name="SKYPE_TOOLBAR" content="SKYPE_TOOLBAR_PARSER_COMPATIBLE">
    <meta name="format-detection" content="telephone=no">
            <?php
            if ( ! is_single() and ! is_page() )
            {
                // execute archive stuff
            }
            else
            {
                // single page stuff
            }
        }
        add_action('wp_head', 'no_automagic_phone_numbers');
    }
    

    现在我想知道为什么你被允许安装插件而不是改变主题...... :)

  • 0

    这是否适用于类似的东西

    function addmeta()
    {
    
      echo "\t<meta name='keywords' content='xxxxxxxxxxxxxxxxx' />\n";
    
    }
    
    function shortcodeTemplate($atts) //SHOWS THE SHORTCODE AND RENDERS THE VIEW MODE FOR THE GIVEN VIEW ID
    {
     add_action('wp_head', 'addmeta');
    echo showTemplate($atts['id']);
    }
    

    正在调用短代码模板以呈现短代码....这是在帖子中 .

相关问题