dongying7847 2014-06-29 18:10
浏览 79
已采纳

自定义主题显示注意未定义的变量:function.php中的wp_query

I have a code in function.php for showing pagination in a custom theme index page but it shows two errors in same line $wp_query

  • Undefined variable: wp_query in function.php

  • Trying to get property of non-object in function.php

my function:

function getpagenavi(){
        ?>
<div id="pagination" class="clearfix">
<?php
 if (function_exists('wp_pagenavi')): ?>
<?php
 wp_pagenavi()  ?>
<?php  else : ?>
        <div class="alignleft">  
        <a href="<?php $max = $wp_query->max_num_pages; previous_posts();?>" class="classname" title="Previous post">Previous post</a></div>
        <div class="alignright"><a href="<?php  next_posts($max);?>" class="classname" title="Next post">Next post</a></div>
        <div class="clear"></div>
<?php
 endif; ?>
</div>
<?php
    }

What can I do to correct this error?

  • 写回答

2条回答 默认 最新

  • duan02143 2014-06-29 19:20
    关注

    You're using $wp_query->max_num_pages when $wp_query is not defined within that function scope.

    Pretending you know what wp_query is supposed to be at the point where that function is called, you can add the following line at the top of the getpagenavi function:

    Global $wp_query;
    

    or better, pass it as a parameter:

    // where you call the function assuming $wp_query exists there.
    getpagenavi($wp_query);
    
    // your function
    function getpagenavi($wp_query){
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题