2013-09-24 45 views
0

我正在製作一個網站,其中的Feed由tumblr運行。有一部分是針對一般帖子的部分,另一部分是針對「精選」帖子的部分,由標籤指定(即#featured)。Tumblr API - 通過標籤排除結果

我試圖阻止顯示在同一頁面上的兩個不同的點了同樣的職位,所以我一般進料段,有沒有辦法把它排除職位與#featured

回答

0

當你得到這個職位,對象處理可以隨時查詢

if(!in_array($tag_to_exclude, $post->tags)) { 
    // Post does not contain tag - display ... 
} 

我猜你搶$apidata->response->posts和運行foreach循環?否則,請隨時索取更多信息

+0

是的,我想這會工作,想知道是否有一個API調用,但結果將是相同的。謝謝! – wkd

+0

對不起,我不認爲他們對遺漏標籤查詢感到困擾 - >您可以通過構建標記帖子數組並將其餘的數據發送到另一個數組作爲常規feed來使用它來排序來自api的帖子。如果你想挑選更多的標籤,你可以使用這種方法來構建更多的包含省略標籤的數組 - 我認爲這是最好的解決方案:) – GroovyCarrot

0

我一直在努力完成同樣的事情,並沒有與Tumblr API的運氣。看起來很奇怪,他們沒有這個功能,但我想那就是這樣。我確實編寫了一個PHP類,它可以完成這個任務,這可能對OP或其他任何想做同樣事情的人有幫助。

class Tumblr { 
    private $api_key = 'your_tumblr_api_key'; 
    private $api_version = 2; 
    private $api_uri = 'api.tumblr.com'; 
    private $blog_name = 'your_tumblr_blog_name'; 

    private $excluded = 0; 
    private $request_total = 0; 

    public function get_posts($count = 40, $offset = 0) { 
     return json_decode(file_get_contents($this->get_base_url() . 'posts?limit=' . $count . '&offset=' . $offset . $this->get_api_key()), TRUE)['response']['posts']; 
    } 

    /* 
    * Recursive function that can make multiple requests to retrieve 
    * the $count number of posts that do not have a tag equal to $tag. 
    */ 
    public function get_posts_without_tag($tag, $count, $offset) { 
     $excluded = 0; 

     // get the the set of posts, hoping they won't have the tag 
     $posts = $this->get_posts($count, $offset); 
     foreach ($posts as $key => $post) { 
      if (in_array($tag, $post['tags'])) { 
       unset($posts[$key]); 
       $excluded++; 
      } 
     } 
     // if the full $count hasn't been retrieved, call this function recursively 
     if ($excluded > 0) { 
      $posts = array_merge($posts, $this->get_posts_without_tag($tag, $excluded, $offset + $count)); 
     } 

     return $posts; 
    } 

    private function get_base_url() { 
     return 'http://' . $this->api_uri . '/v' . $this->api_version . '/blog/' . $this->blog_name . '.tumblr.com/'; 
    } 

    private function get_api_key() { 
     return '&api_key=' . $this->api_key; 
    } 
} 

get_posts_without_tag()函數是發生大部分操作的地方。它不幸地通過提出多個請求來解決問題。請務必用您的API密鑰和博客名稱替換$api_key$blog_name