• Resolved techdriver

    (@techdriver)


    I have been searching forums and docs on how to do this but nothing has worked so far.
    Currently this is what I have and it works.

    <?php get_header();?>
    <div id=”main”>
    <div id=”content”>

      Latest News

    <?php rewind_posts(); ?>
    <?php
    if (is_home()) {
    query_posts(“cat=3”);
    }
    ?>

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class=”post2″ id=”post -<?php the_ID(); ?>”>

    • ” rel=”bookmark”><?php the_title(); ?>
    • </div>
      <?php endwhile; else: ?>

      <?php endif; ?>

      <?php include (TEMPLATEPATH . ‘/welcome_page.php’); ?>
      </div>
      <?php get_sidebar();?>
      <?php get_footer();?>

      Ok this is what I want to do.
      Have the resent posts from a specific category(i.e. news) in a list at the top of the main page and Also display a specific “page id” on that same page. I am using a home.php file template. I am not an expert on php I just want to know the proper way on doing this.

Viewing 7 replies - 1 through 7 (of 7 total)
  • I assume you mean “recent” posts… Also, by page_id you mean a Page (Write > Write Page) in WordPress.

    The following uses the WP_Query() class (it accepts the same arguments as query_posts()) that will display the titles for the last 3 posts in category 3. It’s more beneficial than using query_posts() as it will not whack the default $posts object on a page:

    <?php
    $recent = new WP_Query('cat=3&showposts=3');
    while ( $recent->have_posts() ) : $recent->the_post();
    ?>
    <div class="post2" id="post-<?php the_ID(); ?>">
    <h2><?php the_title(); ?></h2>
    </div>
    <?php endwhile; ?>

    Works the same for calling up a Page:

    <?php
    $mypage = new WP_Query('page_id=10');
    while ( $mypage->have_posts() ) : $mypage->the_post();
    ?>
    <div class="page" id="post-<?php the_ID(); ?>">
    <h2><?php the_title(); ?></h2>
    </div>
    <?php endwhile; ?>

    Thread Starter techdriver

    (@techdriver)

    I tryed the code above and I get this

    Parse error: syntax error, unexpected T_WHILE in
    ???

    Sorry, little coding slip on my part. The statements above have been corrected.

    Thread Starter techdriver

    (@techdriver)

    Well it works but I get this msg also

    Fatal error: Call to a member function on a non-object in /home.php on line 20

    This is what I have…I am not sure if this is the correct way to code this.

    <?php get_header();?>
    <div id=”main”>
    <div id=”content”>
    Latest News – see all

    <?php
    $recent = new WP_Query(‘cat=3&showposts=3’);
    while ( $recent->have_posts() ) : $recent->the_post();
    ?>
    <div class=”post2″ id=”post-<?php the_ID(); ?>”>

      <?php the_title(); ?>

    </div>
    <?php endwhile; ?>

    <?php
    $page = new WP_Query(‘page_id=5’);
    //this is the line where it said the error is\\ while ( $page->have_posts() ) : $page->the_post();
    ?>
    <div class=”page” id=”post-<?php the_ID(); ?>”>
    <h2><?php the_title(); ?></h2>
    </div>
    <?php endwhile; ?>
    </div>

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

    Thread Starter techdriver

    (@techdriver)

    Would this be loop problem?

    The first loop works fine but the second loop has an error but it does show the page.

    Can you run a second loop on the same page (static home.php)
    When I try I get an error like:
    error msg:Call to a member function on a non-object in /home.php line 21

    line21( on the second loop): while ( $page->have_posts() ) : $page->the_post();
    ?>
    Is there something I need to do to support two loops on the same page.

    Hmm. My suggested var name ($page) is conflicting with something in WordPress. So change the Page query code to:

    <?php
    $mypage = new WP_Query('page_id=1008');
    while ( $mypage->have_posts() ) : $mypage->the_post();
    ?>

    That appears to have no problems. (Lesson for the day: Make everything unique…)

    Thread Starter techdriver

    (@techdriver)

    Thanks alot that worked great.

    There is the code for displaying recent posts and displaying a specific “page” on a static home page

    <?php get_header();?>
    <div id=”main”>
    <div id=”content”>
    Latest News – see all
    <?php rewind_posts(); ?>
    <?php
    $recent = new WP_Query(‘cat=3&showposts=3’);
    while ( $recent->have_posts() ) : $recent->the_post();
    ?>
    <div class=”post2″ id=”post-<?php the_ID(); ?>”>

    • ” rel=”bookmark”><?php the_title(); ?> | Posted: <?php the_time(‘F j, Y’); ?>
    • </div>
      <?php endwhile; ?>

      <?php
      $mypage = new WP_Query(‘page_id=5’);
      while ( $mypage->have_posts() ) : $mypage->the_post();
      ?>

      <div class=”page” id=”post-<?php the_ID(); ?>”>
      <h2><?php the_title(); ?></h2>
      <div class=”entry”>
      <?php the_content(__(‘Continue Reading »’)); ?></div>
      </div>
      <?php endwhile; ?>

      </div>

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

      thanks again

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Resent posts and page display’ is closed to new replies.