Title: Need help styling posts with PHP
Last modified: July 7, 2018

---

# Need help styling posts with PHP

 *  [denisecontent](https://wordpress.org/support/users/denisecontent/)
 * (@denisecontent)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/need-help-styling-posts/)
 * This is rather a basic question. Generally I work in HTML,CSS & Javascript but
   I do have basic knowledge in PHP.
 * I need to have multiple “recommended” posts appear above the regular posts in
   a category. I checked various plugins, using sticky – nothing worked properly.
 * So… I am creating “recommended” categories so these posts will have their own
   unique category and I used the following code, found in WordPress Codex, to implement
   them in the category template:
 *     ```
       <?php 
       $args = array ( 'category' => ID, 'posts_per_page' => 10);
       $myposts = get_posts( $args );
       foreach( $myposts as $post ) :	setup_postdata($post);
        ?>
       //Style Posts here
       <?php endforeach; ?>
       ```
   
 * I, of course, replace ID with the new category ID I am creating.
 * In this case I am using the Business Services category page and created a Recommended
   Business Services.
 * Now I need to be know where to find the styling of posts to place in //Style 
   Posts here or a I presume that is what is needed to display the posts in the 
   same exact style as the posts I already have. If you look at my link you will
   see the area where the posts are just say “//Style Posts here” of course. I am
   trying to find the easiest way to do this – could someone please help! I know
   with time I will figure this out – but I could mess things up! A basic example
   that I can tinker with is what I am hoping to see. I know this is simple – I 
   am just having a bad day.
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fneed-help-styling-posts%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

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

 *  [RossMitchell](https://wordpress.org/support/users/rossmitchell/)
 * (@rossmitchell)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/need-help-styling-posts/#post-10470431)
 * I would look at how posts are styled in your particular theme. This doc will 
   let you workout which theme template file is used by your posts (or possibly 
   post archive since you are displaying multiple posts):
    [https://developer.wordpress.org/themes/basics/template-hierarchy/](https://developer.wordpress.org/themes/basics/template-hierarchy/)
   Of course this makes your code potentially theme dependent, but that is inevitable.
 *  Thread Starter [denisecontent](https://wordpress.org/support/users/denisecontent/)
 * (@denisecontent)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/need-help-styling-posts/#post-10470476)
 * I will take a look at that Ross – thank you! I will let you know if it gets me
   what I need.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/need-help-styling-posts/#post-10470512)
 * Another thing you could do is to use one of several plugins that identify what
   template is used on any particular page. Use it to determine the template used
   for a page of whose post styling you wish to copy. Open the template in a text
   editor and locate the code within the main WP loop. Such a loop should start 
   with
    `while ( have_posts() ): the_post();` and end with `endwhile;` Anything
   in between will be what you want to copy to replace //Style posts here. There
   will be minor variations like the presence or lack of <?php ?> tags or {braces}
   in place of : endwhile;. These will need to be coordinated to result in proper
   syntax.
 * BTW – when you use setup_postdata(), you need to declare `global $post;` somewhere
   before the foreach loop.
 * Another thing: You can create a category template for your theme even if it does
   not have one. Even so, it will likely be deleted when the theme is updated. To
   prevent this, create a [child theme](https://codex.wordpress.org/Child_Themes)
   to contain and protect your custom work.
 *  Thread Starter [denisecontent](https://wordpress.org/support/users/denisecontent/)
 * (@denisecontent)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/need-help-styling-posts/#post-10472444)
 * thanks bcworkz I do have category templates for each category. I wonder if the
   randomizer with pagination I coded into the functions.php will work with this
   and not just the posts loop? Not that important – I did not promise that.
 *  Thread Starter [denisecontent](https://wordpress.org/support/users/denisecontent/)
 * (@denisecontent)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/need-help-styling-posts/#post-10473752)
 * I am thinking about going over to Customizr (the theme I am using) support to
   see where the code is to display the posts properly. I followed bcworkz’s suggestion
   and came up with
 *     ```
         <?php while ( have_posts() ) : ##all other cases for single and lists: post, custom post type, page, archives, search, 404 ?>
                                       <?php the_post(); ?>
   
                                       <?php do_action ('__before_article') ?>
                                           <article <?php czr_fn__f('__article_selectors') ?>>
                                               <?php do_action( '__loop' ); ?>
                                           </article>
                                       <?php do_action ('__after_article') ?>
   
                                   <?php endwhile; ?>
       ```
   
 * Unfortunately nothing displays when you put this in. I tried an alternative one
   I found else where on the internet but it does not display the posts with any
   styling. I assume I think I am going to go over to the Customizr support page
   and see if someone can help me. Although I am a paying customer their actual 
   support now no longer helps with issues like this. If anyone has an answer for
   me, please post here – I will keep looking at the topic.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 9 months ago](https://wordpress.org/support/topic/need-help-styling-posts/#post-10476891)
 * You grabbed a little too much code, this should be all you need to replace the
   style posts here comment:
 *     ```
       <?php do_action ('__before_article') ?>
          <article <?php czr_fn__f('__article_selectors') ?>>
             <?php do_action( '__loop' ); ?>
          </article>
       <?php do_action ('__after_article') ?>
       ```
   
 * Don’t forget to add `global $post;` before the foreach loop 🙂
 * If that still does not do it, your theme has not hooked into the ‘__loop’ action
   as it normally does. Your options then are to either find the action callback
   and add the hook yourself, or recreate the callback based on page output that
   is styled the way you want.

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

The topic ‘Need help styling posts with PHP’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 6 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [7 years, 9 months ago](https://wordpress.org/support/topic/need-help-styling-posts/#post-10476891)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
