WordPress 如何在 Child Theme 重写 Parent 里的 shortcode

很多时候客户选择去购买商业主题来建站,很多商业主题都做得很不错,功能也很完善,但是肯定不是100%客户想要的,有些功能还是要修改的。

对于这种商业主题,作者会不定期更新,所以修改原主题就不是一个很好的解决办法,一般做法是新建一个 child theme 来重写一些功能。

那问题就来了,如何重写 shortcode 呢?方法如下

function itc_shortcodes() {
    remove_shortcode( 'portfolio_slider' );
    add_shortcode( 'portfolio_slider', 'itc_portfolio_slider' );
}

function itc_portfolio_slider(){
    return "YAO";
}
add_action( 'init', 'itc_shortcodes' );

注:对于应该使用哪一个 action,要去夫主题查看一下,如果夫主题是用的 action wp, 那么最后一行就应该替换为

add_action( 'wp', 'itc_shortcodes' );

这样你只需要将原来的代码负责过来放到新的 shortcode 的函数里,修改即可。

Comments are closed.