0

I am making a site that will have posts on several subjects, and often in multi-part posts.

I would like a Home page that can display a Categorised Index, so that visitors can easily find the relevant Posts.

I am currently using the [archive] tag, but that creates a full list of posts with no distinction between them.

Is it possible to create a List that will automatically split my posts into Categories e.g.

Posts with Category “Windows”:

  • post 1
  • post 2
  • post 3

Posts with Category “Linux”

  • post 5
  • post 7
  • post 10

Posts with Category “Security”

  • post 2
  • post 4
  • post 6

etc

I would just like the Post Titles, as links, rather than whole posts, or "Preview” posts.

I am using TwentySixteen Theme, but am prepared to change, if a more appropriate one would work better.

The page I need help with: https://thepcwizarduk.com/wp/

1

The following code will first iterate and display all the categories with ID 2, 3 and 4. And the second loop will display all the posts within each of the category.

<?php
$_categories = get_categories( array(
            'orderby' => 'order',
            'order' => 'ASC',
            'include' => array(2,3,4) // put the category IDs here
        ) );

// Loop to display each of the Category
foreach( $_categories as $_category ) :

    $_posts = get_posts( array(
            'posts_per_page' => 6,
            'category__in' => array( $_category->term_id ),
            'ignore_sticky_posts' => 1,
            'post_status' => 'publish'
        ) );

    if( $_posts ) :

        echo '<section id="category-'. intval($_category->term_id) .'" class="category-block">';

            echo '<h1 class="category-title">'. esc_html($_category->name) .'<h1>';

            echo '<div class="category-articles">';

                // Loop to display posts of certain a category
                foreach( $_posts as $post ) : setup_postdata( $post );
                    echo '<article id="post-'. get_the_ID() .'" class="'. join( ' ', get_post_class( '', get_the_ID() ) ) .'">';
                        echo '<a href="'. get_the_permalink() .'" rel="bookmark">'. get_the_title() .'</a>';
                    echo '</article>';
                endforeach; wp_reset_postdata();

            echo '</div>';

            // A link to the Category archive
            echo '<a href="'. get_category_link( $_category->term_id ) .'" title="'. sprintf( esc_attr__( "View all posts in %s" ), $_category->name ) .'"></a>';

        echo '</section>';

    endif;

endforeach;

Templating

Page Template or Shortcode

If you want to stick with the WordPress' default standard, then make a Page Template or a Shortcode with the code above and set a WordPress page as Front Page from Settings » Reading in admin end, and use the Page Template or Shortcode to let the code run on your front page.

Static Front Page

If you want to set a static front page, create a front-page.php in your Child theme, and add the code above in that page to let it work on your front page.

Remember
If you are changing in your theme, DON'T work on TwentySixteen, you will loose your work on the next release. Make a child theme and put your changes there.

Resources

  1. get_categories — WordPress Developer Resources
  2. get_posts — WordPress Developer Resources
  • Minor errors in code: echo '<article id="post-'. get_the_ID() .'" class="'. join( ' ', get_post_class( '', get_the_ID() ) .'">'; //Missing ) should be: get_the_ID() )) . ">'; I presume No closing ?> I built front-page.php with this (corrected) code, and it worked, but with NO formatting. Just a plain page, no headers, sidebar etc. I have tried a shortcode, building a new Page with just [listcat] and it displays a blank page. :( – Black Spike Jan 20 '18 at 15:05
  • Thanks. Fixed the missing bracket. When working with HTML inside shortcode, Object Buffering would be your friend: use ob_start(); before whole HTML block, and return ob_get_clean(); at the end. Example here. – Mayeenul Islam Jan 21 '18 at 17:06
  • I now have the page basically working, using a Page Template. Good enough for now. I shall investigate ob_start();. – Black Spike Jan 21 '18 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.