修改作者动态显示的文章类型

2024-02-01 13:45 小编

B2 主题带了一个【作者面板】小工具,可以在外观 – 小工具界面,添加到包含“内页”字样的小工具区域:

然后在前端侧边栏就会显示出来:

内部绝密传真282期

如果你点击上面的【Ta的全部动态】就可以看到关于该作者发布的最新消息,默认包含'文章post','圈子circle','文档document','快讯newsflashes'四种文章类型的动态,如果你需要新增或删除某些文章类型,那就是我们这个教程的用意了。

查看B2主题的 b2/Pages/tastream.php 文件,我们可以看到预留了一个钩子 b2_stream_author_post_type,代码片段如下:

$args = array(    'post_type'=>apply_filters('b2_stream_author_post_type', array(        'post','circle','document','newsflashes'    )),    'posts_per_page' => $count,    'orderby' => 'modified',    'offset'=>$offset,    'author'=>$author_id,    'post_status'=>'publish',    'include_children' => true,    'paged'=>$paged);

所以,我们通过钩子就可以修改了,具体的代码片段如下,可添加到子主题的 functions.php 中:

/** 修改作者动态的文章类型* https://www.wpdaxue.com/docs/b2/b2-dev/stream-author-post-type*/function b2child_stream_author_post_type( $post_types ) {        $post_types = array('post','circle','document','newsflashes','shop');        return $post_types;    }add_filter('b2_stream_author_post_type', 'b2child_stream_author_post_type' );

注意看代码的第7行的数组array()的值,就是你希望显示的文章类型数组,在上面的的样例中,我们添加了【商品shop】这个类型。

如果你要查看文章类型的值,可以在后台点击对应文章类型导航菜单下的第一个子菜单,比如页面-全部页面,就可以在网址中看到 /wp-admin/edit.php?post_type=page,其中 post_type= 后面的值,就是文章类型的值了,比如页面就是 page

注意,以上代码只会影响【Ta的全部动态】页面的文章类型,下图的【作者小工具】显示的文章类型不会改变,因为这个小工具没有添加钩子。

内部绝密传真282期

解决办法就是我们要添加钩子给它,在 b2/Modules/Templates/Widgets/Author.php 文件的 166 行左右的位置,有设置文章类型的代码:

'post_type'=>array('post','circle','document','newsflashes','shop'),

将上面的代码替换为下面的,保存即可:

'post_type'=>apply_filters('b2_stream_author_post_type', array('post','circle','document','newsflashes','shop')),


在线咨询 拨打电话

电话

13363039260

内部绝密传真282期

微信二维码