Title: PHP code in template tag
Last modified: August 19, 2016

---

# PHP code in template tag

 *  Resolved [daand](https://wordpress.org/support/users/daand/)
 * (@daand)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/php-code-in-template-tag/)
 *     ```
       <?php
        $postslist = get_posts('numberposts=10&orderby=title');
        foreach ($postslist as $post) :
           setup_postdata($post);
        ?>
       ```
   
 * I need the php code beneath”
 *     ```
       <?php
       $category = get_the_category();
       echo $category[0]->cat_ID;
       ?>
       ```
   
 * To be inserted in the code above.
 * So i can display: `get_posts('numberposts=10&category=.......&orderby=title');`
   
   and replace ….. with the cateogry ID number.
 * Does anyone understand this?

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

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/php-code-in-template-tag/#post-1514050)
 * How about:
 *     ```
       <?php
       $category = get_the_category();
       $cat_id = $category[0]->cat_ID;
       $postslist = get_posts('numberposts=10&orderby=title&cat=' . $cat_id);
       foreach ($postslist as $post) :
       setup_postdata($post);
       ?>
       ```
   
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/php-code-in-template-tag/#post-1514052)
 * Do you mean
 *     ```
       get_posts('numberposts=10&category=' . $category[0]->cat_ID . '&orderby=title');
       ```
   
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/php-code-in-template-tag/#post-1514065)
 * Don’t you have to use `cat` for category ids? Or have I missed something?
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/php-code-in-template-tag/#post-1514101)
 * Righto!
 *     ```
       get_posts('numberposts=10&cat=' . $category[0]->cat_ID . '&orderby=title');
       ```
   
 *  Thread Starter [daand](https://wordpress.org/support/users/daand/)
 * (@daand)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/php-code-in-template-tag/#post-1514125)
 * Esmis solution worked!!
    Thanks very much!!
 *  Thread Starter [daand](https://wordpress.org/support/users/daand/)
 * (@daand)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/php-code-in-template-tag/#post-1514256)
 * What if i want to exclude the current post in the get posts code?
 * `$postslist = get_posts('numberposts=1000&order=ASC&exclude=....&orderby=date&
   cat=' . $cat_id);`
 * In which case … should be the current post ID.
 * The current post ID template tag is; `the_ID()` I thought.
 * I would very much appericiate it when someone could explain how it works, because
   i dont want to bother you every time..
 * Thanks in advance!
    Daan

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

The topic ‘PHP code in template tag’ is closed to new replies.

 * 6 replies
 * 3 participants
 * Last reply from: [daand](https://wordpress.org/support/users/daand/)
 * Last activity: [15 years, 10 months ago](https://wordpress.org/support/topic/php-code-in-template-tag/#post-1514256)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
