将Meta数据添加到控制器的codeigniter视图中

问题描述 投票:4回答:5

在我的控制器中,我添加了这个代码。对于不同的视图文件,有不同的功能。如何为不同的视图添加不同功能的元标记?这是我的控制器:

<?php
class home extends CI_Controller {



    function index()
    {
                $data['meta_title'] = 'Tracenow | iOS Version';
                $data['meta_description'] = 'Responsive HTML5 Theme in iOS Style';
                $data['meta_keywords'] = 'responsive html5 theme, ios, android, material design, landing, application, mobile, blog, portfolio, bootstrap 3, css, jquery, flat, modern';
                $data['meta_author'] = '8Guild';
                $data['meta_viewport'] = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no';
        $this->load->view('home_page/home_page');


    }

        function about()
    {
        $this->load->view('home_page/about');

    }

        function blog()
    {
        $this->load->view('home_page/blog');

    }

        function blog_single()
    {
        $this->load->view('home_page/blog-single');

    }


}
?>
codeigniter meta-tags
5个回答
2
投票
For this you can simply use this in your controller:

$data['meta_title'] = 'Your meta title';
$data['meta_description'] = 'Your meta description';
$data['meta_keywords'] = 'Your meta keywords';

And you view should be like:

<title><?php echo $meta_title; ?></title>
<meta name="description" content="<?php echo $meta_description; ?>" />
<meta name="keywords" content="<?php echo $meta_keywords; ?>" />

希望它会对你有所帮助。或者如果您需要任何帮助,请在下面评论。


0
投票

你可以使用这个库Meta Tags


0
投票

在你的控制器上

$data['metas'] = array(
             array('name'=>'description', 'content'=>'A short but sweet DEFAULT description of this fine site'),
             array('name' =>'keywords', 'content'=>'some awesome DEFAULT keywords for those rascally web crawlers')
    );

在你的视图上

<?php 
      foreach($metas as $meta)
      {?>
         <meta name="<?=$meta['name']?>" content="<?=$meta['content']?>" />
<?php }?>

0
投票
class Home extends CI_Controller{
    public function __construct(){
        parent::__construct();
        $this->load->helper('html'); //Load global helper 'html' 
    }
[...]
//in controller: meta tag
$meta = array(
        array(
                'name' => 'robots',
                'content' => 'no-cache'
        ),
        array(
                'name' => 'description',
                'content' => 'My Great Site'
        ),
        array(
                'name' => 'keywords',
                'content' => 'love, passion, intrigue, deception'
        ),
        array(
                'name' => 'robots',
                'content' => 'no-cache'
        ),
        array(
                'name' => 'Content-type',
                'content' => 'text/html; charset=utf-8', 'type' => 'equiv'
        )
);

//HTML
<!DOCTYPE html>
<head>
    <title></title>
    <?= meta($meta) ?> //use helper HTML to show meta tag
</head>
// Generates:
// <meta name="robots" content="no-cache" />
// <meta name="description" content="My Great Site" />
// <meta name="keywords" content="love, passion, intrigue, deception" />
// <meta name="robots" content="no-cache" />
// <meta http-equiv="Content-type" content="text/html; charset=utf-8" />

Font:https://codeigniter.com/userguide3/helpers/html_helper.html?highlight=helper

0
投票

在你的控制器中:

$this->output->set_common_meta($page_title, $page_description, $page_keywords);  // for title, description, keywords
$this->output->set_meta('property_name', 'property_value');  // for other properties
© www.soinside.com 2019 - 2024. All rights reserved.