将category_description放置在元描述中(wordpress) [英] Place category_description in the meta description (wordpress)

查看:89
本文介绍了将category_description放置在元描述中(wordpress)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主题标题中,我为元描述创建了以下代码:

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 } ?>

并将此代码包含在我的function.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);
 }
}

现在我还想将category_description包含在主题标题中:

now I want to also incorporate category_description the theme header :

<?php if ( is_category() ) { echo category_description(); } ?>

你能帮我怎么办?谢谢

推荐答案

您已经完成了大部分工作:

You already have the work mostly done:

<?php
    if( is_single() || is_page() ) $description = strip_tags($post->post_content);
    elseif( is_category() ) $description = category_description();
    else $description = get_bloginfo( 'description' );
    $description = substr($description,0,150);
?>

<meta name="description" content="<?= $description ?>" />

如您所见,我会忘记您在metadesc()中所做的所有清理工作,只需使用strip_tags()删除html,但我认为无需删除换行符或转换为html实体,当然我不要认为搜索引擎会介意换行或&&amp;.

As you see, I would forget about all the cleaning you are doing in metadesc(), just get rid of the html with strip_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 &amp;.

此外,无需检查描述的长度.只需尝试截断它,如果它的长度小于150个字符,substr()将返回完整的整个字符串.

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.

回应您的评论,如果您更喜欢使用所使用的metadesc()函数,可以这样做:

Responding to your comment, you can do it this way if you prefer to use the metadesc() function you were using:

function metadesc() {
    global $post;

    if( is_single() || is_page() ) $description = strip_tags($post->post_content);
    elseif( is_category() ) $description = category_description();
    else $description = get_bloginfo( 'description' );

    $description = substr($description,0,150);

    return $description;
}

这篇关于将category_description放置在元描述中(wordpress)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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