0

My question is about data organization more than code, I hope it still has its place here. My site's main menu has an item pointing to a page called "Videos". This page has 2 main parts:

  • The last "video" (a Custom Post Type I created) post content
  • A list of links of the other "videos" posts

Clicking one of these links leads to a similar page (identical layout), except the video on top, this time, is the one corresponding to the link.

Question is, should these pages be separate or the same one? Could it be the "video" archive page for instance, whose custom template displays on top the video passed as query string (or the last video if there's no parameter)? Or, should I use the archive page on the one hand and the single CPT page on the other, in which case I'd end up with 2 almost identical templates? What's the best way to organize this?

1

I vote for having two separate pages since this gives you all the power of permalinks that go directly to a specific video. That's much better for you and your visitors since the content can be more easily shared.

From the codes perspective, you can apply DRY principles and still use a single-video.php and archive-video.php template. Have a look at the codex article about the get_template_part function that allows you to split your template into sub-parts.

For example you would create a file called video-top.php that displays the video on the top of the page and another file called video-list.php the is just part of a list of links to the other videos.

Then in the template single-video.php you would show the specific video at the top by including the template part and then afterwards a custom loop of the other videos but excluding that top video:

<?php

// the top video
if ( have_posts() ) {
    while ( have_posts() ) {
        get_template_part( 'video', 'top' ); // includes video-top.php 
    } // end while
} // end if

// The list of other videos
$video_query = new WP_Query( array( 'post_type' => 'video', 'post__not_in' => array( get_the_ID() ) ) );

if ( $video_query->have_posts() ) {
    echo '<ul>';
    while ( $video_query->have_posts() ) {
        $video_query->the_post();
        get_template_part( 'video', 'list' ); // includes video-top.php
    }
    echo '</ul>';
}
/* Restore original Post Data */
wp_reset_postdata(); ?>

The content of these files could be something like this:

<li><a href="<?php echo get_permalink( get_the_ID() ) ?>"><?php the_title() ?></a></li>

And in video-top.php:

 <h1><?php the_title() ?></h1>
 <?php the_content() ?>

In archive-video.php your loop could look like this:

<?php

// the top video
if ( have_posts() ) {
    while ( have_posts() ) {
        if ( 0 == $current_post ) { // only runs for the first video
            get_template_part( 'video', 'top' ); // includes video-top.php
        } else {
            get_template_part( 'video', 'list' ); // includes video-list.php
        }
    } // end while
} // end if

$current_post is a global variable the is available during the loop and counts up for each post.

  • I used your solution as the basis and ended up with something very close to it, thanks! – drake035 May 5 '15 at 12:06
  • Glad to hear that! – Jan Beck May 5 '15 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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