Title: help with this code
Last modified: August 19, 2016

---

# help with this code

 *  [raptrex](https://wordpress.org/support/users/raptrex/)
 * (@raptrex)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/help-with-this-code/)
 * `<?php $posts = get_posts( “category=2&numberposts=3” ); ?>
    <?php if( $posts):?
   >
 * <div class=”post” id=”modernScience”>
    <h2>Modern Science</h2> <?php foreach(
   $posts as $post ) : setup_postdata( $post ); ?> <div class=”pleft”> <h3 class
   =”entry-title”>” title=”<?php printf(__(‘Permalink to %s’), wp_specialchars(get_the_title(),
   1)) ?>” rel=”bookmark”><?php the_title() ?></h3> <div class=”entry-content”> 
   <?php the_content(“<span class=\”continue\”>” . __(‘Continue reading’,”) . ” ‘”.
   the_title(”, ”, false) . “‘</span>”); ?> </div> </div> <div class=”pright”>
    - 
 *  </div>
    <?php endforeach; ?>
 * </div>
 * <?php endif; ?>
 * <?php $posts = get_posts( “category=3&numberposts=3” ); ?>
    <?php if( $posts ):?
   >
 * <div class=”post” id=”lifestyle”>
    <h2>Lifestyle</h2>
    -  <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
       <div class
      =”pleft”> <h3 class=”entry-title”>” title=”<?php printf(__(‘Permalink to %
      s’), wp_specialchars(get_the_title(), 1)) ?>” rel=”bookmark”><?php the_title()?
      ></h3> <div class=”entry-content”> <?php the_content(“<span class=\”continue\”
      >” . __(‘Continue reading’,”) . ” ‘” . the_title(”, ”, false) . “‘</span>”);?
      > </div> </div> <div class=”pright”>
 *  - 
 *  </div>
    <?php endforeach; ?>
 * </div>
 * <?php endif; ?>
 * what i want this to do is this:
    display the latest post from the category in
   pleft and then display the title of the next 5 posts from the category in pright
   how would i do it to this code?

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

 *  Thread Starter [raptrex](https://wordpress.org/support/users/raptrex/)
 * (@raptrex)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/help-with-this-code/#post-673958)
 * here it is on pastbin
    [http://paste.uni.cc/17953](http://paste.uni.cc/17953)
 *  [Andrew Ozz](https://wordpress.org/support/users/azaozz/)
 * (@azaozz)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/help-with-this-code/#post-673959)
 * This gets 3 posts from one category and 3 posts from another… Get all 6 posts
   from the same category first:
    `$posts = get_posts( "category=2&numberposts=6");`
 * You only need one “foreach()” loop, but instead of doing
    `foreach( $posts as
   $post )` make it:
 *     ```
       foreach( $posts as $num => $post ):
           if ( $num == 0 ) // this is the first post
           else // these are the rest
       ```
   
 *  Thread Starter [raptrex](https://wordpress.org/support/users/raptrex/)
 * (@raptrex)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/help-with-this-code/#post-673960)
 * thx for the reply
 * this is what you meant right?
    [http://paste.uni.cc/17955](http://paste.uni.cc/17955)
 * ill test it when i get home
 *  Thread Starter [raptrex](https://wordpress.org/support/users/raptrex/)
 * (@raptrex)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/help-with-this-code/#post-673983)
 * alright testing the code out and got some problems
    the_content doesnt show anything,
   just a </p> with no <p>
 * next the “url” or second category only shows the latest post in pright and none
   of the others
    pright works fine for the first category chat
 * heres my index.php
    [http://paste.uni.cc/17957](http://paste.uni.cc/17957)
 *  [Andrew Ozz](https://wordpress.org/support/users/azaozz/)
 * (@azaozz)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/help-with-this-code/#post-674056)
 * Raptrex, get_posts() returns an array of objects. You can either use `setup_postdata(
   $post);` just after foreach(), or apply individual filters like this:
    `echo 
   apply_filters('the_title', $post->post_title);` etc.
 *  Thread Starter [raptrex](https://wordpress.org/support/users/raptrex/)
 * (@raptrex)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/help-with-this-code/#post-674185)
 * thx azaozz
    it worked until i started adding more stuff and posts
 * now my categories are not in order in the <h2> part such as here
    `<h2><?php 
   the_category(', '); ?> &raquo;</h2>`
 * its suppost to show categories 1,4,3 in that order
    but it shows 3,1,4 the posts
   show up fine in its correct categories just this h2 part is out of order
 * my latest code
    [http://paste.uni.cc/17998](http://paste.uni.cc/17998)
 * all of my posts are in a single category, none are in both 3 and 4 or anything
   like that
 *  Thread Starter [raptrex](https://wordpress.org/support/users/raptrex/)
 * (@raptrex)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/help-with-this-code/#post-674193)
 * lol just made another category “6”
    so i replaced 1 with 6
 * now it shows 6,6,4
    when its suppose to be 6,4,3
 * oh and im doing this on my computer, not on a server
 *  [Andrew Ozz](https://wordpress.org/support/users/azaozz/)
 * (@azaozz)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/help-with-this-code/#post-674209)
 * The current code runs loop whitin a loop whitin a loop… You’re missing the `endforeach;`.
   Put one of them just before each `endif;`, like this:
    `endforeach;endif;`
 * Or if you prefer, use the other syntax with the curly brackets – see the examples
   [here.](http://us3.php.net/manual/en/control-structures.foreach.php)
 *  Thread Starter [raptrex](https://wordpress.org/support/users/raptrex/)
 * (@raptrex)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/help-with-this-code/#post-674210)
 * if i add endforeach; before each endif, i get this error
 * Parse error: parse error, unexpected T_ENDFOREACH in C:\xampplite\htdocs\wordpress\
   wp-content\themes\mag\index.php on line 35
 * with line 35 and each other line with causing this error being
    <?php endforeach;
   endif; ?>
 * then i tried using the curly brackets
 *     ```
       <?php foreach( $posts as $num => $post ) {
       	setup_postdata($post);
           if ( $num == 0 ) {// this is the first post ?>
       ```
   
 * taking out the colon “:”
    i got an error with line 34 endforeach; that i had 
   previously so i deleted that and this just messed everything up…
 *  [Andrew Ozz](https://wordpress.org/support/users/azaozz/)
 * (@azaozz)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/help-with-this-code/#post-674217)
 * Yea, it’s my error. The pastebin doesn’t highlight the code properly and I didn’t
   notice it.
 * It looks ok, perhaps you have some posts in more than one cat or in a sub-cat,
   they will be listed twice.
 *  Thread Starter [raptrex](https://wordpress.org/support/users/raptrex/)
 * (@raptrex)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/help-with-this-code/#post-674237)
 * nope, all of my posts are in a single category
 *  Thread Starter [raptrex](https://wordpress.org/support/users/raptrex/)
 * (@raptrex)
 * [18 years, 3 months ago](https://wordpress.org/support/topic/help-with-this-code/#post-674247)
 * lol anyone else that can help? so far only azaozz, whom i appreciate for replying,
   has helped

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

The topic ‘help with this code’ is closed to new replies.

## Tags

 * [loop](https://wordpress.org/support/topic-tag/loop/)

 * 12 replies
 * 2 participants
 * Last reply from: [raptrex](https://wordpress.org/support/users/raptrex/)
 * Last activity: [18 years, 3 months ago](https://wordpress.org/support/topic/help-with-this-code/#post-674247)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
