国内关于bbpress的资料实在是太少了,我好不容易弄好了插件和主题,却发现meta description的调用出现了问题。
百度之后,我还是啥都没有看到,只好用有道词典配合下去看下国外的资料,找到两个方法,但是我估计还得再修改下:
第一,在head区域加上这个,我尝试过,先不说版块的描述只能调用的默认设置的,(估计翻页也是一样)描述里面没有成功的去掉<p>标签,还出现了些许乱码。
<?php
if ($posts) {
foreach ($posts as $bb_post) : $del_class = post_del_class();
$old=ob_get_contents(); ob_clean(); ob_start(); // you may leave ob_start();
post_text();
$out.=ob_get_contents(); ob_clean();
endforeach;
$out = preg_replace('#<p[^>]*>(\s| ?)*</p>#', '', $out); // takes out <p>
$out = substr($out,0,200); // only displays the first 200 lines of the first post.
echo '<META NAME="Description" CONTENT="';
echo $out;
echo '">';
}
else {
echo '<META NAME="Description" CONTENT="This is your defalt description edit this to what ever you want ">'; // This displays when where is no post
}
global $tags;
if (!empty($tags)) {
$keywords=""; foreach ($tags as $t) {$keywords.=$t->raw_tag.', ';}
echo "\n".'<meta NAME="keywords" CONTENT="'.trim($keywords,", ").'">'."\n"; // This displays any tags associated with that post as a keyword
} ?>
第二,看到一个回帖说bbpress本身有函数支持的。(no need for all that object-based markey, bbPress has functions to pass as a string rather than echo...)
<?php
/*
Plugin Name: Meta Headers
*/
function meta_heads() {
global $posts, $tags;
if ( $posts ) {
$out = $posts[0]->post_text;
$out = strip_tags( $out );
$out = str_replace( array( "\n", "\r" ), ' ', $out );
$out = substr($out, 0, 200); // only display the first 200 characters of the first post
}
if( empty( $out ) )
$out = 'Default description';
echo "\n".'<meta name="description" content="'.$out.'" />';
if ( !empty( $tags ) ) {
$keywords = '';
foreach ($tags as $t) {
$keywords.=$t->raw_tag.', ';
}
echo "\n".'<meta name="keywords" content="'.substr($keywords,0,-2).'">'."\n"; // displays any tags associated with a topic as keywords
}
}
add_action( 'bb_head', 'meta_heads' );
这个我不晓得怎么用,bbpress模板文章里面没有找到function文件,是不是我要新建一个?易优老师,你又是怎么实现的呢?