将category_description放在元描述中(wordpress)
在我的主题标题中,我为元描述创建了以下代码:
<?php if (is_single() || is_page()) { ?>
<meta name="description" content="<?php echo metadesc($post->ID); ?>" />
<?php }else{ ?>
<meta name="description" content="<?php bloginfo('description'); ?>" />
<?php } ?>
并将此代码包含在我的 function.php 中:
function metadesc($pid) {
$p = get_post($pid);
$description = strip_tags($p->post_content);
$description = str_replace ("\n","",$description);
$description = str_replace ("\r","",$description);
if (strlen($description) > 150) {
return htmlspecialchars(substr($description,0,150) . "...");
}else{
return htmlspecialchars($description);
}
}
现在我还想将 Category_description 纳入主题标题:
<?php if ( is_category() ) { echo category_description(); } ?>
你能帮我吗?谢谢
Header in my theme I created the following code for the meta description:
<?php if (is_single() || is_page()) { ?>
<meta name="description" content="<?php echo metadesc($post->ID); ?>" />
<?php }else{ ?>
<meta name="description" content="<?php bloginfo('description'); ?>" />
<?php } ?>
and included this code in my function.php:
function metadesc($pid) {
$p = get_post($pid);
$description = strip_tags($p->post_content);
$description = str_replace ("\n","",$description);
$description = str_replace ("\r","",$description);
if (strlen($description) > 150) {
return htmlspecialchars(substr($description,0,150) . "...");
}else{
return htmlspecialchars($description);
}
}
now I want to also incorporate category_description the theme header :
<?php if ( is_category() ) { echo category_description(); } ?>
can you help me how can I do? thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经完成了大部分工作:
如您所见,我会忘记您在
metadesc()
中所做的所有清理工作,只需使用strip_tags()
删除 html >,但我认为没有必要删除换行符或转换为 html 实体,当然我认为搜索引擎根本不会介意换行符或&
或&
。另外,无需检查描述的长度。只要尝试截断它,如果它的长度小于 150 个字符,
substr()
将返回整个字符串不变。编辑:
回应您的评论,如果您更喜欢使用您正在使用的
metadesc()
函数,您可以这样做:You already have the work mostly done:
As you see, I would forget about all the cleaning you are doing in
metadesc()
, just get rid of the html withstrip_tags()
, but I see no need to remove linebreaks or to translate to html entities, certainly I don't think search engines will mind at all about a line break or either a&
or a&
.Plus, there is no need to check the length of the description. Just try to truncate it, if its length is less than 150 chars,
substr()
will return the whole string untouched.EDIT:
Responding to your comment, you can do it this way if you prefer to use the
metadesc()
function you were using: