Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have created pages for my website, however when i create a new post, it is not assigned to any page and i am missing the option to place it under a certain page. For example i have a menu like

Home Football Basketball

if a user clicks on home, i would like them to see the page that i have published about home, which is static, however, when they click on basketball, they must see all relevant posts that have been made on besketball likewise for football.

share|improve this question
2  
Are football and basketball categories, taxonomies or custom post types? –  Pieter Goosen Apr 24 at 17:53
    
@PieterGoosen want to make them categories, but have created them as pages, it is possible to transform a page into a category –  sosytee Apr 24 at 18:03
    
You will need to use Custom Page Templates, that have custom/secondary queries, that show posts from a given category. –  Chip Bennett Apr 24 at 18:11
    
You can change the pages to posts and add them to any category using a plugin like Post Type Switcher. wordpress.stackexchange.com/a/127748/9884 –  Brad Dalton Apr 24 at 18:36

1 Answer 1

I've been using this template from twenty eleven. I must give great credit here to a great tutorial from digitalraindrops. I have rewritten this to suite my needs and it is currently written to suite the twentyfourteen theme.

You can check out the tutorial at the link given above. What I like about this template is that it can be reused a thousand times. What this template do, it let you choose a category from a drop down list. This category's posts will then be displayed on that page. Here is the code that I use

First, all the meta functions that include options for posts per page, order, page title, post title, etc. This code will go in your functions.php or custom functions file

<?php
/**-----------------------------------------------------------------------------
 *
 *  Add a post metabox with options to the admin page screen.
 *  After selcting the page-pop.php template as a page template,
 *  this metabox will appear in the admin page screen.
 *  From here you can choose which category's posts to display
 *  and how the posts will be displayed on the page
 *
 *  @package WordPress
 *  @subpackage Twenty_Fourteen
 *  @since Twenty Fourteen 1.0
 *
*------------------------------------------------------------------------------*/

/**-----------------------------------------------------------------------------
 *
 *  1. Only add meta boxes for the pop template
 *
 *  @since Twenty Fourteen 1.0
 *
*------------------------------------------------------------------------------*/
add_action('admin_init', 'pietergoosen_add_pop_meta_box');

function pietergoosen_add_pop_meta_box(){   
    $post_id = isset( $_GET['post'] ) ? $_GET['post'] : 0 ;
    if($post_id) { 
        $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
        if ($template_file == 'page-pop.php') { 
            add_meta_box('pop_meta_box', __( 'Page of Posts with the same name', 'pietergoosen' ), 'pietergoosen_pop_meta_options', 'page', 'side', 'core');
        } else {
            $meta = get_post_meta($post_id, '_cat_id', true);
            if( $meta ) {
                pietergoosen_pop_update_post_meta($post_id, '_cat_id', '');
                pietergoosen_pop_update_post_meta($post_id, '_page_title', '');
                pietergoosen_pop_update_post_meta($post_id, '_posts_title', '');
                pietergoosen_pop_update_post_meta($post_id, '_order_by', '');
                pietergoosen_pop_update_post_meta($post_id, '_asc', '');
                pietergoosen_pop_update_post_meta($post_id, '_post_count', '');
                pietergoosen_pop_update_post_meta($post_id, '_days', '');
                remove_meta_box( 'pop_meta_box', 'page', 'side' );
            }
        }
    }
    add_action('save_post', 'pietergoosen_pop_update_post_meta_box');
}

