Dynamically adding meta tags using php

孤者浪人 提交于 2019-11-30 19:24:18

问题


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:


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.



来源:https://stackoverflow.com/questions/3028211/dynamically-adding-meta-tags-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!