带有Open Graph标签的Meta标签助手

时间:2011-12-11 03:02:30

标签: php facebook codeigniter opengraph metatag

如何将Codeigniter元标记helperFacebook开放图元标记一起使用?

元标记助手想要将名称注入Facebook不使用的第一个属性。有没有办法使用帮助器添加我自己的自定义方案?似乎有一些默认值,如'name',我无法摆脱使用帮助。

我做了什么:

$meta = array(
        array('property' => 'og:type', 'content' => 'movie'),
        array('property' => 'og:video:height', 'content' => '260'),
        array('property' => 'og:video:width', 'content' => '420'),
        array('property' => 'og:video:type', 'content' => 'application/x-shockwave-flash'),
        array('property' => 'og:title', 'content' => $data['video']),
        array('property' => 'og:description', 'content' => $data['video_desc']),
        array('property' => 'og:image', 'content' => 'http://i2.ytimg.com/vi/'.$data['links']['extra_link_info']['original_key'].'/hqdefault.jpg'),
        array('property' => 'og:video', 'content' => 'https://www.youtube.com/v/'.$data['links']['extra_link_info']['original_key'].'?version=3&autohide=1&autoplay=1')
        );

我得到了什么:

<meta name="" content="movie" />

<meta name="" content="260" />

<meta name="" content="420" />

<meta name="" content="application/x-shockwave-flash" />

<meta name="" content="xxxxxxxx" />

<meta name="" content="xxxxxxxx" />

<meta name="" content="http://i2.ytimg.com/vi/xxxxxxxx/hqdefault.jpg" />

<meta name="" content="xxxxxxxx" />

我想要的是什么:

<meta property="og:tyoe" content="movie" />

<meta property="og:video:height" content="260" />

<meta property="og:video:width" content="420" />

<meta property="og:video:type" content="application/x-shockwave-flash" />

<meta property="og:title" content="xxxxxxxx" />

<meta property="og:description" content="xxxxxxxx" />

<meta property="og:image" content="http://i2.ytimg.com/vi/xxxxxxxx/hqdefault.jpg" />

<meta property="og:video" content="xxxxxxxx" />

2 个答案:

答案 0 :(得分:2)

我用过这个

<强> MY_html_helper.php

if (!function_exists('meta')) {

function meta($name = '', $content = '', $type = 'name', $newline = "\n") {
    // Since we allow the data to be passes as a string, a simple array
    // or a multidimensional one, we need to do a little prepping.
    if (!is_array($name)) {
        $name = array(array('name' => $name, 'content' => $content, 'type' => $type, 'newline' => $newline));
    } else {
        // Turn single array into multidimensional
        if (isset($name['name'])) {
            $name = array($name);
        }
    }

    $str = '';
    foreach ($name as $meta) {
        if ((!isset($meta['type']) OR $meta['type'] == 'name')) {
            $type = 'name';
        } else if ($meta['type'] == 'equiv') {
            $type = 'http-equiv';
        } else {
            $type = $meta['type'];
        }
        $name = (!isset($meta['name'])) ? '' : $meta['name'];
        $content = (!isset($meta['content'])) ? '' : $meta['content'];
        $newline = (!isset($meta['newline'])) ? "\n" : $meta['newline'];

        $str .= '<meta ' . $type . '="' . $name . '" content="' . $content . '" />' . $newline;
    }

    return $str;
}

}

用法:

$meta = array(
    array('name' => 'og:title', 'content' => 'my great title', 'type' => 'property')
);

答案 1 :(得分:1)

修正了它:

扩展HTML帮助程序并将所有name引用更改为property

<强> MY_html_helper

<?php

/**
 * Generates meta tags from an array of key/values
 *
 * @access  public
 * @param   array
 * @return  string
 */
if ( ! function_exists('meta'))
{
    function meta($property = '', $content = '', $type = 'property', $newline = "\n")
    {
        // Since we allow the data to be passes as a string, a simple array
        // or a multidimensional one, we need to do a little prepping.
        if ( ! is_array($property))
        {
            $property = array(array('property' => $property, 'content' => $content, 'type' => $type, 'newline' => $newline));
        }
        else
        {
            // Turn single array into multidimensional
            if (isset($property['property']))
            {
                $property = array($property);
            }
        }

        $str = '';
        foreach ($property as $meta)
        {
            $type       = ( ! isset($meta['type']) OR $meta['type'] == 'property') ? 'property' : 'http-equiv';
            $property       = ( ! isset($meta['property']))     ? ''    : $meta['property'];
            $content    = ( ! isset($meta['content']))  ? ''    : $meta['content'];
            $newline    = ( ! isset($meta['newline']))  ? "\n"  : $meta['newline'];

            $str .= '<meta '.$type.'="'.$property.'" content="'.$content.'" />'.$newline;
        }

        return $str;
    }
}