Sharing content on a wordpress page

Creation of a wordpress site that should organize content in a stream - for example, mechanical, electrical, etc. In addition, each page needs sections such as news, events in articles, etc. If you select one page (for example, mechanical), it should have the following sections

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

Other threads will have the same sections as

Is there a plugin to achieve, or is it better for me to build a template page for each vertical and write PHP code? The code for a page with one section is shown.

<?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 } ?> 
+4
source share
2 answers

In my opinion, you can just write a plugin that does this filtering. The mentioned plugin will have some kind of shortcode that will take a parameter (for example, a category) and will return or echo all the records associated with this category.

Register Short Code:

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

Short Code 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(); } } } 

Using

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

In your case, the page may be something like this

 <div> News : <br/> [shortcode_name category='news'] </div> <div> Articles : <br/> [shortcode_name category='articles'] </div> <div> Events : <br/> [shortcode_name category='events'] </div> 
+1
source

I don’t think there is one right way to do this ... it depends on the scenario ... How many messages, how often sections or "main threads" are added / changed, you need other things on the pages, etc.

One fairly simple and easy to maintain solution:

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

2. Change (or add) category.php to repeat the loop three times and display only messages related to each section (another 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 simply assign each message to one (or more) “parents”, fx mechanical , as well as one (and only one) of news , articles or events ...

  • If you go to the mechanical archive page, you will get three sections
  • if you go to news you will get all the news (but also two empty sections, so of course you have to check it out).

Note that this can be done using tags or even a custom taxonomy, if you want, you just need to edit another theme file - see the hierarchy template.

Beware that this doesn’t work well with pagination, so if this is a concern, you will have to deal with it.

+1
source

All Articles