-
AuthorPosts
-
September 18, 2014 at 10:43 am #107267
I am working on a home page layout (Integrity) with the X version of Visual Composer and would like to separate one category to give those posts their own section on the home page. Having a recent posts section with just that category is easy enough, but in my main recent posts listing I would love to exclude those posts so they are not displayed twice.
September 18, 2014 at 12:49 pm #107349Hi there,
Thank you for writing in!
This isn’t a feature offered by X. However it could be possible with custom development. Because this requires a template change, I’d advise that you setup a child theme. This allows you to make code changes that won’t be overwritten when an X update is released. After your child theme is setup, please review how we recommend making template changes in Customization Best Practices.
After that, add the following code in your child theme’s functions.php file:
// Recent Posts - Adding "exclude_category" to recent_posts shortcode // ================================================================================ function x_shortcode_recent_posts2( $atts ) { extract( shortcode_atts( array( 'id' => '', 'class' => '', 'style' => '', 'type' => '', 'count' => '', 'category' => '', 'exclude_category' => '', 'offset' => '', 'orientation' => '', 'no_image' => '', 'fade' => '' ), $atts ) ); $id = ( $id != '' ) ? 'id="' . esc_attr( $id ) . '"' : ''; $class = ( $class != '' ) ? 'x-recent-posts cf ' . esc_attr( $class ) : 'x-recent-posts cf'; $style = ( $style != '' ) ? 'style="' . $style . '"' : ''; $type = ( $type == 'portfolio' ) ? 'x-portfolio' : 'post'; $count = ( $count != '' ) ? $count : 3; $category = ( $category != '' ) ? $category : ''; $exclude_category = ( $exclude_category != '' ) ? $exclude_category : ''; $category_type = ( $type == 'post' ) ? 'category_name' : 'portfolio-category'; $offset = ( $offset != '' ) ? $offset : 0; $orientation = ( $orientation != '' ) ? ' ' . $orientation : ' horizontal'; $no_image = ( $no_image == 'true' ) ? $no_image : ''; $fade = ( $fade == 'true' ) ? $fade : 'false'; $output = "<div {$id} class=\"{$class}{$orientation}\" {$style} data-fade=\"{$fade}\">"; $q = new WP_Query( array( 'orderby' => 'date', 'post_type' => "{$type}", 'posts_per_page' => "{$count}", 'offset' => "{$offset}", 'cat' => "{$exclude_category}", "{$category_type}" => "{$category}" ) ); if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post(); if ( $no_image == 'true' ) { $image_output = ''; $image_output_class = 'no-image'; } else { $image_output = '<div class="x-recent-posts-img">' . get_the_post_thumbnail( get_the_ID(), 'entry-' . x_get_option( 'x_stack', 'integrity' ) . '-cropped', NULL ) . '</div>'; $image_output_class = 'with-image'; } $output .= '<a class="x-recent-post' . $count . ' ' . $image_output_class . '" href="' . get_permalink( get_the_ID() ) . '" title="' . esc_attr( sprintf( __( 'Permalink to: "%s"', '__x__' ), the_title_attribute( 'echo=0' ) ) ) . '">' . '<article id="post-' . get_the_ID() . '" class="' . implode( ' ', get_post_class() ) . '">' . '<div class="entry-wrap">' . $image_output . '<div class="x-recent-posts-content">' . '<h3 class="h-recent-posts">' . get_the_title() . '</h3>' . '<span class="x-recent-posts-date">' . get_the_date() . '</span>' . '</div>' . '</div>' . '</article>' . '</a>'; endwhile; endif; wp_reset_postdata(); $output .= '</div>'; return $output; } add_filter('init', function() { remove_shortcode( 'recent_posts' ); add_shortcode( 'recent_posts', 'x_shortcode_recent_posts2' ); });
After that you need to add a new attribute to your recent posts shortcode, i.e. exclude_category=””. You can exclude multiple categories by adding the ID of the category with a minus “-” sign before it, and separate multiple categories with commas. Example:
// Single Category [recent_posts type="post" count="4" orientation="horizontal" exclude_category="-21"] // Multiple Categories [recent_posts type="post" count="4" orientation="horizontal" exclude_category="-21, -20, -13, -15"]
To find out the “ID” of a category, just go to it’s edit page (see: http://prntscr.com/4o135o) and in the URL find tag_id variable (see: http://prntscr.com/4o13ty)
Hope this helps. 🙂
Thank you.
September 18, 2014 at 9:41 pm #107715When I add that block of code to my child theme’s function.php file, everything breaks. All I see is a white screen both front-end and admin. Removing the code and all returns to well.
September 18, 2014 at 10:34 pm #107735Hi there,
I just checked the code for parse error. And it doesn’t have any error. It could be that you replaced the entire code of your functions.php including the opening
<?php
from the first line. You should only append the given code at the end of your child theme’s functions.phpThanks!
September 19, 2014 at 12:22 am #107769That’s what I did. But I guess I’ll try it again. Fingers crossed.
September 19, 2014 at 12:55 am #107781Hi There,
If still doesn’t work, you can provide us the login access of your site so we can check on your set-up
Thanks
September 20, 2014 at 6:58 pm #108900I am getting a parse error in the code you provided. It is the only thing added to the functions.php provided with the child theme from your site. So the opening tag is fine. Once I save it, the error I get says:
Parse error: syntax error, unexpected T_FUNCTION in /public_html/home/wp-content/themes/x-child-integrity-light/functions.php on line 81
Line 81 is:
add_filter('init', function() {
I have not changed anything else in the code.
September 20, 2014 at 7:09 pm #108902Not sure why it wasn’t parsing function in the add_filter line. I moved the add and remove shortcode lines into their own function, and then put that function name in the add_filter and the error is gone.
function change_shortcode() { remove_shortcode( 'recent_posts' ); add_shortcode( 'recent_posts', 'x_shortcode_recent_posts2' ); } add_filter('init', 'change_shortcode');
Going to continue testing.
September 20, 2014 at 10:30 pm #108971Thanks. Let us know how it goes.
September 21, 2014 at 11:56 am #109193Got it working. Thanks for the help.
September 21, 2014 at 7:20 pm #109323Great to hear! You’re so welcome 🙂
December 10, 2015 at 6:56 pm #701764This reply has been marked as private.December 10, 2015 at 10:53 pm #702061Hi Rukaiya,
Thanks for writing in!
#1: This is a quite old thread so the solution provided above may not work with the current version of theme. However, it seems you want to exclude categories from your blog page instead of recent posts itself. To do that, simply add following code in your child theme‘s functions.php file:
// Exclude Category From Main Blog Page // ============================================================================= function exclude_category_from_blog( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', '-13' ); } } add_filter( 'pre_get_posts', 'exclude_category_from_blog' );
Replace 13 from the code with your category ID. You can refer to our knowledge base article on How to Locate Category IDs.
#2: This is a native feature of our [recent_posts] shortcode that you can display posts from certain category(s). Checkout our demo on [recent_posts] shortcode for more detail: http://theme.co/x/demo/integrity/1/shortcodes/recent-posts/
Hope this helps. 🙂
Thank you!
December 10, 2015 at 11:43 pm #702146Perfect, both worked!!
Just another quick question – I added the recent_posts shortcode on my homepage and noticed a huge white space between the featured image and the title.
I checked the forum and found this code –>
.x-recent-posts .x-recent-posts-img {
padding-bottom: 0;
}This removes the white space, which is great. However, it then deletes the pictures from the portfolio on the homepage (if you scroll down to ‘museboard’). Is there a different code I need to put in to stop the portfolio images from disappearing?
I’ll send my backend details in the next post just in case.
December 10, 2015 at 11:46 pm #702153This reply has been marked as private. -
AuthorPosts