Search code examples
codeignitermeta-tags

Adding Meta data to a codeigniter view from a controller


In my controller I have added this code.For different view files there are different functions.How can I add meta tags for different function for different views? This is my controller:

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

    }


}
?>

Solution

  • 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; ?>" />
    

    Hopefully it will help you. or if you need any help please comment below.