我有一个密码get_the_title()
这是可行的,但是get_the_excerpt()
返回空。我怎样才能让它工作?
此代码位于名为“WP Facebook Open Graph protocol”的插件中。以下是我想更改的部分:
if (is_singular(\'post\')) {
if (has_excerpt($post->ID)) {
echo "\\t<meta property=\'og:description\' content=\'".esc_attr(strip_tags(get_the_excerpt($post->ID)))."\' />\\n";
}else{
echo "\\t<meta property=\'og:description\' content=\'". [?] ."\' />\\n";
}
}else{
echo "\\t<meta property=\'og:description\' content=\'".get_bloginfo(\'description\')."\' />\\n";
}
在这里,
has_excerpt
总是失败,并且
get_the_excerpt($post->ID)
不再工作(已弃用)。
那么,如何在那里显示摘录?
附言:我也在使用“高级摘录”插件
SO网友:Withers Davis
试试这个:
在函数中创建新函数。然后从任何地方调用它。
function get_excerpt_by_id($post_id){
$the_post = get_post($post_id); //Gets post ID
$the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt
$excerpt_length = 35; //Sets excerpt length by word count
$the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images
$words = explode(\' \', $the_excerpt, $excerpt_length + 1);
if(count($words) > $excerpt_length) :
array_pop($words);
array_push($words, \'…\');
$the_excerpt = implode(\' \', $words);
endif;
$the_excerpt = \'<p>\' . $the_excerpt . \'</p>\';
return $the_excerpt;
}
Here\'s a post describing the code.