dtnmuyoiw680512744 2015-05-29 13:17
浏览 42

Wordpress - 将类方法传递给query_var挂钩

I have the following code in WordPress so as to try and achive a stage where I can use esc_url(get_permalink() . '?month=' . get_query_var('month'));.

In an attempt to learn more about WordPress and to make a class that would allow me to add more values as and when I need them, I created the following class and linked it into the query_vars filter:

 //Create the needed GET vars //

 $custom_query_values = array('month','day');

 new _custom_query_vars($custom_query_values);

 class _custom_query_vars
 {
     public $_custom_vars;
     function __construct($custom_vars){
        $this->_custom_vars = $custom_vars;
        add_filter('query_vars',array(&$this, '_add_custom_querys'));
    }

    public function _add_custom_querys(){
    // Return an array of values //
        foreach($this->_custom_vars as $value)
        {
            $vars[] = $value;
        } 
        print_r($vars);
        return $vars;
     }
 } 

 /*function add_custom_query_var( $vars ){
   $vars[] = "month";
   $vars[] = "day";
   return $vars;
 }
 add_filter( 'query_vars', 'add_custom_query_var' ); */

The above code does not work, instead, when the class is active and when I create a new instance all pages on my website will stop working and I will simple be directed to my root address. However, the function "seems" to be working as the print_r() will indeed print the values of Array ( [0] => month [1] => day ) so the method must be getting passed to the query_var hook in some shape or form.

The second part of the code that is commented out is me trying the standard function that simply returns static values. This works and using the normal esc_url(get_permalink() . '?month=' . get_query_var('month')); works as expected. Any ideas? (one last thing, is there a way of making this http://www.sitename/pagename/month).

Thank you for any and all help,

  • 写回答

1条回答 默认 最新

  • duanlu0075 2015-06-01 06:02
    关注

    I am Not Sure But Try This.

    public function _add_custom_querys(){
             $vars =  Array();
    
             foreach($this->_custom_vars as $value){
                 array_push($vars,$value);
             }
              return $vars;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