tumblr からフィードが流れてくるサイトを作っています。一般的な投稿用の 1 つのセクションと、タグ (つまり ) で指定される「おすすめ」の投稿用の別のセクションがあります#featured
。
同じ投稿が同じページの 2 つの異なる場所に表示されるのを防ごうとしているのですが、一般的なフィード セクションで、 を含む投稿を除外する#featured
方法はありますか?
処理する投稿オブジェクトを取得すると、いつでも確認できます
if(!in_array($tag_to_exclude, $post->tags)) {
// Post does not contain tag - display ...
}
をつかんでループ$apidata->response->posts
を実行すると思いますか?foreach
それ以外の場合は、お気軽に詳細をお尋ねください
私は同じことを達成しようとしてきましたが、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_key
and$blog_name
を API キーとブログ名に置き換えてください。