META Description in Header

6.36K viewsCore
1

I’m trying to get the META Description to be the Question Description in the header. I’m struggling! 😀

I’ve tried this..

<?php if (is_question()) { ?>
<meta name="description" content="<?php ap_post_status_description(ap_question_get_the_ID());	?>"/>
<?php } ?>

…and a few other things without sucess. Any advise?

function get_post_excerpt_by_id( $post_id ) {
global $post;
$post = get_post( $post_id );
setup_postdata( $post );
$the_excerpt = get_the_excerpt();
wp_reset_postdata();
return $the_excerpt;
}
get_post_excerpt_by_id(ap_question_get_the_ID()) something along this lines should work as well.
Why would you try to use ap_post_status_description that renders html into the meta tag anyway?

Thats the code the developer tries to use in the theme header, well something similar without success already.

1
function get_post_excerpt_by_id($post_id) {
	global $post;
	$post = get_post($post_id);
	setup_postdata($post);
	$the_excerpt = get_the_excerpt();
	wp_reset_postdata();
	return $the_excerpt;
}
add_action('wp_head', 'd_developer_of_matt_please');
function d_developer_of_matt_please() {
    if (function_exists('is_question') && is_question()) {
        echo '<meta name="description" content="'. get_post_excerpt_by_id(get_question_id()).'"/>';
    }
}

Where should I put this? Header?

functions.php
Just give it to your developer, he knows what to do.

I’m my own developer. I muddle by. The theme was created by the owner of Anspress – although its mega glitchy.

Have some PHP / CSS experience so I’ve been able to get it to work. Sorta.

You are viewing 1 out of 3 answers, click here to view all answers.