/**-----------------------------------------------------------------------------
 *
 *  2. Built the list to display the options in the metabox
 *
 *  @since Twenty Fourteen 1.0
 *
*------------------------------------------------------------------------------*/
$order_list = array(
    'none' => array( 'value' => 'none','label' => 'None' ),
    'id' => array( 'value' => 'ID','label' => 'Post ID' ),
    'author' => array( 'value' => 'author','label' => 'Author' ),
    'title' => array( 'value' => 'title','label' => 'Post Title' ),
    'date' => array( 'value' => 'date', 'label' => 'Post Date'  ),
    'modified' => array( 'value' => 'modified','label' => 'Modified Date' ),
    'parent' => array( 'value' => 'parent','label' => 'Parent Post' ),
    'rand' => array( 'value' => 'rand','label' => 'Random' ),
    'comment_count' => array( 'value' => 'comment_count','label' => 'Comment Count' ),
    'menu_order' => array( 'value' => 'menu_order','label' => 'Menu Order' ),
);

$sort = array(
    'DESC' => array( 'value' => 'DESC','label' => 'Descending' ),
    'ASC' => array( 'value' => 'ASC','label' => 'Ascending' ),
); 

function pietergoosen_pop_meta_options(){

    $post_id =  !empty($_GET['post']) ? $_GET['post'] : 0;
    if( !$post_id ) return;

    $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
    if ($template_file != 'page-pop.php') return;

    global $order_list,$post_styles,$sort;
    $categories = get_categories();

    //Check if we have values
    $post_meta=array();
    $post_meta = get_post_meta( $post_id,false );

    $cat_id = isset( $post_meta['_cat_id'] ) ? $post_meta['_cat_id'][0] : 1;
    $page_title = isset( $post_meta['_page_title'] ) && $post_meta['_page_title'] ? $post_meta['_page_title'][0] : '';
    $posts_title = isset( $post_meta['_posts_title'] ) && $post_meta['_posts_title'] ? $post_meta['_posts_title'][0] : '';
    $order_by = isset( $post_meta['_order_by'] ) ? $post_meta['_order_by'][0] : 'date';
    $asc = isset( $post_meta['_asc'] ) ? $post_meta['_asc'][0] : 'DESC';
    $post_count = isset( $post_meta['_post_count'] ) ? $post_meta['_post_count'][0] : get_option('posts_per_page');
    if(!$post_count || !is_numeric( $post_count )) $post_count = get_option('posts_per_page');
    $days = isset( $post_meta['_days'] ) ? $post_meta['_days'][0] : '0';
    if($days && !is_numeric( $days )) $days = '0';
    ?>

    <!-- Sart the meta boxes -->
    <div class="inside">
    <p><label><strong><?php _e( 'Page Title', 'pietergoosen' ); ?></strong></label></p> 
    <input id="_posts_title" name="_posts_title" type="text" style="width: 98%;" value="<?php echo $posts_title; ?>"/>  

    <p><label><strong><?php _e( 'Post Title', 'pietergoosen' ); ?></strong></label></p> 
    <input id="_page_title" name="_page_title" type="text" style="width: 98%;" value="<?php echo $page_title; ?>"/>

    <p><label><strong><?php _e( 'Category', 'pietergoosen' ); ?></strong></label></p>
    <select id="_cat_id" name="_cat_id">
<?php 
    //Category List
    foreach ($categories as $cat) :
        $selected = ( $cat->cat_ID == $cat_id ) ? ' selected = "selected" ' : '';
        $option = '<option '.$selected .'value="' . $cat->cat_ID;
        $option = $option .'">';
        $option = $option .$cat->cat_name;
        $option = $option .'</option>';
        echo $option;
    endforeach;
?>
    </select>

    <p><label><strong><?php _e( 'Sort by', 'pietergoosen' )?></strong></label></p>
    <select id="_order_by" name="_order_by">
<?php 
    foreach ($order_list as $output) :
        $selected = ( $output['value'] == $order_by ) ? ' selected = "selected" ' : '';
        $option = '<option '.$selected .'value="' . $output['value'];
        $option = $option .'">';
        $option = $option .$output['label'];
        $option = $option .'</option>';
        echo $option;
    endforeach;
?>
    </select>       

    <p><label><strong><?php _e( 'Order', 'pietergoosen' )?><strong></label></p>
    <select id="_asc" name="_asc">
<?php 
    foreach ($sort as $output) :
        $selected = ( $output['value'] == $asc ) ? ' selected = "selected" ' : '';
        $option = '<option '.$selected .'value="' . $output['value'];
        $option = $option .'">';
        $option = $option .$output['label'];
        $option = $option .'</option>';
        echo $option;
    endforeach;
?>
    </select>

    <p><strong><label><?php _e( 'Posts per Page', 'pageofposts' ); ?><strong></label></p>
    <input id="_post_count" name="_post_count" type="text" value="<?php echo $post_count; ?>" size="3" />

    <p><strong><label><?php _e( 'Posts in the last days', 'pageofposts' ); ?><strong></label></p>
    <input id="_days" name="_days" type="text" value="<?php echo $days; ?>" size="3" />
    </div>
    <!-- End page of posts meta box -->
    <?php
}
function pietergoosen_pop_update_post_meta_box( $post_id ){

    if ( empty( $_POST ) ) {
        return;
    } else {
        $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
        if ($template_file != 'page-pop.php') return;

        if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
            return $post_id;
        } else {
            if ( $_POST['post_type'] == 'page' ) {
                if ( !current_user_can( 'edit_page', $post_id ) )
                  return $post_id;
            } else {
                if ( !current_user_can( 'edit_post', $post_id ) )
                  return $post_id;
            }
            $meta = isset( $_POST['_cat_id'] ) ? $_POST['_cat_id'] : 1;         
            pietergoosen_pop_update_post_meta($post_id, '_cat_id', $meta);
            $meta = isset( $_POST['_page_title'] ) ? $_POST['_page_title'] : '';            
            pietergoosen_pop_update_post_meta($post_id, '_page_title', $meta);
            $meta = isset( $_POST['_posts_title'] ) ? $_POST['_posts_title'] : '';          
            pietergoosen_pop_update_post_meta($post_id, '_posts_title', $meta);
            $meta = isset( $_POST['_order_by'] ) ? $_POST['_order_by'] : 'date';            
            pietergoosen_pop_update_post_meta($post_id, '_order_by', $meta);
            $meta = isset( $_POST['_asc'] ) ? $_POST['_asc'] : 'DESC';
            pietergoosen_pop_update_post_meta($post_id, '_asc', $meta);
            $meta = isset( $_POST['_post_count'] ) ? $_POST['_post_count'] : get_option('posts_per_page');
            pietergoosen_pop_update_post_meta($post_id, '_post_count', $meta);
            $meta = isset( $_POST['_days'] ) ? $_POST['_days'] : 0;
            pietergoosen_pop_update_post_meta($post_id, '_days', $meta);
            return;
        }
    }
}

