• I am looking for a way to display three full posts on one of the wordpress pages. The page needs to be editable as a standard page, then I’d like the most recent post from three separate categories to be displayed after it. I figured if there were a way to set up a template file that could somehow use four separate loops, one for the page and one for each of the necessary posts. But, I’m not really PHP savvy enough to know just how to do that from scratch.

    I searched for a plugin that might do it, but to no avail. Any suggestions would be really appreciated.

Viewing 6 replies - 1 through 6 (of 6 total)
  • This works as page.php for the WordPress Default theme…

    <?php
    /**
     * @package WordPress
     * @subpackage Default_Theme
     */
    
    get_header(); ?>
    
    	<div id="content" class="narrowcolumn" role="main">
    
    <?php
    //for categories 1,3,16 show 1 post
    $cat_args=array(
      'include' => '1,3,16',
      'orderby' => 'name',
      'order' => 'ASC'
       );
    $categories=get_categories($cat_args);
      foreach($categories as $category) {
        $args=array(
          'showposts' => 1,
          'category__in' => array($category->term_id),
          'caller_get_posts'=>1
        );
        $posts=get_posts($args);
          if ($posts) {
            echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
            foreach($posts as $post) {
              setup_postdata($post); ?>
              <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
              <?php
            } // foreach($posts
          } // if ($posts
        } // foreach($categories
    ?>
    
    		<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    		<div class="post" id="post-<?php the_ID(); ?>">
    		<h2><?php the_title(); ?></h2>
    			<div class="entry">
    				<?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?>
    
    				<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
    
    			</div>
    		</div>
    		<?php endwhile; endif; ?>
    	<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
    	</div>
    
    <?php get_sidebar(); ?>
    
    <?php get_footer(); ?>
    Thread Starter bpetruzzo

    (@bpetruzzo)

    I tinkered with that file a bit, but I don’t think that it’s the solution I’m looking for. The page needs to be a true page. One where the content can be edited through the edit-pages section of the admin area. Then, following that, one post from each of three separate categories needs to display below it.

    As far as I can see, this will only display the newest post from one of three categories, but it also takes over so that the page’s native content isn’t displayed.

    Is it possible to use more than one loop in the same template file?

    Thread Starter bpetruzzo

    (@bpetruzzo)

    I should have read the article on using multiple loops. Doing so, I’ve made some progress.

    Using rewind_posts() I was able to display more than one loop. But, being that it’s a page and not a post, I can’t get it to pull in a post. I tried using this bit of code from the default theme:

    <?php
    //for categories 1,3,16 show 1 post
    $cat_args=array(
      'include' => '1,3,16',
      'orderby' => 'name',
      'order' => 'ASC'
       );
    $categories=get_categories($cat_args);
      foreach($categories as $category) {
        $args=array(
          'showposts' => 1,
          'category__in' => array($category->term_id),
          'caller_get_posts'=>1
        );
        $posts=get_posts($args);
          if ($posts) {
            echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
            foreach($posts as $post) {
              setup_postdata($post); ?>
              <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
              <?php
            } // foreach($posts
          } // if ($posts
        } // foreach($categories
    ?>

    But, this isn’t loop dependent and instead, breaks the whole thing giving me this error message:

    Parse error: syntax error, unexpected $end in /home/bacclead/public_html/wp-content/themes/BACCLeadership/template-uppage.php on line 89

    I’m betting that the solution is in that bit of code from the default theme. But my PHP skills aren’t good enough to know how to get it to work independently for each separate loop.

    Thread Starter bpetruzzo

    (@bpetruzzo)

    Success.

    I found a plugin that could be twisted to do what I needed. It’s called BNS Featured Category. In the template file I added a custom widget section below the loop. Then, I added three instances of the BNS Featured Category plugin to it and under the settings assigned which category and how many posts.

    Perhaps a little jerry-rigged, but it works like a charm.

    Thread Starter bpetruzzo

    (@bpetruzzo)

    I spoke too soon.

    The widget solution will only work once per page. I couldn’t tell because the BNS Featured Category widget uses error trapping which prevented it from telling me there were problems using it more than once.

    I know there must be a way to pull posts onto a page like I’m trying to do. But I’m stretching my PHP ability really thin.

    Can someone please help with this?

    Well the code you posted worked just fine for me…please paste all the code from the theme template file template-uppage.php into a pastebin, such as wordpress.pastebin.ca, and report the link back here. Maybe someone might spot your problem. Thanks.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Display posts within a page’ is closed to new replies.