I am trying to only get the posts that are tagged on a tumblr blog. I can get each post depending on what type of post it is (regular, link, photo, etc) but instead, I want to get each post that has a certain tag on it. For example, only get the posts that are tagged 'file'
Here is my code so far:
<?php
$xml = simplexml_load_file('http://stock开发者_如何学Clamp.tumblr.com/api/read');
$posts = $xml->xpath("/tumblr/posts/post[@type='photo']");
foreach($posts as $post) {
}
?>
<ul>
<li> <a href="<?php echo $post['url-with-slug']; ?>">Post 1(PHP should be here)</a></li>
</ul>
and here is tumblrs API http://www.tumblr.com/docs/en/api/v2
First, you'll need to have your markup within the curly braces that belong to the foreach
loop. Otherwise, the scope of the $post
object you're referencing will not be available, and it give you an error.
This is what I came up with:
<ul>
<?php
$xml = simplexml_load_file('http://stocklamp.tumblr.com/api/read');
$posts = $xml->xpath("/tumblr/posts/post/tag/..");
foreach($posts as $post) {
echo "
<li>
<a href='{$post['url-with-slug']}'>".($post->{'regular-title'}?$post->{'regular-title'}:$post->{'link-text'})."</a>
</li>
";
}
?>
</ul>
And if you want to limit the tags matched, you can do the following:
<ul>
<?php
$xml = simplexml_load_file('http://stocklamp.tumblr.com/api/read');
$posts = $xml->xpath("/tumblr/posts/post/tag[contains(text(),'xml') or contains(text(),'php') or contains(text(),'firefox')]/..");
foreach($posts as $post) {
echo "
<li>
<a href='{$post['url-with-slug']}'>".($post->{'regular-title'}?$post->{'regular-title'}:$post->{'link-text'})."</a>
</li>
";
}
?>
</ul>
Of course, that's a little messy. I'm not necessarily an XPath guru, so there should be a cleaner way than that I would think.
And since I wasn't really digging on the ternary for the link text, or the fact I don't know if one or the other will always be available, this should be more descriptive with a fallback to the url.
<ul>
<?php
$xml = simplexml_load_file('http://stocklamp.tumblr.com/api/read');
$posts = $xml->xpath("/tumblr/posts/post/tag[contains(text(),'xml') or contains(text(),'php') or contains(text(),'firefox')]/..");
foreach($posts as $post) {
if ($post->{'regular-title'}) {
$slug = $post->{'regular-title'};
} else if ($post->{'link-text'}) {
$slug = $post->{'link-text'};
} else {
$slug = $post['url-with-slug'];
}
echo "
<li>
<a href='{$post['url-with-slug']}'>".$slug."</a>
</li>
";
}
?>
</ul>
EDIT
And, you also might want to make a match of the entire text of the TAG
element, so that you don't get matches on things like xml-is-for-noobs
when you want xml
tags only. contains()
will match both, while the below will only match xml
.
/tumblr/posts/post/tag[text() = 'xml']/..
So, according to your comment, if you're looking for posts tagged events
but don't want to catch tags like bowling-events
, prevents fun
, other events
, etc..., you would want to make the text() = 'events'
match. On the other hand, if you DID want to match anything with events
in the text of the TAG
node, use the contains(text(), 'events')
method.
EDIT 2
And if you're trying to limit the results, use position()
:
(/tumblr/posts/post/tag[text() = 'xml']/..)[position() <= 3]
Note, the example on my webpage does not include the text()
match, since there are not enough entries to support a submatch and still be productive. However, that should work.
http://jfcoder.com/test/tumblrtest.php
The XPath selects all TAG
elements that belong to a POST
element, then selects the parent (..
) of that TAG
, which gives you a SimpleXML object containing the relevant POST
elements you're looking for.
Then, it iterates over each element, printing the $post
's information into a UL
element.
精彩评论