0

Thanks for taking the time to look at my question. I am building a WP site with custom templates, using Custom Post Types. I have registered a Custom sidebar for a single page, that contains a Custom Post Type loop 'News'. Any news articles that share the same category as the single page will show in the sidebar.

MY PROBLEM: I would like to hide/remove the custom sidebar if there is no News articles available, so that I can make the title and content take up the whole page (full width) instead of just displaying an empty area where there are no News articles in the widget area. Here is my code:

I want to remove widget area so instead of just empty dead space that constrains the main content, I can have a full width layout only on pages that have nothing to show in that widget area Here is my code:

// Register custom sidebar for Single Program Pages only

genesis_register_sidebar(array(
'id' => 'single-program-sidebar',
'name' => 'Single Program Sidebar',
'description' => 'This is the sidebar for single program pages.',
));

// Remove the Primary Sidebar for our Custom Sidebar

add_action('get_header', 'change_genesis_sidebar');
function change_genesis_sidebar() {
if(is_singular('program')) { // change 'post_type' to your CPT slug name
    remove_action('genesis_sidebar', 'genesis_do_sidebar'); //remove the 
default genesis sidebar
    add_action('genesis_sidebar', 'category_news'); //add an action hook to 
call the function for my custom sidebar
}
}

function category_news() {
//This piece of code is what tells WP that we will look for the category for 
the Single post we are currently on, and display in the widget similar posts 
that share that same category.
$category_main = get_the_category();
$cat_slug = $category_main[0]->slug;
// echo $cat_slug; // This is just to see if I got the right output--> 
authors note to himself

//Arguments that you can pass through the widget function. We are displaying 
only the CPT 'News' in our widget
$args = array(
    'post_type' => 'News',
    'posts_per_page' => 3,
    'category_name' => $cat_slug,
);
//Loop through Custom Post Type in there are posts and if we are currently 
on a single Program page. ('program' must be lowercase)
$loop = new WP_Query($args);
if($loop->have_posts() && is_singular('program')):
    while($loop->have_posts()) {
        $loop->the_post();
        //Using current Program post category to filter through what will be 
displayed in Widget
        $category_course = get_the_category();
        $cat_slug_course = $category_course[0]->slug;
        //   echo $cat_slug_course; // This is just to see if I got the 
right output --> authors note to himself
        //What will be displayed physically in the widget for each item

            echo '<div class="entry-content">';
            the_post_thumbnail('thumbnail');
            echo '<br />';
            echo '<h6><a href="' . get_the_permalink() . '">' . 
get_the_title() . '</a></h6>';
            the_content();
            echo '</div>';

    }
endif;
wp_reset_postdata();

} 
  • where are you calling the sidebar in your code? – Michael May 25 '18 at 1:00
  • Thanks for the reply, I just realized when I put the code in this editor I didn't do it right, seems some commented out code is showing as uncommented. I hook the function the genesis_sidebar Sorry, taking the code from a previous developer so trying to figure out myself, but this is my current ticket so thought I'd ask if anyone has done this before. Thanks again! – Esteban Martinez May 25 '18 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.