dongwen7187 2018-08-13 14: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 14: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.

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

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探