We help IT Professionals succeed at work.

Display additional post titles on my home page

adrian7474
adrian7474 asked
on
Hi, I am trying to put a div that will list out 10 additional post titles  from the 10 already being displayed on my home page.
Basically, in the reading section of my admin panel, i have the post set to display 10 post. I would like to display the NEXT 10 additional post by title only. (to keep bandwidth and site-load down on my site).
I am experimenting (with the attached code) below the loop in the index.php file in my theme. I was reading the codex page of wordpress, but sadly, my php skills only go so far. I am getting it display the post titles however they are not formatted(listed out) and they are not the 10 additional post, but all of them.
Can someone help me tweak this to work properly?

Also, I read using two loops was bad, so if there is another way to do it, let me know.
<?php
// test code for post box
$query = 'posts_per_page=10';

$queryObject = new WP_Query($query);

// The Loop...
if ($queryObject->have_posts()) {
	while ($queryObject->have_posts()) {
		$queryObject->the_post();
		the_title();
		
	}
}
?>

Open in new window

Comment
Watch Question

you can do two queries no problem ...

in principle if you don't want 2 queries...

select 20 records.

$i = 0;
while statement
{
echo the title
if ($i <10)
{
 echo the body
}

$i++;
}

Author

Commented:
I'm sorry, so where would i insert this code. Would it be in the loop? Like this?

<?php
// test code for post box
$query = 'posts_per_page=10';

$queryObject = new WP_Query($query);

// The Loop...
if ($queryObject->have_posts()) {
	$i = 0;
while statement 
{
echo the title
if ($i <10)
{
 echo the body
}

$i++;
}
		
	}
}
?>

Open in new window

Commented:
p_nuts is partly right, set posts number to 20  in the reading section of your admin panel, then in the loop of the corresponding template count the posts, display first ten as usual, and then display the rest just the way you like it. Remember you can structure it and style it however you like. I believe it's just a matter of couple additional conditionals.

If you do not want to change posts number site-wide, then call query_posts('posts_per_page=20') just before your loop starts:


...
 <?php query_posts('posts_per_page=20'); ?>
 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...

Open in new window

Author

Commented:
Guys, I can't get it working. Can someone place the exact code i would use?

Commented:
Can you post the contents of corresponding template here? I will alter it as needed.

Author

Commented:
sure. here is what i got so far.
<?php if ( have_posts() ) : ?>
      <?php while ( have_posts() ) : the_post() ?>
      <?php include ('entry_main.php') ?>
      <?php endwhile; ?>
      <?php else : ?>
      <li class="no-posts">
        <h3>
          <?php _e( 'No posts yet!', 'p2' ) ?>
        </h3>
      </li>
      <?php endif; ?>
    </ul>
	<?php // test code for post box  ?>
   <?php rewind_posts(); ?>
 <?php 

 query_posts('posts_per_page=10&offset=10');
echo the_title();
?>

Open in new window

Commented:
The main reason your code won't work is because you actually have to display the loop again

 query_posts('posts_per_page=10&offset=10');
 echo the_title();

should be

 query_posts('posts_per_page=10&offset=10');
 if ( have_posts() ) : while ( have_posts() ) : the_post()

 the_title();

 endwhile; endif;

But, since your first query was only 10 posts, rewinding that loop will display those 10 posts again.  You need to create another query object.

If you want to display them in a list, try this
$list_posts = new WP_Query('posts_per_page=10&offset=10');

if ( $list_posts->have_posts() ) : 
 
 echo "<ul>";

 while ( $list_posts->have_posts() ) : $list_posts->the_post()

  the_title('<li>', '</li>');

 endwhile; 

 echo "</ul>";

endif;

Open in new window

Sweet, that worked, though I had to tweak the code a bit by putting everything in it's own php tag. See code below
thanks again.
<?php 
$list_posts = new WP_Query('posts_per_page=10&offset=10');?>

<?php if ( $list_posts->have_posts() ) : ?>
 
 <?php echo "<ul>"; ?>

<?php  while ( $list_posts->have_posts() ) : $list_posts->the_post() ?>

 <?php the_title('<li>', '</li>'); ?>

<?php endwhile; ?>

 <?php echo "</ul>"; ?>

<?php endif;?>

Open in new window