我需要从标记列表中删除最后一个逗号
全部放置$keyword->name
放入数组并使用implode()
以获得结果。
我无法加载帖子或页面的内容
对象$post
在您的函数中不可用。使用global $post;
访问$post->ID
和$post->post_content
等
所以
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.\'">\';
}
}
应该是这样的
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).\'">\';
}
}
以及
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);
}
}
应该是
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: 更好的解决方案是将描述缩短到140个字符。
试试这个
// 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 .\'">\';
我希望这有帮助。