【问题标题】:How do I add a meta tag to the page template from a WordPress plugin?如何从 WordPress 插件向页面模板添加元标记?
【发布时间】:2010-04-01 11:11:08
【问题描述】:

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

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

到 WordPress 中的某些页面。我知道,我可以将它添加到我的模板中,它会显示出来。但问题是,我什至不允许触摸模板。它完全独立于模板。

所以,我只能通过在我的插件代码中做一些事情来添加元标记。我试过wp_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 包含了页面文本区域框内的所有内容。我不知道如何将一些元信息添加到我的主页或摘要页面。在函数mainPage()summaryPage() 中使用wp_head 不起作用,这是正确的。 那么,如何在页面的 head 部分中获取元标记?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    如果您向我们展示您已经尝试过的内容,我们可以为您提供更好的帮助。 这是一个工作示例:

    <?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');
    }
    

    现在我想知道为什么您可以安装插件但不能更改主题... :)

    【讨论】:

    • 客户有这样的要求……没办法!
    • 在我直接输出 HTML 的地方,您可以检查 is_single() 或 is_page() 和 echo mainPage();。见codex.wordpress.org/Conditional_Tags
    • 检查 is_single() 或 is_page() 如何帮助我添加 标签。请解释!
    • 哇!谢谢你!我已经实现了上面的代码,它的工作方式和我想要的完全一样。
    【解决方案2】:

    这是否适用于类似于

    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']);
    }
    

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

    【讨论】:

    • 短代码必须返回一个字符串,它们不能使用echoprint
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多