dongwen7187 2018-08-13 06:51
浏览 18
已采纳

wordpress短代码仅适用于一页

I wrote a shortcode that is used for displaying data and it is used just in one page (ie: PAGEX using full-width-page-template.php). The shortcode function is very huge.

function rj_mysh_shortcode() {
   // Lots of lines of business logic
}
add_shortcode('rj_mysh', 'rj_mysh_shortcode');

Every time a webpage is called I think functions.php is called and parsed by PHP. I'm looking for a solution in order to avoid to spend time in parsing something not useful.

I thought that I can create a template TEMPLATE_PAGEX and move the function there.

TEMPLATE_PAGEX.php:
<?php
    function rj_mysh_shortcode_template() {
       // Lots of lines of business logic
    }

include(locate_template('full-width-page-template.php'));
?> 

At the end the shortcode is:

function rj_mysh_shortcode() {
   return rj_mysh_shortcode_template()
}
add_shortcode('rj_mysh', 'rj_mysh_shortcode');

Is this the right solution?

Rr

  • 写回答

1条回答 默认 最新

  • douchuxun4162 2018-08-13 06:57
    关注

    You could always check what page you are on: developer.wordpress.org/reference/functions/is_page ... is_page($pageID) ... and only run the code if on that page.

    function rj_mysh_shortcode() {
        if(is_page($pageID)) {
          // Lots of lines of business logic
        }
    }
    add_shortcode('rj_mysh', 'rj_mysh_shortcode');
    

    or just remove the need for a shortcode, and just run the logic/code etc. directly from functions.php but doing the same page check so it only gets ran on that specific page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部