开发者

Dynamically adding meta tags using php

开发者 https://www.devze.com 2023-01-03 10:02 出处:网络
In my site I have a lis开发者_StackOverflow社区t 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.

In my site I have a lis开发者_StackOverflow社区t 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消