0

Does anyone know how to structure a WP template in the loop by:

Publishing the page content on top, immediate followed by certain category posts underneath the page content?

Seems like it should be straight forward. I have not been able to locate any help on it.

Thank you.

Here's what I pulled together so far:

<?php
/**
 * Template Name: Page & Posts
 */
?>
<?php get_header(); ?>
<div id="page">
    <div class="content">
        <article class="article">
            <div id="content_box">
            <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
                <div id="post-<?php the_ID(); ?>" <?php post_class('g post'); ?>>
                    <div class="single_page">
                        <?php if ($options['mts_breadcrumb'] == '1') { ?>
                            <div class="breadcrumb"><?php the_breadcrumb(); ?></div>
                        <?php } ?>
                        <header>
                            <h1 class="title"><?php the_title(); ?></h1>
                        </header>
                        <div class="post-content box mark-links">
                            <?php the_content(); ?>
                        </div>
                    </div>
                </div>
                <?php comments_template( '', true ); ?>
            <?php endwhile; ?>
                <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                    <div class="post excerpt">
                        <header>
                            <span class="thecomment">
                                <p><a href="<?php comments_link(); ?>"> <?php comments_number('0','1','%'); ?></a></p>
                            </span>                     
                            <h2 class="title">
                                <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a>
                            </h2>
                            <?php if($options['mts_headline_meta'] == '1') { ?>
                                <div class="post-info"> 
                                    <span class="thetime"><?php _e('Written on ',''); the_time('F j, Y'); ?></span>
                                    <span class="theauthor"><?php _e('by ',''); the_author_posts_link(); ?></span>
                                    <span class="thecategory"><?php _e(' in ',''); the_category(', ') ?></span>
                                </div>
                            <?php } ?>
                        </header>
                        <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="nofollow" id="featured-thumbnail">
                            <?php if ( has_post_thumbnail() ) { ?> 
                                <?php echo '<div class="featured-thumbnail">'; the_post_thumbnail('featured',array('title' => '')); echo '</div>'; ?>
                            <?php } ?>
                        </a>
                        <div class="post-content image-caption-format-1">
                            <?php echo excerpt(53);?>
                        </div>
                        <div class="readMore"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark"><?php _e('Read More',''); ?></a></div>
                    </div>
                <?php endwhile; endif; ?>   
                <?php if ($options['mts_pagenavigation'] == '1') { ?>
                    <?php pagination($additional_loop->max_num_pages); ?>
                <?php } else { ?>
                    <div class="pnavigation2">
                        <div class="nav-previous"><?php next_posts_link( __( '&larr; '.'Older posts', '' ) ); ?></div>
                        <div class="nav-next"><?php previous_posts_link( __( 'Newer posts'.' &rarr;', '' ) ); ?></div>
                    </div>
                <?php } ?>
            </div>
        </article>
        <?php get_sidebar(); ?>
<?php get_footer(); ?>
3
  • You going to have to use 2 loops here using WP_Query, that's my guess. May 22, 2014 at 18:27
  • 1
    You can use get_posts or WP_Query with two loops. May 22, 2014 at 18:27
  • 1
    Just to clarify, are you looking for related post below your post, or just category posts from a specific category in general May 22, 2014 at 18:39

3 Answers 3

4

This is very straightforward, at least from development point of view.

  1. You need to create Page Template for the page.
  2. Main Loop will take care of page itself.
  3. Follow it by adding secondary loop with arguments for set of posts you need.
1
  • 2
    @H.Ferrence Codex has quite a few examples of loops and templates, if you are still having trouble you can elaborate on your question by adding your code progress to it.
    – Rarst
    May 22, 2014 at 18:35
3

Here is a code snippet for the second loop:

<?php $args = array (
    'cat' => array ( 1,2,3 ), //replace with your category ids
    'posts_per_page' => 5 //change it as needed
   );
$category_posts = new WP_Query ( $args );

if( $category_posts->have_posts() ):
    while( $category_posts->have_posts() ):
        $category_posts->the_post(); ?>
        <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
        <p><?php the_excerpt(); ?></p>
    <?php endwhile; 
endif; ?>

Use your css selectors to style it perfectly. Hope this helps.

2
  • 2
    +1 for the example loop, but no need to call wp_reset_postdata() as the new WP_Query gets saved into its own variable and does not interfere with the main $wp_query and the $wp_the_query contents.
    – kaiser
    May 22, 2014 at 22:03
  • @kaiser Thanks for the info. I have updated my answer accordingly. May 23, 2014 at 9:40
-3

UPDATE: Do not use this answer. I recommend you use the answer provided by @The Sufi below.

@H.Ferrence: I highly recommend you "un-accept" this answer

11

Your Answer

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