eassae
Member
Posted 11 months ago #
Hello,
I am trying to use my post excerpt as og:description meta data. I am using some code I found with a minor modification. The problem I am having is that post_excerpt seems to return nothing.
Any help would be appreciated.
<?php
function og_meta_desc() {
global $post;
$meta = strip_tags($post->post_excerpt);
$meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);
$meta = substr($meta, 0, 200);
if ( $meta != '' ) {
echo "<meta property=\"og:description\" content=\"$meta\" />";
} else {
echo "<meta property=\"og:description\" content=\"WTF\" />";
}
}
og_meta_desc();
?>
eassae
Member
Posted 11 months ago #
Solved:
<?php
function og_meta_desc() {
global $post;
$meta = strip_tags($post->post_content);
$meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);
$meta = substr($meta, 0, 200);
if ( $meta != '' ) {
echo "<meta property=\"og:description\" content=\"$meta...\" />";
} else {
echo "<meta property=\"og:description\" content=\"$Here is some cool new content. Check it out.\" />";
}
}
og_meta_desc();
?>
eassae
Member
Posted 11 months ago #
Final:
<?php
function og_meta_desc() {
global $post;
$meta = strip_tags($post->post_content);
$meta = strip_shortcodes($meta);
$meta = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), ' ', $meta);
$meta = substr($meta, 0, 200);
if ( $meta != '' ) {
echo "<meta property=\"og:description\" content=\"$meta...\" />";
} else {
echo "<meta property=\"og:description\" content=\"Here is some cool new content. Check it out.\" />";
}
}
og_meta_desc();
?>