Exclude one of the blog categories to another page
-
Hi,
I have a blog on my website, it consists of a few categories. I’d like one of the categories to be excluded to another page. I want to have posts from this category displayed in a separate place (and only there).
I tried with writing such a line to the theme file:
<?php query_posts( 'cat=ID' ); ?>
Wrote it in page.php (this file is responsible for the page I’d like to have category posts on) right after this line
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
and yes, I see posts from this category, but they are oddly looped dozens of times, one inside another.
I have now two posts in this category and for this moment there is only newer of them displayed and looped inside itself.Could anyone please tell me how to do it correctly?
-
which specific category id # are you trying to exclude?
please provide link to your website.one of the categories to be excluded
review https://codex.www.ads-software.com/Plugin_API/Action_Reference/pre_get_posts
and particular https://codex.www.ads-software.com/Plugin_API/Action_Reference/pre_get_posts#Exclude_categories_on_your_main_pagecategory displayed in a separate place (and only there)
posts of one category would automatically get shown in a category archive: you might be able to add a link to this into your menu, and possibly create a (custom) category template for this category https://codex.www.ads-software.com/Category_Templates
or create a custom page template with a custom query https://codex.www.ads-software.com/Page_Templates
example https://codex.www.ads-software.com/Page_Templates#A_Page_of_Posts@alchymyth, thank you a lot.
I was trying with category templates from codex, but with no success.
Maybe this time ??One more question.
I created a new page of posts based on codex example.
And it doesn’t work good for now: When there is no posts of this category on my blog, the page gives a truly information, that there is nothing to show. But when there are two posts in the category, I can’t see them here. There are template parts as menu or footer, but no wanted posts, just no content.The second, bigger problem is that when used, the Page of Posts is the only working page on my website; the rest of pages are not displayed, instead there is an information: internal server error (500).
What did I do wrong this time?This is my Page of Posts template:
<?php /* Template Name: Page Of Posts */ get_header(); ?> <section id="container" class="<?php echo tempera_get_layout_class(); ?>"> <div id="content" role="main"> <?php cryout_before_content_hook(); ?> <?php /* The loop: the_post retrieves the content * of the new Page you created to list the posts, * e.g., an intro describing the posts shown listed on this Page.. */ if ( have_posts() ) : while ( have_posts() ) : the_post(); // Display content of page get_template_part( 'content', get_post_format() ); wp_reset_postdata(); endwhile; endif; $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array( // Change these category SLUGS to suit your use. 'category_name' => 'testy', 'paged' => $paged ); $list_of_posts = new WP_Query( $args ); ?> <?php if ( $list_of_posts->have_posts() ) : ?> <?php /* The loop */ ?> <?php while ( $list_of_posts->have_posts() ) : $list_of_posts->the_post(); ?> <?php // Display content of posts ?> <?php get_template_part( 'content', get_post_format() ); ?> <?php endwhile; if($tempera_pagination=="Enable") tempera_pagination($the_query->max_num_pages); else tempera_content_nav( 'nav-below' ); else : ?> <article id="post-0" class="post no-results not-found"> <header class="entry-header"> <h1 class="entry-title"><?php _e( 'Nothing Found', 'tempera' ); ?></h1> </header><!-- .entry-header --> <div class="entry-content"> <p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'tempera' ); ?></p> <?php get_search_form(); ?> </div><!-- .entry-content --> </article><!-- #post-0 --> <?php endif; ?> <?php cryout_after_content_hook(); ?> </div><!-- #content --> <?php tempera_get_sidebar(); ?> </section><!-- #container --> <?php get_footer(); ?>
Ok, I figured out the 500 error problem, but I didn’t manage with my Page of Posts.
I changed the template of the page (previosly it was based on a blog template and that was why internal error occured), now it’s based on one column, no sidebar template. It looks like this:
<?php /* Template Name: Page Of Posts */ get_header(); ?> <section id="container" class="one-column"> <div id="content" role="main"> <?php cryout_before_content_hook(); ?> <?php /* The loop: the_post retrieves the content * of the new Page you created to list the posts, * e.g., an intro describing the posts shown listed on this Page.. */ if ( have_posts() ) : while ( have_posts() ) : the_post(); // Display content of page get_template_part( 'content', get_post_format() ); wp_reset_postdata(); endwhile; endif; $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array( // Change these category SLUGS to suit your use. 'category_name' => 'music, videos', 'paged' => $paged ); $list_of_posts = new WP_Query( $args ); ?> <?php if ( $list_of_posts->have_posts() ) : ?> <?php /* The loop */ ?> <?php while ( $list_of_posts->have_posts() ) : $list_of_posts->the_post(); ?> <?php // Display content of posts ?> <?php get_template_part( 'content', get_post_format() ); ?> <?php endwhile; ?> <?php get_template_part( 'content/content', 'page'); ?> </div><!-- #content --> </section><!-- #container --> <?php get_footer(); ?>
And now the page isn’t working at all on this template, I’m given such an information:
Parse error: syntax error, unexpected $end in /wp-content/themes/tempera/templates/template-pageofposts.php on line 50
line 50 is the last line in this template, it ends the same way as in the template I based on.
the parse error is possibly caused by a missing corresponding ‘endif’ for this line:
<?php if ( $list_of_posts->have_posts() ) : ?>
are you looking for posts of the category ‘video’ and ‘music’?
'category_name' => 'music, videos',
if you know the categort ID (example 273), you can alternatively use:
'category_in' => array( 273 ),
https://codex.www.ads-software.com/Class_Reference/WP_Query#Category_Parameters
ok, no errors now!
but still my category posts are not visible.
that’s how my Page of Posts looks now
<?php /* Template Name: Page Of Posts */ get_header(); ?> <section id="container" class="one-column"> <div id="content" role="main"> <?php cryout_before_content_hook(); ?> <?php /* The loop: the_post retrieves the content * of the new Page you created to list the posts, * e.g., an intro describing the posts shown listed on this Page.. */ if ( have_posts() ) : while ( have_posts() ) : the_post(); // Display content of page get_template_part( 'content', get_post_format() ); wp_reset_postdata(); endwhile; endif; $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array( // Change these category SLUGS to suit your use. 'category_in' => array( 11 ), 'paged' => $paged ); $list_of_posts = new WP_Query( $args ); ?> <?php if ( $list_of_posts->have_posts() ) : endif; ?> <?php /* The loop */ ?> <?php while ( $list_of_posts->have_posts() ) : $list_of_posts->the_post(); ?> <?php // Display content of posts ?> <?php get_template_part( 'content', get_post_format() ); ?> <?php endwhile; ?> <?php get_template_part( 'content/content', 'page'); ?> </div><!-- #content --> </section><!-- #container --> <?php get_footer(); ?>
(category id is 11, name ‘testy’)
a better location for the ‘endif’ would be after the ‘endwhile’.
what is the output of your template in the html in teh browser?
make sure to call the content.php template which is in teh /content/ subfolder;
i.e. in your loop, replace:<?php get_template_part( 'content', get_post_format() ); ?>
with:
<?php get_template_part( 'content/content', get_post_format() ); ?>
also, different from some themes, the ‘tempera’ theme has the loop within the /content/content.php template, so with your code you have nested loops which might not get any output.
for testing, try to overwrite this line in your loop:
<?php get_template_part( 'content', get_post_format() ); ?>
simply with:
<?php the_title(); the_content(); ?>
for theme specific suggestions, you might better post your questions in https://www.ads-software.com/support/theme/tempera/
You’re genious, thank you once more.
Only one problem still exists – on the page of posts I have now all the categories, so it’s in fact the same content as on the blog page.
Could you be so nice and tell me one more, the last thing I hope – what to do now, as I want only category ’11’ to be displayed on this page?
My page looks now like that:
<?php /* Template Name: Page Of Posts */ get_header(); ?> <section id="container" class="one-column"> <div id="content" role="main"> <?php cryout_before_content_hook(); ?> <?php /* The loop: the_post retrieves the content * of the new Page you created to list the posts, * e.g., an intro describing the posts shown listed on this Page.. */ if ( have_posts() ) : while ( have_posts() ) : the_post(); // Display content of page get_template_part( 'content', get_post_format() ); wp_reset_postdata(); endwhile; endif; $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array( // Change these category SLUGS to suit your use. 'category_in' => array( 11 ), 'paged' => $paged ); $list_of_posts = new WP_Query( $args ); ?> <?php if ( $list_of_posts->have_posts() ) : endif; ?> <?php /* The loop */ ?> <?php while ( $list_of_posts->have_posts() ) : $list_of_posts->the_post(); ?> <?php // Display content of posts ?> <?php the_title(); the_content(); ?> <?php endwhile; ?> <?php get_template_part( 'content/content', 'page'); ?> </div><!-- #content --> </section><!-- #container --> <?php get_footer(); ?>
Oh, there is one thing left.
I need to restrain the number of posts on this page to 4. (The older would be available the same way as they are on the blog page – by clicking the ‘previous posts’ button).??
I made a typing mistake in one of my earlier replies:category__in
needs two underline characters.so the line should be:
'category__in' => array( 11 ),
https://codex.www.ads-software.com/Class_Reference/WP_Query#Category_Parameters
Brilliant!
only the page title is after the posts ??
only the page title is after the posts
possibly caused by this line (4th line from the end):
<?php get_template_part( 'content/content', 'page'); ?>
move the line to a diiferent location or remove it.
Ok, everything works beautifully ??
Could you tell me the very last thing?
I wonder how to limit the number of posts on the first page of posts to four – I’d like to have four posts on the first and every next “previous page”.Cordially greet.
and thank you very, very much!
I’m still trying to remove posts of category of id 11 (the one excluded to page of posts) from the main blog page.
I’m trying with such a code in functions.php
function exclude_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', '11' ); } } add_action( 'pre_get_posts', 'exclude_category' );
but again with no success.
- The topic ‘Exclude one of the blog categories to another page’ is closed to new replies.