Wordpress queries and wikipedia
- Use whatever method you know (or will learn) to get the information from WordPress (ask WordPress support)
- Use MediaWiki's API on http://en.wikipedia.org/w/api.php to get information about it. Or if you want to just get the article, fetch URLs like: http://en.wikipedia.org/w/index.php?title=Email&action=render (render = just article without header/footer/sidebar etc.).
- Then in PHP (getWikiIntroParagraph) extract the first one (or two)
<p>-tags. And with what you have left, use strip_tags() to get rid of other unwanted html code, and then you have an clear-text excerpt.:
Electronic mail, commonly called email or e-mail, is a method of exchanging digital messages from an author to one or more recipients. Modern email operates across the Internet or other computer networks.
Thanks, Krinkle.
If I have more than one tag in wordpress how can I solve this problem that not all of them should be included in an URL like this: http://en.wikipedia.org/w/index.php?title=Email&action=render
And instead of "Email" how do I need to include $tag for example? Like this? http://en.wikipedia.org/w/index.php?title=.$tag.&action=render
Thanks a lot!
Well, assuming you have an array of tags that you fetched from WordPress (get_the_tags()), something like this:
function getWikiIntroParagraph( $title ) { $url = 'http://en.wikipedia.org/w/index.php?' . http_build_query(array( 'title' => $title, 'action' => 'raw' )); // Download $url and do your magic extraction return $intro; } $posttags = get_the_tags(); if ($posttags) { echo '<ul>'; foreach($posttags as $tag) { echo '<li><strong>' . htmlspecialchars( $tag->name ) . '</strong>: ' . getWikiIntroParagraph( $tag->name ); } echo '</ul>'; }
I have this right now
<?php $posttags = get_the_tags(); if ($posttags) { foreach($posttags as $tag) { echo $tag->name . ' '; } } function getWikiIntroParagraph( $title ) { $url = 'http://en.wikipedia.org/w/index.php?' . build_http_query(array( 'title' => $title, 'action' => 'raw' )); // Download $url and do your magic extraction return $intro; } $posttags = get_the_tags(); if ($posttags) { echo '<ul>'; foreach($posttags as $tag) { echo '<li><strong>' . htmlspecialchars( $tag ) . '</strong>: ' . getWikiIntroParagraph( $tag->name ); } echo '</ul>'; } ?>
But it says htmlspecialchars() expects parameter 1 to be string, object given on line 41 in single.php and Fatal error: Call to undefined function build_http_query() on 32, same file. Guess it's wrong what I did, isn't it?
@Krinkle Can you give me details on the error for build http query? Solved the htmlspecialchars error wth some help but don't know much about PHP or anything else in programming to solve the build http query problem. Best!
I mistyped build_http_query for http_build_query. And $tag should be $tag-name, just like the others. See my updated example
Thanks. Returns now just
- Tag1:
- Tag2:
function getWikiIntroParagraph( $title ) { $url = 'http://en.wikipedia.org/w/index.php?' . http_build_query(array( 'title' => $title, 'action' => 'raw' )) . $tag . '&action=render'; // Download $url and do your magic extraction return $intro; } $posttags = get_the_tags(); if ($posttags) { echo '<ul>'; foreach($posttags as $tag) { echo '<li><strong>' . htmlspecialchars( $tag->name ) . '</strong>: ' . getWikiIntroParagraph( $tag->name ); } echo '</ul>'; }
Didn't get any other result, just a list of the tag names without an excerpt. If I put $url instead of $url it shows the the url in plain text. Guess I need to define the $intro, right? Hav no idea how to do so.
I'm not going to write it for you. In the previous comments I explained how $intro would be defined (obviously it's not defined right now, that's up to you) where it reads " // Download $url and do your magic extraction".
Then in PHP (getWikiIntroParagraph) extract the first one (or two)
<p>-tags. And with what you have left, use strip_tags() to get rid of other unwanted html code, and then you have an clear-text excerpt.
I know, but I do need further explanation or better the parts I need to use for what is left to do. I am not into php so need to be pointed to each function or whatever I need to have, you know? I just know it is not defined but cannot think about a function or whatever to get what I want. Is $intro the only thing what's left? If so, what do I need to make run? If not, what else and what do I need? As I said I am not into php and need more info. Sorry.
//EDIT
I guess it's only $intro I need to define right now. Do I have to make it within function get WikiIntroParagraph or before / after that? As far as I understand this function extracts anything from the page. Do I have to use truncate to get a summary or is there something else I can use?
I have tried something but doesn't function. Do have any idea(s)? http://pastebin.com/fkLBeViY