Dynamically adding meta tags using php

2019-02-15 18:14发布

In my site I have a list of categories and I have to put meta keywords and description for them.I have a single page where I will retrieve the categories from the database.

Can anyone tell me how to make this much simpler to put meta tags for all the categories.

Regards, Rekha http://hiox.org

1条回答
乱世女痞
2楼-- · 2019-02-15 18:46

I'm not sure if this is what you are looking for but...

I have a simple script I created to dynamically populate the meta keywords with random keywords taken from an array.

Put this in the header of your template file.

<meta name="keywords" content="<?php get_keywords()?>" />

This will create a comma delimited list of no more than 10 keywords from an array of keywords. If you wanted to avoid a database query each time you could hard-code arrays of possible keywords for each category. If you don't mind a query, you could replace the array with a query which returns an array.

function get_keywords(){
    $keywords=array('keyword1','keyword2','keyword3','keyword4','keyword5');
    if (count($keywords)<10)
        $max=count($keywords);
    else
        $max=10;
    $rand_keys = array_rand($keywords, $max);
    foreach($rand_keys as $vals){
        $keyword[]=$keywords[$vals];
    }
    echo implode(", ", $keyword);
}

Hope this helps.

查看更多
登录 后发表回答