外贸独立站的底层设计密码WordPress 成品站建站

将内置的文章SEO功能添加到其他文章类型

  1. 主页
  2. 文档
  3. B2主题使用教程
  4. B2主题二开
  5. 将内置的文章SEO功能添加到其他文章类型

B2主题自带了一个简单文章SEO功能,允许设置文章的SEO标题、关键词和描述,如下图所示:

默认情况下,B2主题的这个功能只对某些文章类型生效,我们在 b2/Modules/Settings/Seo.php 文件中可以看到如下代码:

$seo_meta = new_cmb2_box(array( 
    'id'            => 'single_post_seo_side_metabox',
    'title'         => __( 'SEO设置', 'b2' ),
    'object_types'  => array( 'post','shop','page','document','links'), // Post type
    'context'       => 'side',
    'priority'      => 'high',
    'show_names'    => true,
));

在代码的第4行就是设置在哪些文章类型显示SEO设置,我们看到,B2 3.8.3 版本及以下版本的主题中,并没有添加过滤钩子。

所以,我们需要修改上面代码的第4行,添加一个钩子b2_single_seo_post_type,修改后的代码为:

$seo_meta = new_cmb2_box(array( 
    'id'            => 'single_post_seo_side_metabox',
    'title'         => __( 'SEO设置', 'b2' ),
    'object_types'  => apply_filters('b2_single_seo_post_type', array( 'post','shop','page','document','links')), // Post type
    'context'       => 'side',
    'priority'      => 'high',
    'show_names'    => true,
));

以上添加钩子的代码,倡萌已经提交给B2作者春哥,2022年9月12日以后发布的B2版本会默认包含。

有了钩子以后,我们就可以通过下面的代码片段修改文章类型,将代码添加到子主题的 functions.php 即可:

/*
* 将B2的SEO功能添加自定义文章类型
* https://www.wpdaxue.com/docs/b2/b2-dev/single-seo-post-type
*/
function b2child_single_seo_post_type( $post_types ) {
    
    $post_types = array('post','shop','page','document','links','newsflashes');
    
    return $post_types;
    
}
add_filter('b2_single_seo_post_type', 'b2child_single_seo_post_type' );

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

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

这篇文章对您有用吗? 5 1
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索