0

How can I associate posts with pages?

Trying to determine the best architecture / strategy to achieve the following...

Using WordPress as a CMS
8 Pages each with sub pages
Each page can also have posts

I can set up 8 categories where each category matches a page and then list the posts in that category under the page but that seems clunky. Is there a better way?

Example:
Awards Page
has sub page Annual Awards and
has sub page Monthly Awards
has post March 2012 Awards which should be displayed on Awards Page & Monthly Awards Page

Thanks in advance.

1
  • Posts, by default, are not hierarchical so yes, it is clunky.
    – AlxVallejo
    Commented Mar 14, 2012 at 18:31

2 Answers 2

1

There is an awesome plugin by @scribu called Posts 2 Posts. It allows you to manually associate different post types (or the same post type) with each other. I use it quite a bit for bigger CMS type projects and even pluginized a common pattern I found myself writing on a few sites.

Here's an example to do what you need with Posts 2 Posts

<?php
WPSE45561_Pages_Posts::init();

class WPSE45561_Pages_Posts
{
    private static $ins = null;

    public static function init()
    {
        add_action('plugins_loaded', array(__CLASS__, 'instance'));
    }

    public static function instance()
    {
        is_null(self::$ins) && self::$ins = new self;
        return self::$ins;
    }

    protected function __construct()
    {
        add_action('p2p_init', array($this, 'connections'));
    }

    public function connections()
    {
        p2p_register_connection_type(array(
            'name'      => 'page_to_posts',
            'from'      => 'page',
            'to'        => 'post',
            'admin_box' => array(
                'show'    => 'from', // only show on pages
                'context' => 'advanced', // put admin box in main col, instead of side
            ),
        ));
    }
}

Getting connected posts on the front end is simple as well. Somewhere in the loop:

<?php
$connected = p2p_type('pages_to_posts')->get_connected($post->ID);
if($connected->have_posts())
{
    while($connected->have_posts())
    {
        $connected->the_post();
        // normal loop stuff here
    }
}

If you don't want to use that plugin, there are a few options.

  1. Associate a category with a given page, pull posts in from that category. You can either make this static or add a meta box to do that.
  2. Associated posts with pages via some sort of multi select (again, in a meta box).

I can provide you some example code for the above two options, but I'd strongly suggest you check out Posts 2 Posts.

0

If I understand you correctly, you'd need something like WP_Query to iterate through your different categories, pages, and etc. Use this:

function myAwardsPage() {

    // The Query 

    $the_query = new WP_Query( 'category_name=awards' );
    // get Params from: http://codex.wordpress.org/Class_Reference/WP_Query#Parameters
    // if multiple use array('','')

    // The Loop 
    while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <?php the_title(); ?>
    <?php the_content(); ?>

    <?php endwhile;

     // Reset Post Data
     wp_reset_postdata();
}

and add it to your page-template.php file then wherever you want to display that query put:

 <?php myAwardsPage(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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