Support » Developing with WordPress » Need help styling posts with PHP

  • 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 to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • 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/
    Of course this makes your code potentially theme dependent, but that is inevitable.

    I will take a look at that Ross – thank you! I will let you know if it gets me what I need.

    Moderator bcworkz

    (@bcworkz)

    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 to contain and protect your custom work.

    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.

    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

    (@bcworkz)

    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)
  • You must be logged in to reply to this topic.