views:

74

answers:

4

Hi all,

I have three pages on my site. Let's call them home, page2, and page3. My 'home' page is set as a static front page. My 'page2' is set up as the blog page.

What I want is the following:

I want page2 to display blog posts with a certain category (of which ID is known).

AND

I want page3 to display blog posts with a certain category (of which ID is known).

The PHP code to only show posts with a certain category (or actually in my case, show posts excluding two categories) is the following:

<?php query_posts($query_string . '&cat=-3,-8'); ?>
<?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
        <div class="post" id="post-<?php the_ID(); ?>">
        <h3><a href="<?php the_permalink() ?>" rel="bookmark"
            title="Permanent Link to <?php the_title_attribute(); ?>">
            <?php the_title(); ?></a></h3>
        <?php the_excerpt('Read the rest of this entry &raquo;'); ?>
        </div><!-- /.post-->

Now, in my page.php, I have the following code to display posts with one category:

<?php
    // BEGIN IF PAGE is newspaper articles page
    if ( is_page('newspaper') ) {

        //BEGIN POST REGION

        query_posts($query_string . '&cat=8'); ?>
        <?php if (have_posts()) : ?>
            <?php while (have_posts()) : the_post(); ?>
                <div class="post" id="post-<?php the_ID(); ?>">
                <h3><?php the_title(); ?></h3>

                <?php the_content('Read more &raquo;'); ?>


                </div><!-- /.post-->

            <?php endwhile; ?>

        <?php else : ?>

        <?php endif; ?>

        <?php

    } //end if is_page
?>

But it doesn't show the proper posts on the newspaper page (or page3 in this question). It does, however, work for the articles page (main index.php blog page).

EDIT: I've also tried the following (but it doesn't work). I put this in the index.php file:

<?php
if ( is_page('newspaper') || is_home() ) { // START if is home

?>
<?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
        <div class="post" id="post-<?php the_ID(); ?>">
            <h3><a href="<?php the_permalink() ?>" rel="bookmark"
                title="Permanent Link to
                <?php the_title_attribute(); ?>">
                <?php the_title(); ?></a></h3>

            <!--<p><?php the_time('F jS, Y') ?> <?php //the_author() ?></p>-->

            <?php the_excerpt('Read the rest of this entry &raquo;'); ?>


        </div><!-- /.post-->

    <?php endwhile; ?>

<?php else : ?>

<?php endif; ?>

<?php
} //end if is_home() or is_page()
?>

Again, this shows the posts on the main blog page, but doesn't show any posts on the newspaper page...

The question is therefore simple (I think). How do I show posts on another page OTHER than the main blog page?

Thanks! Amit

A: 

I think you have to make different templates for different page. Check this link http://codex.wordpress.org/Pages

Rojan
A: 

I think this thread answers the question and does what you want. http://wordpress.org/support/topic/show-only-x-category-posts-on-page?replies=9#post-1053767

Jamie Morgan
A: 

Using the string 'newspaper' in is_page('newspaper') is a potential source of the problem. It might be misspelled easily. Have you ever tried using the page id? Something like

is_page('999')
sprain
It's not a matter of misspelling. I've tried page_id as well
Amit
+1  A: 

Rather than exclude categories and exclude pages and change the standard Wordpress loop, use a new query, like this:

<?php $my_query = new WP_Query('category_name=mycategory&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a></h3>
<?php the_excerpt('Read the rest of this entry &raquo;'); ?>
<?php endwhile; ?>

This can be used inside the standard WP loop and be used mutiple times in a page/post or page template without conflicting. (Enable php execution to use it in the page/post editor). Function Reference/WP Query « WordPress Codex

This also works well to use page templates to create different pages with blog posts: Page Templates « WordPress Codex, but don't forget that WP also uses category pages, too, depending on your theme: Category Templates « WordPress Codex.

songdogtech
Let me try your way. I must say though, that I've read on the Wordpress forums that using two different pages to show two different type of posts is not possible. Instead, you can use categories, where the page shown is actually the archive of the category. However, if your solution works, you've just saved me a ton of work.
Amit
Unfortunately using categories would not work for me because the two pages that need to have posts in them are drop-down pages from a parent link (and I'm using dynamic navigation, not hard-coded)...There's no way to give a category a parent-page (only a parent-category).
Amit
This worked. You're a genius.
Amit
You should comment on the Wordpress forums that claim that accomplishing this is impossible and is not a built-in feature of Wordpress, that Wordpress only has 1 blog page. Trust me, there are several of those threads.
Amit
Glad that worked; I added some notes on page and category templates. You could add category pages as drop downs, but like you say they might have to be hardcoded.
songdogtech