Reputation: 43884
First off I have gone through this link http://codex.wordpress.org/Integrating_WordPress_with_Your_Website. I cannot use this.
I am trying to get the latest post from a WordPress installation on a remote server I own. They share the same database server so that is where I am taking the post now and then brining it back into my PHP app. This all works.
What doesn't work is the display. Before I was using nl2br
to make new lines but this does not work right.
I have noticed that WordPress does some post-processing to add p
tags to certain lines they consider should be paragraphs (not in ul
s or li
s for example). They do this after grabbing the post from the DB (the p
tags are not saved to DB).
I have tried to find out what post-processing they use in the source code but I have come up blank after finding the the_content
function etc and where the $post
var comes from but not finding the code I am looking for.
What post-processing function does WordPress use to add these paragraphs to make their posts look ok?
For regex or general PHP people here I am looking to change something like:
<em><a href="link">awesome link</a></em>
<h3>Awesome Head</h3>
lalalaalal
<ul>
<li>Awesome li</li>
</ul>
Into something like:
<p>
<em><a href="link">awesome link</a></em>
</p>
<h3>Awesome Head</h3>
<p>lalalaalal</p>
<ul>
<li>Awesome li</li>
</ul>
Missing out tags that obviously should not have p
tags around them like h
and ul
and li
tags.
Upvotes: 0
Views: 619
Reputation: 43884
The right thing to do would be as @BryanH said: use the RSS feed which already has the post pre-formatted for you.
However if you don't want to make an XML parser and deal with all the stuff that comes with it just to get a blog post then you can use something like:
$content = preg_split('/\\r?\\n/', str_replace(']]>', ']]>', $post->content));
foreach($content as $line){
if(strlen(trim($line)) > 0){
$line = trim($line);
if(!preg_match('/^(<|<\/)(ul|li|div|h[1-6])/', $line)){
echo '<p>'.$line.'</p>';
}else{
echo $line;
}
}
}
This is the code I personally used to solve this problem in the end.
There is probably a more robust and elegant way of doing this (quickly coded) however I tested this on a very complex blog post with images and lots of different tags and it seemed to work nicely without any errors.
I am still unsure what WP uses but this solves my problem in the end.
Upvotes: 0
Reputation: 6062
The easiest, fastest way: parse the latest post from the RSS feed. You'll find all of the <p>
tags automatically added for you.
Upvotes: 2