I need to remove the last comma from the tag list
Put all $keyword->name
into an array and use implode()
to get the result.
I’m unable to load the content of the post or page
Object $post
is not available in your function. Use global $post;
to access $post->ID
and $post->post_content
etc.
So,
public function add_meta_keywords(){
if( is_single() ){
$post_tags = get_the_tags( $post->ID );
$key = '';
foreach( $post_tags as $keyword ){
$key .= $keyword->name.', ';
}
echo '<meta name="keywords" content="'.$key.'">';
}
}
should be something like this
public function add_meta_keywords(){
if( is_single() ){
global $post;
$post_tags = get_the_tags( $post->ID );
$key = array();
foreach( $post_tags as $keyword ){
$key []= $keyword->name;
}
echo '<meta name="keywords" content="'.implode(', ',$key).'">';
}
}
And
public function add_meta_description(){
if( is_single() ){
#$description = strip_tags( $post->post_content );
$d = get_the_content( $post->ID );
#$description = strip_shortcodes( $post->post_content );
#$description = str_replace( array("\n", "\r", "\t"), ' ', $description );
#$description = substr( $description, 0, 125 );
#echo '<meta name="description" content="'. $description .'">';
var_dump($d);
}
}
should be
public function add_meta_description(){
global $post;
if( is_single() ){
global $post;
#$description = strip_tags( $post->post_content );
$d = get_the_content( $post->ID );
#$description = strip_shortcodes( $post->post_content );
#$description = str_replace( array("\n", "\r", "\t"), ' ', $description );
#$description = substr( $description, 0, 125 );
#echo '<meta name="description" content="'. $description .'">';
var_dump($d);
}
}
EDIT:
Better solution to trim the description to 140 characters.
TRY this
// Checks the content's length, if more 140 chars...
// Output 140 chars, but don't break a word
if ( strlen($description) > 140){
$description = substr($description, 0, strpos($description, ' ', 140));
}
echo '<meta name="description" content="'. $description .'">';
I hope this helps.