douxianglu4370 2014-03-16 08:44
浏览 120

Wordpress使用functions.php中的外部查询结果

I have a custom php page outside of wordpress, this page is connecting also to a external database, and get resultat form a sql query on a variable

i use require('../wp-blog-header.php') and require('../wp-load.php') to integrate with wordpress.

now i need to use this variable on the functions.php of my wordpress theme.

i do on my external page :

<head>
<connect to my database>
<my query>
<get resulte on $variable>
<require('../wp-blog-header.php')>
<require('../wp-load.php')>

but when i use $variable on my functions.php it do not work ?

  • 写回答

2条回答 默认 最新

  • dongqindu8110 2014-03-16 09:05
    关注

    You need to include wp-load.php alone. It will load wp-include (and wpdb), plugins and your theme (functions.php). To use the database directly you would do this:

    global $wpdb;
    $wpdb->get_results('select ...');
    

    If you need to access variable declared in functions.php, it's straight forward:

    say.php

    <?php
    include 'var.php';
    print_r( $moo );
    ?>
    

    var.php

    <?php
    $moo= 'cow';
    ?>
    

    output of say.php

    $ php say.php 
    cow
    

    Loading (include or require) functions.php after wp-load.php will result in redeclaration of the functions defined within functions.php (it will be loaded twice), causing PHP to crash.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测