• Hi all,

    I needed a quick shove in the right direction.

    Several (but not all) of my categories list code samples, and I’d like to rig up a page to list the headings of all posts within those categories; each being a link to the full article.

    Any pointers on how to go about doing this? I suspect it’s a rudimentary problem, but I’m not having much luck locating the solution in the docs.

    thx!

Viewing 3 replies - 1 through 3 (of 3 total)
  • I’m not sure where I got this information from now but here’s some code that I’ve used to create a page template with posts in it:

    <?php
    /*
    Template Name: Samples
    */
    ?>
    <?php get_header(); ?>

    <?php $temp_query = $wp_query; // keeps page status intact when reversed below ?>

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <h2 class="posttitle" id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php _e('Permanent link to'); ?> <?php the_title(); ?>"><?php the_title(); ?></a></h2>

    <?php edit_post_link(__('Edit'), '', ''); ?>

    <div class="postentry">
    <?php the_content("__('Read the rest of this entry &raquo;')
    "); ?>
    <?php wp_link_pages(); ?>
    </div>
    <?php endwhile; endif; ?>

    <?php rewind_posts(); // repeat from here for multiple categories?>

    <?php query_posts('category_name=sample1'); ?>

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <h2 class="posttitle" id="post-<?php the_ID(); ?>"><a>" rel="bookmark" title="<?php _e('Permanent link to'); ?> <?php the_title(); ?>"><?php the_title(); ?></a></h2>

    <?php endwhile; endif; // repeat section end ?>

    <?php $wp_query = $temp_query; // returning the 'page' status?>

    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    The first section is the ‘page’ part with any text you might want to write to introduce the posts in the category below. Then just copy and paste the repeat section any number of times with different categories named. The page status stuff I found I needed in order for the sidebar code to realise it was a page rather than a post, however if you are using PHP5 rather than PHP4, as I am, on your server you’ll need to change

    <?php $temp_query = $wp_query; ?> to
    <?php $temp_query = clone $wp_query; ?>

    This goes in your theme folder (with adjustments I imagine) and you’ll choose Samples as you template when creating the page.

    PS I’ve never tried to post code before so hopefully it hasn’t got munged. If you use a text editor like Notepad2 – https://www.flos-freeware.ch/notepad2.html – the colours should make it a lot clearer if you save this to a textfile named eg samples.php

    Doesn’t the Category Template does this out of the box?
    (You just have to remove the_content tag…)

    Reading: https://codex.www.ads-software.com/Category_Templates

    What the code above is doing is creating a template for a WordPress Page (in the strict WordPress sense), not a template for archive post page.

    It lists the titles of the posts from categories defined in the repeated section, although I’ve just realised that you don’t need to repeat the repeat section just add categories to the query, ie

    <?php query_posts('cat=2,6,17,38'); ?> instead of

    <?php query_posts('category_name=sample1'); ?>

    What’s different is that if you have an <?php if (is_page()) { ?> statement in the sidebar then the bits of code at the beginning and the end keep the sidebar seeing the page as a Page, otherwise it would be seen as a post page.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Creating page listing all post headings in certain categories’ is closed to new replies.