Create Sitemap Using WordPress Shortcode Functionality | Tutorial Freak – Online Tutorials

Create Sitemap Using WordPress Shortcode Functionality

Do you know that the functions.php file in your WordPress website can be used to achieve a lot of things you never knew you could? This file can be used to extend the functionality of your theme and site. Let us learn one such hack of functions.php file.

The shortcode when added to any post or page will return a clean and formatted list of all the pages on your website.

You will first need to add the below PHP code in your functions.php file:

function GenerateSitemap($params = array()) {
    // default parameters
    extract(shortcode_atts(array(
        'title' => 'Site map',
        'id' => 'sitemap',
        'depth' => 2
    ), $params));
    // create sitemap
    $sitemap = wp_list_pages("title_li=&depth=$depth&sort_column=menu_order&echo=0");
    if ($sitemap != '') {
        $sitemap =
            ($title == '' ? '' : "<h2>$title</h2>") .
            '<ul>$sitemap</ul>";
    }
    return $sitemap;
}
add_shortcode('sitemap', 'GenerateSitemap');

After adding this code to your functions.php file you just need to enter [sitemap] in the post or page you want the list to appear.

That is not all!! You can generate list of all posts too, just modify the code a bit and there you go – you’ll have the list of all pages. You can also play with the CSS and title to customize it further.

Author, Blogger, SEO Expert, Working Professional and a Student! I play so many roles in life and love what i do. I enjoy each moment of my life. Do follow us on Facebook

Sagar – who has written posts on Tutorial Freak – Online Tutorials.


Help keep us alive & kicking, we need coffee 24/7, please donate, thanks!
Embed

Leave a Reply

      
      
      
      
      
      
      
      
             
Save Now! $1.99 .COM at GoDaddy!
             
      
  • Twitter blocked in Turkey!