首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CodeIgniter查询WordPress博客

CodeIgniter查询WordPress博客
EN

Stack Overflow用户
提问于 2014-02-07 20:00:37
回答 1查看 437关注 0票数 2

我终于开始尝试一些CodeIgniter了,现在我被困住了。

新闻模型

代码语言:javascript
复制
public function get_news($slug = FALSE){
    if($slug === FALSE){
        // if there is no slug specified, pull all records from the 'wp_posts' table
        $this->db->select('post_title, post_content, post_name');
        $this->db->where('post_status', 'published');
        $this->db->where('post_type', 'post');
        $this->db->order_by("post_date", "desc");
        $this->db->order_by("post_title", "asc");
        $query = $this->db->get('wp_posts');
        
        var_dump($query);
        
        // return the resulting array
        return $query->result_array();
    }else{
        // if it does exist, pull only the record(s) specified where column 'slug' = the parameter
        $this->db->select('post_title, post_content, post_name');
        $query = $this->db->get_where('wp_posts', array('post_name' => $slug));
        // return the resulting array
        return $query->row_array();
    }
}

控制器

代码语言:javascript
复制
class News extends CI_Controller{
    
    public function __construct(){
        parent::__construct();
        // located @ /application/models/news_model.php
        $this->load->model('news_model');   
    }
    
    // pull in all news items
    public function index(){
        $data['news'] = $this->news_model->get_news();
        // give the page a title
        $data['title'] = 'Articles Archive';
        $data['c_year'] = date('Y');
        // Load the header template
        $this->load->view('templates/header', $data);
        // Load the page
        $this->load->view('news/index', $data);
        // Load the footer template
        $this->load->view('templates/footer', $data);
    }
    
    // pull in only the news item(s) where the slug matches
    public function view($slug){
        $data['news_item'] = $this->news_model->get_news($slug);
        // give the page a title
        $data['title'] = $data['news_item']['title'];
        $data['c_year'] = date('Y');
        // Load the header template
        $this->load->view('templates/header', $data);
        // Load the page
        $this->load->view('news/view', $data);
        // Load the footer template
        $this->load->view('templates/footer', $data);
    }
        
}

连接到数据库是可以的,但是在我看来,我只返回show_404();,因为有no记录。

直接对数据库运行类似的查询确实会返回我的所有记录。

以下是var_dump的结果

代码语言:javascript
复制
object(CI_DB_mysql_result)#18 (8) { 
   ["conn_id"]=> resource(3) of type (mysql link persistent) 
   ["result_id"]=> resource(4) of type (mysql result) 
   ["result_array"]=> array(0) { } 
   ["result_object"]=> array(0) { } 
   ["custom_result_object"]=> array(0) { } 
   ["current_row"]=> int(0) ["num_rows"]=> int(0)
   ["row_data"]=> NULL 
}

我在这里做错什么了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-07 20:19:40

变化:$this->db->where('post_status', 'published');

致:$this->db->where('post_status', 'publish');

问题解决了..。这一周很漫长..。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21636725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档