将category_description放在元描述中(wordpress)

发布于 2024-11-02 10:36:15 字数 876 浏览 0 评论 0原文

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

<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

記柔刀 2024-11-09 10:36:15

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

<?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 实体,当然我认为搜索引擎根本不会介意换行符或 &&

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

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

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

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 ?>" />

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 &.

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:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文