1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Hiding category from Blog Home Page

Discussion in 'WordPress' started by pratik, Jul 4, 2013.

  1. #1
    Hi.
    We run a tech blog using WordPress and want to hide posts from certain category on the homepage. My theme developer replied to my email saying...
    ====
    For this you would need to modify the WP_Query function in the file \functions\newstrick-blog-widget.php
    http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
    ====
    I checked the link and found this for my need.
    ====
    $query = new WP_Query( 'cat=-12,-34,-56' );
    ====
    But I do not know where to add this line in my file below.
    PHP:
    1. }
    2.    
    3.     function widget($args,$instance){
    4.         extract($args);  
    5.  
    6.         global $data, $post, $wp_query, $query;
    7.  
    8.         $ct_num_blog_posts = $instance['ct_num_blog_posts'];
    9.         //$categories = $instance['categories'];
    10.         $title = $instance['title'];
    11.         $pagination_type = $instance['pagination_type'];
    12.  
    13.         $show_image = isset($instance['show_image']) ? 'true' : 'false';
    14.         $show_likes = isset($instance['show_likes']) ? 'true' : 'false';
    15.         $show_comments = isset($instance['show_comments']) ? 'true' : 'false';
    16.         $show_views = isset($instance['show_views']) ? 'true' : 'false';
    17.         $show_date = isset($instance['show_date']) ? 'true' : 'false';
    18.         $show_category = isset($instance['show_category']) ? 'true' : 'false';
    19.         $excerpt_lenght = $instance['excerpt_lenght'];
    20.         $video_height = $instance['video_height'];
    21.         $widget_width = $instance['widget_width'];
    22.         $background = $instance['background'];
    23.         $background_title = $instance['background_title'];
    24.         $divider_type = $instance['divider_type'];
    25.  
    26.         // Get number of posts to display from Theme Options
    27.         $blog_num_posts = stripslashes( $data['ct_blog_num_posts'] );
    28.  
    29.         $max = 0;
    30.         $count_posts = wp_count_posts();
    31.         $ct_post_count = $count_posts->publish;
    32.         $max = ceil ($ct_post_count / $blog_num_posts);
    33.  
    34.         /* Before widget (defined by themes). */
    35.         if ( $title ){
    36.             echo "\n<!-- START BLOG WIDGET -->\n";
    37.             echo '<div class="' . $widget_width . '"><div class="widget ct-blog-widget margin-30t box border-1px bottom-shadow clearfix" style="background:' . $background . ';">';
    38.             echo '<div class="widget-title bottom-shadow" style="background:' . $background_title .';"><h2>' . $title . '</h2><div class="arrow-down" style="border-top-color:' . $background_title . ';"></div><!-- .arrow-down --><div class="plus"><span></span></div><!-- .plus --></div><!-- widget-title -->';
    39.         } else {
    40.             echo "\n<!-- START BLOG WIDGET -->\n";
    41.             echo '<div class="' . $widget_width . '"><div class="widget margin-30t box border-1px bottom-shadow clearfix" style="background:' . $background . ';padding-top: 20px;">';
    42.         }
    43.                
    44.         ?>
    45.  
    46.         <?php
    47.  
    48.         if ( get_query_var('paged') ) {
    49.               $paged = get_query_var('paged');
    50.         } elseif ( get_query_var('page') ) {
    51.               $paged = get_query_var('page');
    52.         } else {
    53.               $paged = 1;
    54.         }
    55.  
    56.  
    57.         if ( !function_exists( 'ct_blog_pagination' ) ) {
    58.             function ct_blog_pagination($pages = '', $range = 4)
    59.             {
    60.                 $showitems = ($range * 2)+1;
    61.  
    62.                 global $paged;
    63.                 if(empty($paged)) $paged = 1;
    64.  
    65.                 if($pages == '')
    66.                 {
    67.                     global $wp_query;
    68.                     $pages = $wp_query->max_num_pages;
    69.                     if(!$pages)
    70.                     {
    71.                         $pages = 1;
    72.                     }
    73.                 }
    74.  
    75.                 if(1 != $pages)
    76.                 {
    77.                     echo "<div class=\"pagination clearfix\"><span>Page ".$paged." of ".$pages."</span>";
    78.                     if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
    79.                     if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";
    80.  
    81.                     for ($i=1; $i <= $pages; $i++)
    82.                     {
    83.                         if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
    84.                         {
    85.                             echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
    86.                         }
    87.                     }
    88.  
    89.                     if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";
    90.                     if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
    91.                     echo "</div>\n";
    92.                 }
    93.             }
    94.         }
    95.  
    96.  
    97.         // Check if Load More Button
    98.         if ( $pagination_type == 'load_more' ) :
    99.             wp_enqueue_script(
    100.                 'pbd-alp-load-posts',
    101.                 get_template_directory_uri() . '/js/load-posts.js',
    102.                 array('jquery'),
    103.                 '1.0',
    104.                 true
    105.             );
    106.  
    107.             // Add some parameters for the JS.
    108.             wp_localize_script(
    109.                 'pbd-alp-load-posts',
    110.                 'pbd_alp',
    111.                 array(
    112.                     'startPage' => $paged,
    113.                     'maxPages' => $max,
    114.                     'nextLink' => next_posts($max, false)
    115.                 )
    116.             );
    117.         endif;
    118.  
    119.         $recent_posts = new WP_Query(array(
    120.             'posts_per_page' => $blog_num_posts,
    121.             'paged' => $paged,
    122.             'post_type' => 'post'
    123.             //'cat' => $categories
    Can someone please help.

    Thanks in advance.
    pratik, Jul 4, 2013 IP
  2. (BN)Nolan

    (BN)Nolan Active Member

    Messages:
    40
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    65
    #2
    What file are you showing right there? If you give me the filename I may be able to help you out.
    (BN)Nolan, Jul 4, 2013 IP
  3. (BN)Nolan

    (BN)Nolan Active Member

    Messages:
    40
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    65
    #3
    If youre trying to display posts from only those categories, depending on where you want them, I'd say you need to make a new template page and find the similar code in that page or the and replace it.

    Something similar to this:
    http://wordpress.org/support/topic/show-posts-of-a-certain-category-on-a-page
    Read the last post there. There's a WP_query code near the top of the code that user posted.

    Here's the code I'm referring to.
    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $the_query = new WP_Query('cat=36&paged='.$paged.'&showposts=30&orderby=post_date&order=desc'); while ($the_query->have_posts()) : $the_query->the_post(); $do_not_duplicate = $post->ID; ?>
    (BN)Nolan, Jul 4, 2013 IP