get_the_content()
必须在里面the loop, 在标题中。php您可以这样做(不要忘记将内容替换为属性):
if (is_single()) {
while (have_posts()) {
the_post();
$content = get_the_content();
$desc=\'<meta property="og:description" content="\'. esc_attr($content) .\'">\';
echo $desc;
}
}
或者更好,在你的功能上。php钩住wp\\U头部动作;此外,我建议使用摘录而不是内容作为描述符。(注意使用global$post和
setup_postdata).
add_action( \'wp_head\', \'my_wp_head\' );
function my_wp_head() {
if (is_single()) {
$post_id = get_queried_object_id():
$excerpt = get_the_excerpt( $post_id );
$desc = \'<meta property="og:description" content="Blabla\'. esc_attr( $excerpt ) .\'">\';
echo $desc;
}
//More stuff to put in <head>
}