Displaying posts categories separately on a single page

4 posts in this topic

Displaying posts categories separately on a single page.


On any "standard" WP template, one can easily display on a single page several blog posts categories, in separate blocs, using the following code and parameters:

<h3>Featured Category 1</h3>
<?php $recent = new WP_Query("cat=1&showposts=3&offset=1"); while($recent->have_posts()) : $recent->the_post();?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>

<h3>Featured Category 2</h3>
<?php $recent = new WP_Query("cat=2&showposts=3&offset=1"); while($recent->have_posts()) : $recent->the_post();?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>

How can this be achieved on PageLines' templates? How can I have on the same page — but in different "blocs" — my posts from Category 1 and those from Category 2?

Also — although only partially related — it seems that when a page is set as the "Posts page" in the "Reading" settings, the Blog template take precedence over any other template chosen in the "Edit Page". For example, if my page "Blog" uses "Template 1", where a QuickSlider is set to appear above the Content in the active sections, theQuickSlider is not displayed and instead appears the Callout section added to the Blog page template.

Share this post

Link to post
Share on other sites



For you to use that code, you can either use a hook or create your own section. However, I would recommend you have a look at AnyLoop in the PageLines store, as it may be what you're looking for.



Also, when you set a page as the Post Page, its considered a Special Page, therefore going to Wordpress Admin Dashboard > Pages > Select the Page you have set as your post page will not do anything. Now that the Page is set as the Post page and considered special you need to go to Wordpress Admin Dashboard > PageLines > Page Options > Blog, to customisation the layout, sections etc...

Share this post

Link to post
Share on other sites


Thank you for your input. Since I wasn’t really sure if AnyLoop was what I was looking for, I wrote a very basic section (my first one!) which retrieves the latest post of a given category, using first a new WP_Query, and other posts of the same category, using $theposts->load_loop();.


I provide below my “amateur” code for the discussion, but I wouldn’t advise anyone to use it...:

class PageLinesPostLoopGolok extends PageLinesSection {

    * Section template.
   function section_template() {
        //Included in theme root for easy editing.
        <div class="tncblog">
        <h1 class="bigtitle">BLOG</h1>

        <div class="featuredbox">
        <?php $recent = new WP_Query("cat=14&showposts=1"); while($recent->have_posts()) : $recent->the_post();?>
            <div class="featuredtitle"><h1><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h1></div>
            <h4>By <?php the_author_posts_link(); ?> | <?php the_time('j F Y') ?></h4>
            <div class="alignleft"><?php echo get_the_post_thumbnail( $id, array(150,150) /*echo get_the_post_thumbnail( null, 'small' );*/ ); ?></div>
                <?php /* the_content('Read more...'); */ ?>
                <?php the_excerpt(); ?>
                <p><a href="<?php the_permalink() ?>" rel="bookmark">Read more ></a></p>
        <?php endwhile; ?><br />


           //Included in theme root for easy editing.
           $theposts = new PageLinesPosts();



Although it would have been nice to have meta settings (to choose the category and the number of post), I’ll probably simply hardcode it as I don’t need such flexibility at present.

My “only” problem is about formatting. I noticed that if I use $theposts->load_loop(); posts are automatically formatted on two columns, even though “Magazine Layout Mode” is selected in the “Blog And Posts” page options. It’s ok, as I managed to format posts the way I want, but how could the formatting be controlled?

Once again, thanks for your help.

Share this post

Link to post
Share on other sites



Try this plugin:  http://wordpress.org/extend/plugins/special-recent-posts/ as it allows you to place code by shortcode, which means you can place it on any page, a post, a sidebar, etc.   In the shortcode, you can select a category, number of posts, and a number of other options.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Similar Content

    • Custom design for different category pages
      By mzhafzah
      I am using DMS 2 theme and I want to create a 2 different links on my website; each link should take you to a different blog post category. Although the process is easy, yet I can not figure out how to give each category page a unique design of its own; Any change I make on any page is applied to the other (I'm aware of the universal elements like the header and footer and I a am not talking about these.)
      Since I am new to wordpress in general, I am not sure if my description is sufficient; apologies if it is not.
    • Max posts in postloop for categories
      By jammersam
      Hi there,
      Having difficulty editing something on a site I'm working on.  In previous Pagelines versions, clicking a post category allowed one to see all of the posts in that category.  In the postloop for DMS, it seems I can only show max ten.  I'd like for any category (except the main blog page, which should remain in magazine mode with 10 max) to show its maximum content - any explanation on how I can do that?
      Thanks very much.
    • Setting Post Categories for Flexible Posts
      By ariel100
      I'm new to WHP6, just got.  Attempting to play with it and ran through the docs and all the guides I could find and hoping could get your help -
      In using the Flexible Posts, section,  I tried to split it into 2 side by side,  in so doing I wanted to restrict left side to posts from Category X  and right side to posts from Category Y.   The edit section lists  'Post Category' that made me think I could do so.  The only options I get though are category, post_tag, post_format  ..  I can't seem to set the specific category here.  Setting it to 'category' doesnt seem to accomplish anything with the posts that are selected / displayed. 
      Can you advise as to how this is best accomplished?  Or if there are documentation support for the sepcific sections that may indicate?
      Thanks for your help!
    • Categories not linking to proper display of posts in that category
      By jordangross
      Hi there-

      - I've got jetpack installed and am using the "categories" widget in a sidebar. It displays perfectly, showing each category and the number of posts in that category in parenthesis
      - permalinks is set to "post name"

      - when you click any of the categories, you always end up at the same generic page, not a dynamic list of the posts in that category

      Attempted & Unsuccessful fixes
      - disabled all plugins; result: instead of linking to a page, just went to 404
      - switched themes to twenty-fourteen; result: categories still linked to 404. Then when I switched back to dms, my home page was wiped out, all my settings, font changes, etc, were completely wiped. I put in a request from my host to restore from backup because the entire site was essentially lost (side-note: this also happens whenever dms updates, why?? It's SUPER inconvenient and frustrating)
      - changed perma links to default, no change

      Any suggestions?
    • Make PostNav category-specific?
      By caspersjan
      Dear Pagelines team, leafing through the Forums, I found an old solution to a problem I am facing: How to make the PostNav section include posts from the same category only? This is the suggested fix:
      BEST ANSWER The Farside, 14 August 2012 - 04:06 AM
      I seem to have figured it out, but if this is wrong or if there is another way, let me know.
      Here is what I've done in the sections directory for postnav section.php
      <span class="previous"><?php previous_post_link('%link', $link='%title', $in_same_cat = true); ?></span>
                      <span class="next"><?php next_post_link('%link', $link='%title', $in_same_cat = true); ?></span>
      I found the postnav section.php (wp-content/themes/dms/dms/sections/navbar). Do I just add the above code? And would it still be valid in DMS2 (it's ancient - from 2012!) I am hesitant as I am always afraid to break things when dealing with PHP. Or is there another way, a plugin or child theme that lets me add this in a safer way (and one that may withstand upgrades)? 
      Thank you for your help and advice. It is always a pleasure working with Pagelines, every day!  I should say that more often, but of course yet again it is only when there is a problem that I get in touch...
      Yours faithfully, Jan.