Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #3813
    David Rogers
    Participant

    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.

    #3814
    Bartosz
    Keymaster

    Definitely, 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)

    #3815
    David Rogers
    Participant

    Thank 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.

    #3817
    Bartosz
    Keymaster

    Ok, 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.

    #3854
    David Rogers
    Participant

    Hi,

    Sorry for late response been busy on other projects. Where about does this need to be pasted within the funtions.php file?

    Thank You

    #3856
    Bartosz
    Keymaster

    Anywhere.

    #3857
    David Rogers
    Participant

    Hi,

    How do I get the ID of the category?

    Thank You for your time.

    #3862
    David Rogers
    Participant

    Hi,

    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

    #3869
    Bartosz
    Keymaster

    You 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

    #3879
    David Rogers
    Participant

    Hi,

    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

    #3880
    Bartosz
    Keymaster

    I’m sorry but all the information you need to do this is available in the Codex link I posted above.

    #3883
    David Rogers
    Participant

    Hi,

    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.

    #3884
    Bartosz
    Keymaster

    This 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(); ?>
    #3906
    David Rogers
    Participant

    Hi,

    Thank You for your help. I have achieved just what I was after. Thank you again for your time and help, much appreciated.

    #3907
    Bartosz
    Keymaster

    You’re welcome David.

Viewing 15 posts - 1 through 15 (of 15 total)
  • You must be logged in to reply to this topic.