WordPress output to XML

For my current project I’m working on a Flash site which is based on a WordPress backend. There are many API’s out there for retrieving and manipulating data (for example FlashPress and SWFPress), but the problem is that most of these libraries are basically an overload for the project I’m working on right now.

I think that there is a strong need for a wordpress structured XML that gives you all the data you need:

  • Navigation Menu (see Appearance>Menu)
  • Pages
  • Posts
  • Bookmarks/Blogroll
  • Categories
  • .. and so on..

Soon I’ll write a plugin which you can install, so you can access your wp data via XML right away. For the timebeing here’s a temporary solution:

  1. Download this file
  2. Extract the file in your theme (wordpress installation/wp-content/themes/themename)
  3. When navigating to your wp site/wp-content/themes/themename/xml.php you get the data you need.

This xml.php file isn’t optimized yet and there’s space for improvement! Modify it for your project needs. Hoops this helps you out ;)

WordPress loop through Menus

Since the release of WordPress 3.0 it’s possible to create custom menus.

Theme developers can enable this menu by adding the following code to ‘functions.php’:

<?php
	add_action( 'after_setup_theme', 'theme_setup' );
 
	function theme_setup() {
		register_nav_menus( array(
			'primary' => __( 'Primary Navigation', 'theme-identifier' ),
		) );
	}
?>

After this the menus will appear in ‘Appearance’ and your ready to create some custom menus the way you like.
Implementing the menu in your theme is as easy as adding this line of code:

<?php wp_nav_menu(); ?>

This will output the menu in an unsorted list (<li>’s). Of course it’s possible to style these menus by providing an arguments array, but I rather retrieve all menus in an ordered array without directly displaying them on the page.

The solution I came up with is really simple (strange it isn’t really documented on codex):

<?php
	$menus = wp_get_nav_menus();
	$menu_items = wp_get_nav_menu_items($menus[0]);
 
	foreach ($menu_items as $menu_item):
		echo "<div>";
		echo " <a href='$menu_item->url'>$menu_item->title</a>";
		echo "</div>";
	endforeach;
?>