getdave / Tanlinell

Boilerplate Wordpress theme for rapid development of new WP themes. Based on the great work of the _s ("Underscore") theme.
GNU General Public License v2.0
6 stars 2 forks source link

Implement Sitemap wihout Plugin #235

Closed getdave closed 10 years ago

getdave commented 10 years ago

Add a standard Sitemap template with the following content

<?php
/*
Template Name: Sitemap
*/

get_header(); ?>
<div class="column-container">
    <div class="main" role="main">
    <?php while ( have_posts() ) : the_post(); ?>

    <?php get_template_part( 'content', 'page' ); ?>
    <?php endwhile; // end of the loop. ?>      

    <h2>Pages</h2>
    <ul>
    <?php wp_list_pages(
        array(
        'exclude' => '',
        'title_li' => '',
        )
    ); ?>
    </ul>

    <?php foreach( get_post_types( array('public' => true) ) as $post_type ) {
      if ( in_array( $post_type, array('post','page','attachment', 'welcome','homepage_slides','slider') ) )
        continue;

      $pt = get_post_type_object( $post_type );

      echo '<h2>'.$pt->labels->name.'</h2>';
      echo '<ul>';

      query_posts('post_type='.$post_type.'&posts_per_page=-1');

      while( have_posts() ) {
        the_post();
        echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
      }

      echo '</ul>';
    }?>

    </div><!-- .main -->

    <div class="sub">
        <?php get_sidebar();?>
    </div>
</div>
<?php get_footer(); ?>

Be sure to add additional CPTs to the exclusion list as required.

Be sure to name the template page-sitemap as this will avoid the need to set a template via the admin.

Alternatively happy for this to become a Plugin wjich automagically creates a Sitemap page (if one doesn't exist) and then adds the sitemap to that page by hooking into the the_content hook and printing out after.

When done please amend Prelaunch to match new requirements.

neilberry001 commented 10 years ago

Revision

<?php
/*
Template Name: Sitemap
*/

get_header(); ?>
<div class="layout-1c">
    <div class="column-container">
        <div class="main" role="main">

            <section class="container">

            <?php while ( have_posts() ) : the_post(); ?>

                <?php get_template_part( 'content', 'page' ); ?>

            <?php endwhile; // end of the loop. ?>      

            <?php foreach( get_post_types( array('public' => true) ) as $post_type ) : ?>

                <?php if ( !in_array( $post_type, array('post','attachment', 'welcome','homepage_slides','slider', 'technologies', 'jobs') ) ) : ?>

                    <?php
                    $args = array(
                        'post_type' => $post_type,
                        'posts_per_page' => '-1',   
                        'orderby' => 'title',
                        'order' => 'ASC'
                    );
                    $custom_posts_obj = new WP_Query( $args );
                    ?>

                    <?php if ( $custom_posts_obj->have_posts() ): ?>
                    <h2><?php echo get_post_type_object( $post_type )->labels->name; ?></h2>

                    <ul>

                        <?php while ( $custom_posts_obj->have_posts() ) : $custom_posts_obj->the_post(); ?>

                        <li><a href="<?php echo get_permalink(); ?>"><?php echo get_the_title(); ?></a></li>

                        <?php endwhile; ?>

                    </ul>

                    <?php endif; wp_reset_postdata(); ?>

                <?php endif; ?>

            <?php endforeach; ?>

            </section>

        </div><!-- .main -->

        <div class="sub">
            <?php get_sidebar();?>
        </div>

    </div>
</div>
<?php get_footer(); ?>
getdave commented 10 years ago

Looks ok to me.

neilberry001 commented 10 years ago

Closed via 947f17b039ebb2c87189031d04a71a1beedcdcc5