- This topic has 14 replies, 2 voices, and was last updated 10 years, 5 months ago by Bartosz.
-
AuthorPosts
-
8 May 2014 at 11:05 #3813David RogersParticipant
Hi All,
I have a series of categories set up that fit the articles that will posted on the site I am working on. What I want to achieve though is to have one particular category separated from the rest of them. For example lets say I have categories A,B,C & D. I want to display categories A,B,C on one page but have category D on a separate page of its own. Is there any way to achieve this idea using this plugin?
Thank You in advance to anyone who answers.
8 May 2014 at 11:34 #3814BartoszKeymasterDefinitely, but we need some more info.
Would you like to display news from A,B,C in news archive page for example? (the default one) And display news from category D on a completely different page? (created in Pages menu)
8 May 2014 at 12:47 #3815David RogersParticipantThank you for your response.
Yeah that sounds very much like what I am aiming to achieve. Category A,B,C on the default page as it is now and category D is separated and only displayed on a new page in the site created from the pages menu.
8 May 2014 at 13:00 #3817BartoszKeymasterOk, so the first part would require you to to alter the default news query and exclude the posts that belong to specific category. Paste that into functions.php of your theme:
// Modify news archive query function df_custom_news_query($query) { if (!is_admin() && is_post_type_archive('news') && $query->is_main_query() && empty($query->query_vars['suppress_filters'])) { $query->set('category__not_in', array(4)); // change 4 to the ID of category you want to exclude (category D) } return $query; } add_filter('pre_get_posts', 'df_custom_news_query');
Please use that and see if you have this working. Then we’ll talk about custom query in a page that includes only selected category.
13 May 2014 at 17:49 #3854David RogersParticipantHi,
Sorry for late response been busy on other projects. Where about does this need to be pasted within the funtions.php file?
Thank You
13 May 2014 at 18:46 #3856BartoszKeymasterAnywhere.
14 May 2014 at 11:28 #3857David RogersParticipantHi,
How do I get the ID of the category?
Thank You for your time.
14 May 2014 at 13:56 #3862David RogersParticipantHi,
Thank you for your help. I have now successfully removed the category I wish to separate and it is working. What is the next step?
Thank You
14 May 2014 at 16:59 #3869BartoszKeymasterYou need to create page tamplate and create a custom query for “news” custom post type that would include only posts from certain category (WP Query ‘category__in’ parameter).
Here’s the detailed info about page template in WordPress: http://codex.wordpress.org/Page_Templates
15 May 2014 at 15:08 #3879David RogersParticipantHi,
Thank you for your help with this. I have now got a page template. I’m just stuck with what code to enter to display the news of a single category?
Thank You
15 May 2014 at 15:53 #3880BartoszKeymasterI’m sorry but all the information you need to do this is available in the Codex link I posted above.
15 May 2014 at 17:53 #3883David RogersParticipantHi,
I feel I am on the right track but possibly missing something. I have been playing around with the code in the codex link you posted to me but so far been unsuccessful in displaying the chosen category. It will work fine if I add them as posts but thats not what I am after as the articles are already there in your news manager plugin. Any suggestions on where I might be going wrong?
Thank you for your time and help.
15 May 2014 at 18:21 #3884BartoszKeymasterThis is the slightly modified copy of Example Using Custom Post Types
<?php /** * Template Name: Page of News * * Print posts of a Custom Post Type. */ get_header(); ?> <?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; ?> <div id="container"> <div id="content"> <?php // query parameters $args = array ( 'post_type' => 'news', // for news 'post_status' => 'publish', 'paged' => $paged, 'category__in' => 4 // id of the category ); $temp = $wp_query; // assign ordinal query to temp variable for later use $wp_query = null; $wp_query = new WP_Query($args); if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); echo '<h2>'; the_title(); echo '</h2>'; echo '<div class="entry-content">'; the_content(); echo '</div>'; endwhile; else : echo '<h2>Not Found</h2>'; get_search_form(); endif; $wp_query = $temp; ?> </div><!-- #content --> </div><!-- #container --> <?php get_sidebar(); ?> <?php get_footer(); ?>
19 May 2014 at 16:06 #3906David RogersParticipantHi,
Thank You for your help. I have achieved just what I was after. Thank you again for your time and help, much appreciated.
19 May 2014 at 16:27 #3907BartoszKeymasterYou’re welcome David.
-
AuthorPosts
- You must be logged in to reply to this topic.
How to Get Support
After you register and login to the site, you may post all plugin support questions in the Support Forum.
If you need to provide private info, please create a ticket and then reply to it using Set as private reply option.
Sign Up
Please login or sign up for a free account.Sign Up Now