WordPress.org

Forums

[resolved] Displaying posts from a category within a static page (5 posts)

  1. guided
    Member
    Posted 6 years ago #

    I'm trying to display all the posts, up to the more tag, from a given category at the bottom of my Kungfu Holidays page.

    I've tried a number of approaches, but had no luck. At the moment I'm using the 'Latest Posts by Category Archive WP Plugin' but that strips the formatting and restricts the exceprt length.

    I'd like the posts to keep the formatting of my archives, here.

    Any suggestions?

  2. stvwlf
    Member
    Posted 6 years ago #

    Hi

    I've not used this plugin, but it sounds like it might do what you want.
    http://wordpress.org/extend/plugins/post-teaser/
    You would use it with a custom query loop at the bottom of the page. The plugin appears to override the default the_excerpt() code, so you activate it with the_excerpt() but its code runs instead - respects the <!-- more --> tag

    <?php
    $catPosts = new WP_Query();
    $catPosts->query('showposts=5&cat=5'); // use your cat ID code
    while ($catPosts->have_posts()) : $catPosts->the_post(); ?>
    <h3>" rel="bookmark"><?php the_title(); ?></h3>
    <?php the_excerpt();
    endwhile; ?>

  3. stvwlf
    Member
    Posted 6 years ago #

    screwed up the code - should be this

    <?php
    $catPosts = new WP_Query();
    $catPosts->query('showposts=5&amp;cat=5'); // use your cat ID code
    while ($catPosts->have_posts()) : $catPosts->the_post(); ?>
      <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
      <?php the_excerpt();
    endwhile; ?>
  4. guided
    Member
    Posted 6 years ago #

    Thanks stvwlf. I had given up on this, and was going to find a less satisfactory workaround. Your help got me to give it another go.

    I've managed to get something resembling what I want, which, with a little styling, should be just about right.

    I used a mixture of antistandard's solution, found here, and a solution I found in the codex, here.

    My code, placed after page.php's <?php endwhile; endif; ?> looks like this:

    <?php query_posts('cat=255');?>
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    	<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    	<?php
    		// Display content above the more tag
    		$more = 0;
    		the_content("More...");
    	?>
    
    	<?php endwhile; else: endif; ?>

    Not sure if that's the neatest way to do it, but it seems to work.

  5. guided
    Member
    Posted 6 years ago #

    Oh, I should mention that I had to place the code below outside the loop.

    <?php
    		// Declare global $more, before the loop.
    		global $more;
    	?>

Topic Closed

This topic has been closed to new replies.

About this Topic