使用php动态添加元标记 [英] Dynamically adding meta tags using php

查看:136
本文介绍了使用php动态添加元标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站上,我有一个类别列表,我必须为它们添加元关键字和描述。我有一个页面,我将从数据库中检索类别。



/ p>

问候,
Rekha
< a href =http://hiox.org =nofollow noreferrer> http://hiox.org

解决方案

我不知道这是你正在寻找的,但是...



我创建了一个简单的脚本来动态填充元关键字使用随机关键字从数组中取出。



将其放在模板文件的标题中。

 < meta name =keywordscontent =<?php get_keywords()?> /> 

这将创建一个逗号分隔的列表,其中包含一系列关键字不超过10个关键字。如果您希望避免数据库查询,则每次可以对每个类别的可能关键字进行硬编码。如果您不介意查询,则可以使用返回数组的查询替换该数组。

  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);
}

希望这有帮助。


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

解决方案

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.

这篇关于使用php动态添加元标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