How to Add post title to content description?

2019-09-02 05:59发布

Using this code

    <?php
$post = $wp_query->post;
$descrip = strip_tags($post->post_content);
$descrip_more = '';
if (strlen($descrip) > 155) {
$descrip = substr($descrip,0,155);
$descrip_more = ' ...';
}
$descrip = str_replace('"', '', $descrip);
$descrip = str_replace("'", '', $descrip);
$descripwords = preg_split('/[\n\r\t ]+/', $descrip, -1, PREG_SPLIT_NO_EMPTY);
array_pop($descripwords);
$descrip = implode(' ', $descripwords) . $descrip_more; $descrip =  strip_shortcodes($descrip );
echo '<meta name="description" content="'.$descrip.'">'; ?>

Which outputs something like this in my header:

<meta name="description" content="   Everything year life dont know why but ...">

I was wondering if I could add something to the code that would add the single post title before the "Everything year life dont know why but ..." part, at the beginning of the content, so it looks like

Maybe out it like this?

<meta name="description" content=" Uverworld - nano second (<-- the post title) Everything year life dont know why but     ...">

in other words

<meta name="description" content=" Uverworld - nano second Everything year life dont know why but     ...">

--

The above code also leaves a lot of spaces before the content and shows up like Is there a way to get rid of that as well?

标签: php wordpress
1条回答
再贱就再见
2楼-- · 2019-09-02 06:53

You could do the following:

<meta name="description" content="<?php echo get_the_title(); ?>  Everything year life dont know why but ...">
查看更多
登录 后发表回答