Twitter Facebook Pinterest Google+ Stumbleupon LinkedIn
Being a WordPress user of many years I’ve found not much can go wrong with the open source CMS but when you have too many plug-ins it can be hard to keep them up to date. Where possible I always try to build a site which wont rely on them the main reasons for this is.
Here is a bit of code I use to build custom HTML Sitemaps. You could even modify it to generate an XML sitemap too with a bit of work.
All you have to do it copy the following code into the functions.php file and it will add a HTML Sitemap short code to your site. Just put [htmlSitemap
] on a page you want it to appear.
/*
Put the following code in your themes functions.php file
*/
function get_html_sitemap( $atts ){
$return = '';
$args = array('public'=>1);
// If you would like to ignore some post types just add them to the array below
$ignoreposttypes = array('attachment');
$post_types = get_post_types( $args, 'objects' );
foreach ( $post_types as $post_type ) {
if( !in_array($post_type->name,$ignoreposttypes)){
$return .= '<h2>' . $post_type->labels->name.'</h2>';
$args = array(
'posts_per_page' => -1,
'post_type' => $post_type->name,
'post_status' => 'publish'
);
$posts_array = get_posts( $args );
$return .= '<ul>';
foreach($posts_array as $pst){
$return .= '<li><a href="'.get_permalink($pst->ID).'">'.$pst->post_title.'</a></li>';
}
$return .= '</ul>';
}
}
return $return;
}
add_shortcode( 'htmlSitemap', 'get_html_sitemap' );
Posted by Adi on January 24, 2016
Twitter Facebook Pinterest Google+ Stumbleupon LinkedIn
This is really helpful, thanks for that. Is there any way to preserve page hierarchy rather than just a flat list with all of the pages?
This is a stunning bit of code. Just trying to figure out now how to ignore certain custom post types I have lol
Thank you
Just add the post types you want to ignore to the array on this line
$ignoreposttypes = array(‘attachment’);
How to limiting the post at 500 early posts only
So useful! Thank you. How can I sort alphabetically?
this is great tutorial thank you
Just what I needed thanks so much!!
Thankksssss so much