sectioning content within a wordpress page

Building a wordpress site that needs to organize content by stream – e.g Mechanical, Electrical etc. Also, each page needs sections like News, Articles events etc. If you pick one page (say Mechanical) it has to have the following sections

  • News
  • Articles (category:articles)
  • Events (category:events)

The other streams will have the same sections as well

Read More

Is there a plugin for achieving or would I be better off building a template page for each vertical and writing php code? Shown code for a page with a single section.

  <?php           
  $args = array(
'posts_per_page'   => 1,
'category_name'    => 'news',
'orderby'          => 'date',
'order'            => 'DESC',
'post_type'        => 'post',
'post_status'      => 'publish',
'suppress_filters' => true 
);

$posts_array = get_posts( $args ); 


    $the_query = new WP_Query($args);
  //EXAMPLE NEWS SECTION 
  if ( $the_query->have_posts() ) {
      while ( $the_query->have_posts() ) {
          $the_query->the_post();
      the_content();          
      echo $content;
      }

  } else {
      // no posts found

  }
  ?>

Related posts

2 comments

  1. In my opinion you could just write a plugin which does that filtering.
    Said plugin would have some kind on shortcode that would take a parameter (category for instance) and would return or echo all the posts associated with that category.

    Shortcode registration :

    add_action('init','register_shortcode');
    function register_shortcode(){
      add_shortcode('shortcode_name', 'shortcode_function');
    }
    

    Shortcode function:

    function shortcode_function($attr){
      $attr = shortcode_atts(array('category' => 'default'),$attr); //Setting defaults parameters
      $args = array(
        'category_name'    => $attr['category'],
        'orderby'          => 'date',
        'order'            => 'DESC',
        'post_type'        => 'post',
        'post_status'      => 'publish',
        'suppress_filters' => true 
        );
    
      $the_query = new WP_Query($args);
      if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
          $the_query->the_post();
          //echo other html related stuff
          the_title();
          the_content();          
        }
      }
    }
    

    Usage

    [shortcode_name category='news']
    [shortcode_name] //category defaults at 'default'
    

    In your case a page could be something like that

    <div>
      News : <br/>
      [shortcode_name category='news']
    </div>
    <div>
      Articles : <br/>
      [shortcode_name category='articles']
    </div>
    <div>
      Events : <br/>
      [shortcode_name category='events']
    </div>
    
  2. I don’t think there is one right way to do this… it depends on the scenario… How many posts, how often do sections or “main streams” get added / change, do you need other stuff on the pages etc.

    One pretty simple and easily maintained solution would be to:

    1. Add mechanical, electrical, etc. as categories alongside news, articles and events

    2. Modify (or add) category.php to repeat the loop three times, and only display the posts that belong to each section (other category):

    //Loop your sections
    foreach(array('news', 'articles', 'events') as $category) {
        echo '<h2>' . $category . '</h2>';
        //Loop posts
        while ( have_posts() ) {
            the_post();
            //Check if post is in section (by slug!)
            if ( in_category($category) ) {
                the_title();
                the_content();
            }
        }
        //Start the loop over
        rewind_posts();
    }
    

    Now just assign each post to one (or more) “parents”, fx mechanical, and also to one (and only one) of news, articles or events

    • If you go to the archive page of mechanical you get three sections
    • if you go to news you get all news (but also two empty sections, so you should of course check for that).

    Note that this could be done using tags or even a custom taxonomy if you wanted, you’d just need to edit a different theme file – see the template hierarchy.

    Beware though that this will not work very nicely with pagination, so if that is a concern you will need to deal with that.

Comments are closed.