function pietergoosen_pop_update_post_meta($post_id, $key, $data) {
    $post_meta = get_post_meta($post_id, $key, true);
    if( $data != '' && $post_meta != $data) {
        update_post_meta($post_id, $key, $data);
    } elseif ( $post_meta != '' && $data == '' ) {
        delete_post_meta($post_id, $key);
    }
}
?>

Secondly, the page template. You have to call this template page-pop.php

<?php
/**
 * Template Name: Page of Posts
 */
get_header(); ?>

<?php
//See if we have any values
$post_meta=array();
$post_meta = get_post_meta( $post->ID,false );
$catid = isset( $post_meta['_cat_id'] ) ? $post_meta['_cat_id'][0] : 1;
$page_title = isset( $post_meta['_page_title'] ) ? $post_meta['_page_title'][0] : '';
$posts_title = isset( $post_meta['_posts_title'] ) ? $post_meta['_posts_title'][0] : '';
$orderby = isset( $post_meta['_order_by'] ) ? $post_meta['_order_by'][0] : 'date';
$asc = isset( $post_meta['_asc'] ) ? $post_meta['_asc'][0] : 'DESC';
$list_style =  isset( $post_meta['_list_style'] ) ? $post_meta['_list_style'][0] : 'default';   
$post_count = isset( $post_meta['_post_count'] ) ? $post_meta['_post_count'][0] : get_option('posts_per_page');
if(!$post_count || !is_numeric( $post_count )) $post_count = get_option('posts_per_page');
$days = isset( $post_meta['_days'] ) ? $post_meta['_days'][0] : 0;
if($days && !is_numeric( $days )) $days = 0;
$do_not_show_stickies = ($list_style == 'default') ? 0 : 1;
?>  
<div id="main-content" class="main-content">

    <div id="primary" class="content-area">
        <div id="content" class="site-content" role="main">

    <!-- Page Title -->
    <?php if( $posts_title ) : ?>
        <article id="posts-title">
            <header class="entry-header">
                <h2 class="entry-title"><?php echo $posts_title; ?></h2>
            </header><!-- .entry-header -->
        </article><!-- #posts-title -->
    <?php endif; ?>

        <?php the_post(); ?>
        <?php global $post;
        if( $post->post_content || $page_title ) : ?>
        <div class="entry-content">
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <?php if( $page_title ) : ?>
                    <header class="entry-header">
                        <h1 class="entry-title"><?php echo $page_title; ?></h1>
                    </header><!-- .entry-header -->

                <?php endif; ?>
            <?php if( $post->post_content ) : ?>    
                <div class="entry-content">
                    <?php the_content(); ?>
                    <?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'pietergoosen' ) . '</span>', 'after' => '</div>' ) ); ?>
                </div><!-- .entry-content -->
                <footer class="entry-meta">

                </footer><!-- .entry-meta -->
            <?php endif; ?>
            </article><!-- #post-<?php the_ID(); ?> -->
        </div>  
        <?php endif; ?>

            <?php 
            /* Do we have any category */
                global $post;

                // Save posts for later use
                $tmp_post = $post;

                $args = array( 
                    'cat' => $catid,
                    'posts_per_page' => $post_count,
                    'paged' => $paged,
                    'orderby' => $orderby,
                    'order' => $asc,
                    'ignore_sticky_posts' => $do_not_show_stickies,
                );

                if( $days ) {
                    function pop_filter_where( $where = '') {
                        global $days;
                        $where .= " AND post_date > '" . date('Y-m-d', strtotime('-' .$days .' days')) . "'";
                        return $where;
                    }
                    add_filter( 'posts_where', 'pop_filter_where' );
                    $wp_query= null;
                    $wp_query = new WP_Query();
                    $wp_query->query( $args );
                    remove_filter( 'posts_where', 'pop_filter_where' );
                } else {
                    $wp_query= null;
                    $wp_query = new WP_Query();
                    $wp_query->query( $args );
                }           
                // Output
        if ( $wp_query->have_posts() ) :

                // Start the Loop.
                while ( $wp_query->have_posts() ) : $wp_query->the_post();

             get_template_part( 'content', get_post_format() ); 

            endwhile;

                pietergoosen_pagination();  

                else : 

                    get_template_part( 'content', 'none' );

                endif; ?>

                <?php 
                    // Reset the post to the page post
                    $post = $tmp_post; 
                ?>

            <?php if ( comments_open() || get_comments_number() ) {
                        comments_template();
                    } ?>

    </div><!-- #content -->
    </div><!-- #primary -->

    <?php get_sidebar( 'content' ); ?>

</div><!-- #main-content -->

<?php
get_footer();

You can now simply create a new page and choose the "page of posts" template, and publish your page. Once that is done, the Page of Posts metabox will appear. From that you can choose the category to display on that page. Hope this helps you

share|improve this answer
    
You have not left any feedback yet. Was my answers useful in solving your issue. If so, mark my answer as accepted. On the other hand, if it did not help, and you solved your issue yourself, please add your solution and accept it. Help us to keep this question constructive, and help us to get this question status as solved. :-) –  Pieter Goosen Oct 12 at 18:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.