更改 wordpress hestia 主题的首页 section 顺序

更新日期: 2018-03-12 阅读次数: 11450 分类: wordpress

由于用的是 hestia 免费版,没法使用自动调整首页 section 的功能。hestia 的 pro 版本确实太贵了,买不起。所以,我决定手动修改一下代码,以调整 section 的顺序。

通过查看 front-page.php 发现了 hestia_sections,grep 一下 hestia_sections,会发现不少的代码段

$ grep hestia_sections -r .

./inc/sections/hestia-about-section.php: add_action( 'hestia_sections', 'hestia_about', absint( $section_priority ) );

./inc/sections/hestia-shop-section.php: add_action( 'hestia_sections', 'hestia_shop', absint( $section_priority ) );

...

./front-page.php: do_action( 'hestia_sections', false );

add_action 与 do_action 的区别

  • add_action( 'foo' ) 注册一个 callback
  • do_action( 'foo' ) 执行注册的 callback

add_action 的参数列表

  • $tag: action 名
  • $function_to_add: 调用的函数
  • $priority: 调用的优先级,数字越小优先级越高。默认值为 10。从 hestia 的使用习惯看,不要使用负数。
  • $accepted_args: function_to_add 指定的函数接受的参数个数。

看一下这行代码

if ( function_exists( 'hestia_about' ) ) {
	$section_priority = apply_filters( 'hestia_section_priority', 15, 'hestia_about' );
	add_action( 'hestia_sections', 'hestia_about', absint( $section_priority ) );
}

hestia_section_priority 是在哪里定义的呢? 我 grep 了所有代码,依旧没有发现。。。这不科学啊。

https://developer.wordpress.org/reference/functions/apply_filters/

apply_filters 相当于调用 add_filter 注册的函数。

把 15 调整成 150,你就会发现 about section 成功的到了页面最后。

关于作者 🌱

我是来自山东烟台的一名开发者,有敢兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式