How to create dynamic page in WordPress

I’ve searched for such how-to, but I didn’t find something exactly like this, so I decided to try by myself and I did it 🙂 The goal was to make page (page as meaning of wordpress), which displays dynamic content everytime it’s loaded. Dynamic content is simply RSS feed. To parse content I needed PHP and start to find way to make WordPress to manage data from database as code. First it’s sound like impossible thing, but of course, there are not impossible things 🙂 The solution, that I found, is Exec-PHP plugin for WordPress. It does exactly this – allows to insert PHP code in wordpress post or page.

Second hard thing was how to write PHP code in WordPress editor, because everytime when I pressed Save (or Publish) WordPress messed up everything. I tried tips on Exec-PHP site, but I didn’t success. The final solution was to edit directly database. So I created a blank page with editor, and than inserted content (with PHP code) with PhpMyAdmin 🙂 Now everything looks like almost as I expected.

Now I have two dynamic pages – my recent tracks from last.fm and my recent visited events (from scenata.com). If you want some similar functionality for your blog just do these three steps:

  1. download and install Exec-PHP plug in
  2. create a page (or post) with WodPress editor
  3. Edit page (or post) with PhpMyAdmin (or whatever you want) and replace field post_content with something like this (for last.fm recent tracks):

<h3>ik0’s Recently Played Tracks</h3>
<h4>
<?php
include_once(ABSPATH . WPINC . ‘/rss.php’);
$rss = fetch_rss(‘http://ws.audioscrobbler.com/1.0/user/ik0/recenttracks.rss’);
$maxitems = 10;
$items = array_slice($rss->items, 0, $maxitems);
?>

<?php if (empty($items)) echo ‘No items’;
else
foreach ( $items as $item ) : ?>
<a href=”<?php echo $item[‘link’]; ?>”>
<?php echo $item[‘title’]; ?>
</a>
<?php endforeach; ?>
</h4>

That’s it 🙂 If you tell me some easier way, I’ll be glad.

p.s. Yes, it’s ugly, but it can be done more beautiful. By now it’s just works !

2 thoughts on “How to create dynamic page in WordPress

Коментирай