Search code examples
phpwordpresstemplatescustom-pages

Wordpress Page of Posts


I am building a personal website to host my university work, personal projects and photos etc.

The menu is a hierarchical structure made up of pages and links. Take my university pages for example. What I would like to achieve is to display posts that are related to the module code which is the page's slug.

I've used the following link http://codex.wordpress.org/Page_Templates#A_Page_of_Posts and managed to get it working but I have hard coded the module code into the template, meaning for each module I will have to have a separate template and the only thing that will be different from one file to the next is 5 characters which isn't great for code re-use.

What I am asking, is, is there a way to get the slug from the page I'm looking at and use that for the WP_Query arguments.

If you go to http://michaelnorris.co.uk/ and look at the menu structure. Navigate to University -> Year Three -> Individual Project, you will notice the url is http://michaelnorris.co.uk/uni/three/ci301 where ci301 is the module code for the Individual Project. I want to have this system on each of the module pages so that I can tag posts and they are displayed in the relevant module.


Solution

  • Ok, I actually found the answer myself, but for others looking to do the same. Below is a solution.

    Solution found here on the Wordpress.org Codex http://codex.wordpress.org/Page_Templates#A_Page_of_Posts

    Name the file pageofposts.php and edit the Page within the Wordpress Dashboard and set the Template (in the dropdown) to 'Page of Posts'. Bingo!

    <?php
    /*
    Template Name: Page Of Posts
    */
    
    /* This example is for a child theme of Twenty Thirteen: 
    *  You'll need to adapt it the HTML structure of your own theme.
    */
    
    get_header(); ?>
    
        <div id="primary" class="content-area">
            <div id="content" class="site-content" role="main">
            <?php 
            /* The loop: the_post retrieves the content
             * of the new Page you created to list the posts,
             * e.g., an intro describing the posts shown listed on this Page..
             */
            global $post;
            $slug = get_post( $post )->post_name;
    
            if ( have_posts() ) :
                while ( have_posts() ) : the_post();
    
                  // Display content of page
                  get_template_part( 'content', get_post_format() ); 
                  wp_reset_postdata();
    
                endwhile;
            endif;
    
            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
            $args = array(
                // Change these category SLUGS to suit your use. category_name is comma separated.
                'tag' => $slug, 
                'paged' => $paged
            );
    
            $list_of_posts = new WP_Query( $args );
            ?>
            <?php if ( $list_of_posts->have_posts() ) : ?>
                <?php /* The loop */ ?>
                <?php while ( $list_of_posts->have_posts() ) : $list_of_posts->the_post(); ?>
                    <?php // Display content of posts ?>
                    <?php get_template_part( 'content', get_post_format() ); ?>
                <?php endwhile; ?>
    
                <?php twentythirteen_paging_nav(); ?>
    
            <?php else : ?>
                <?php get_template_part( 'content', 'none' ); ?>
            <?php endif; ?>
    
            </div><!-- #content -->
        </div><!-- #primary -->
    
    <?php get_footer(); ?>