Skip to content

Instantly share code, notes, and snippets.

@candyappledesign
Created February 7, 2020 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save candyappledesign/ebfb944e284a7a0b39babbb6cf35a81c to your computer and use it in GitHub Desktop.
Save candyappledesign/ebfb944e284a7a0b39babbb6cf35a81c to your computer and use it in GitHub Desktop.
Sidebars for custom post types in GeneratePress
<?php
/**
* GeneratePress child theme functions and definitions.
*
* Add your custom PHP in this file.
* Only edit this file if you have direct access to it on your server (to fix errors if they happen).
*/
function generatepress_child_enqueue_scripts() {
if ( is_rtl() ) {
wp_enqueue_style( 'generatepress-rtl', trailingslashit( get_template_directory_uri() ) . 'rtl.css' );
}
}
add_action( 'wp_enqueue_scripts', 'generatepress_child_enqueue_scripts', 100 );
/**
* Add is_tree function
*/
function is_tree($pid) { // $pid = The ID of the page we're looking for pages underneath
global $post; // load details about this page
if(is_page()&&($post->post_parent==$pid||is_page($pid)))
return true; // we're at the page or at a sub page
else
return false; // we're elsewhere
};
// Show Webinars sidebar for the custom post type tdiwebinars
add_filter( 'generate_sidebar_layout','cpt_tdiwebinars_sidebar_layout' );
function cpt_tdiwebinars_sidebar_layout( $layout )
{
// If we are on a webinars single page, set the sidebar
if ( is_singular( 'tdiwebinars' ) )
return 'webinars_sidebar';
// Or else, set the regular layout
return $layout;
}
// Add Custom Sidebars
function page_widgets_init() {
register_sidebar( array(
'name' => 'Page Sidebar',
'id' => 'page_sidebar',
'before_widget' => '<aside class="widget_text">',
'after_widget' => '</aside>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
) );
register_sidebar( array(
'name' => 'Thermography Sidebar',
'id' => 'thermography_sidebar',
'before_widget' => '<aside class="widget_text">',
'after_widget' => '</aside>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
) );
register_sidebar( array(
'name' => 'Wellness Sidebar',
'id' => 'wellness_sidebar',
'before_widget' => '<aside class="widget_text">',
'after_widget' => '</aside>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
) );
register_sidebar( array(
'name' => 'Patient Resources Sidebar',
'id' => 'patient_resources_sidebar',
'before_widget' => '<aside class="widget_text">',
'after_widget' => '</aside>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
) );
register_sidebar( array(
'name' => 'Webinars Sidebar',
'id' => 'webinars_sidebar',
'before_widget' => '<aside class="widget_text">',
'after_widget' => '</aside>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'page_widgets_init' );
// Add the Custom Post Types
add_action ( 'init', 'create_post_type' ) ;
function create_post_type ( ) {
// Webinars
register_post_type ( 'tdiwebinars',
array (
'labels' => array (
'name' => __( 'TDI Webinars' ),
'singular_name' => __( 'TDI Webinar' ),
'add_new' => _x( 'Add a Webinar', 'tdiwebinar' ),
'all_items' => __( 'All Webinars' ),
'add_new_item' => __( 'Add a Webinar' ),
'edit_item' => __( 'Edit Webinar' ),
'new_item' => __( 'New Webinar' ),
'view_item' => __( 'View this Webinar' ),
'search_items' => __( 'Search Webinars' ),
'not_found' => __( 'No Matching Webinar Found' )
),
'supports' => array (
'title',
'editor',
'excerpt',
'thumbnail',
'custom-fields',
'revisions',
'author',
'custom-fields',
'tags'
),
'rewrite' => array (
'slug' => 'tdiwebinars',
'with_front' => false
),
'show_in_rest' => true,
'label' => 'TDI Webinars',
'description' => 'TDI Webinars',
'public' => true,
'exclude_from_search' => true,
'show_in_nav_menus' => false,
'has_archive' => true,
'menu_icon' => 'dashicons-video-alt2'
)
) ;
// Resources
register_post_type ( 'tdiresources',
array (
'labels' => array (
'name' => __( 'TDI Resources' ),
'singular_name' => __( 'TDI Resource' ),
'add_new' => _x( 'Add a Resource', 'article' ),
'all_items' => __( 'All Resources' ),
'add_new_item' => __( 'Add a Resources' ),
'edit_item' => __( 'Edit Resource' ),
'new_item' => __( 'New Resource' ),
'view_item' => __( 'View this Resource' ),
'search_items' => __( 'Search Resources' ),
'not_found' => __( 'No Matching Resource Found' )
),
'supports' => array (
'title',
'editor',
'excerpt',
'thumbnail',
'custom-fields',
'page-attributes',
'revisions'
),
'rewrite' => array (
'slug' => 'resources',
'with_front' => false
),
'show_in_rest' => true,
'label' => 'TDI Resources',
'description' => 'TDI Resources',
'public' => true,
'exclude_from_search' => true,
'show_in_nav_menus' => false,
'has_archive' => false,
'menu_icon' => 'dashicons-exerpt-view'
)
) ;
// Press Releases
register_post_type ( 'testing',
array (
'labels' => array (
'name' => __( 'Satellite' ),
'singular_name' => __( 'Satellite' ),
'add_new' => _x( 'Add a Satellite Location', 'testing' ),
'all_items' => __( 'Satellite Locations' ),
'add_new_item' => __( 'Add a Satellite Location' ),
'edit_item' => __( 'Edit Satellite Location' ),
'new_item' => __( 'New Satellite Location' ),
'view_item' => __( 'View this Satellite Location' ),
'search_items' => __( 'Search Satellite Locations' ),
'not_found' => __( 'No Matching Satellite Location Found' )
),
'supports' => array (
'title',
'thumbnail',
'custom-fields',
'revisions'
),
'rewrite' => array (
'slug' => 'testing',
'with_front' => false
),
'show_in_rest' => true,
'label' => 'Off-Site Testing Locations',
'description' => 'Off-Site Testing Locations',
'public' => true,
'exclude_from_search' => true,
'show_in_nav_menus' => false,
'has_archive' => true,
'menu_icon' => 'dashicons-location-alt'
)
) ;
// Initialise custom post type
add_action('init', 'codex_custom_init');
}
// Add Taxonomies for Custom Post Types
add_action( 'init', 'create_topics_hierarchical_taxonomy', 0 );
//Create a custom taxonomy for Resources
function create_topics_hierarchical_taxonomy() {
// Add new taxonomy, make it hierarchical like categories
$labels = array(
'name' => _x( 'Services Provided', 'taxonomy general name' ),
'singular_name' => _x( 'Service Provided', 'taxonomy singular name' ),
'search_items' => __( 'Search Services Provided' ),
'all_items' => __( 'All Services Provided' ),
'parent_item' => __( 'Parent Service' ),
'parent_item_colon' => __( 'Parent Service:' ),
'edit_item' => __( 'Edit Service' ),
'update_item' => __( 'Update Service' ),
'add_new_item' => __( 'Add New Service' ),
'new_item_name' => __( 'New Service Name' ),
'menu_name' => __( 'Services Provided' ),
);
register_taxonomy('service',array('tdiresources'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'services' ),
));
}
<?php
/**
* The Sidebar containing the main widget areas.
*
* @package GeneratePress
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
// If the navigation is set in the sidebar, set variable to true.
$navigation_active = ( 'nav-right-sidebar' == generate_get_navigation_location() ) ? true : false;
// If the secondary navigation is set in the sidebar, set variable to true.
if ( function_exists( 'generate_secondary_nav_get_defaults' ) ) {
$secondary_nav = wp_parse_args(
get_option( 'generate_secondary_nav_settings', array() ),
generate_secondary_nav_get_defaults()
);
if ( 'secondary-nav-right-sidebar' == $secondary_nav['secondary_nav_position_setting'] ) {
$navigation_active = true;
}
}
?>
<div id="right-sidebar" itemtype="http://schema.org/WPSideBar" itemscope="itemscope" role="complementary" <?php generate_right_sidebar_class(); ?>>
<div class="inside-right-sidebar">
<?php
/**
* generate_before_right_sidebar_content hook.
*
* @since 0.1
*/
do_action( 'generate_before_right_sidebar_content' );
if ( is_home ( ) || is_single ( ) || is_archive ( ) ) {
dynamic_sidebar( 'sidebar-1' );
}
// If this is the blog index page, show sidebar-1
elseif ( is_page('6495' ) ) {
dynamic_sidebar( 'sidebar-1' );
}
// If this is a Thermography page or child page show the Thermography sidebar
elseif ( is_tree('2509' ) ) {
dynamic_sidebar( 'thermography_sidebar' );
}
// If this is a Thermography Services page or child page show the Thermography sidebar
elseif ( is_tree('849' ) ) {
dynamic_sidebar( 'thermography_sidebar' );
}
// If this is a Wellness page or child page show the Wellness sidebar
elseif ( is_tree('840' ) ) {
dynamic_sidebar( 'wellness_sidebar' );
}
// If this is The POD or POD Testimonials page show the Wellness sidebar
elseif ( is_page( array( 909, 1887 ) ) ) {
dynamic_sidebar( 'wellness_sidebar' );
}
// If this is the Holistic Health Coaching tree show the Wellness sidebar
elseif ( is_tree('846' ) ) {
dynamic_sidebar( 'wellness_sidebar' );
}
// If this is Patient Resource page show the Patient Resources sidebar
elseif ( is_page( array( 973, 2628, 2847, 953, 970, 2847 ) ) ) {
dynamic_sidebar( 'patient_resources_sidebar' );
}
// If this is Webinar page show the Webinars sidebar
elseif ( is_page('2898' ) ) {
dynamic_sidebar( 'webinars_sidebar' );
}
// If this is a webinars page show the Webinars sidebar
elseif ( is_singular('tdiwebinars'))
dynamic_sidebar( 'webinars_sidebar' );
else {
if ( ! dynamic_sidebar( 'page_sidebar' ) ) :
if ( false == $navigation_active ) : ?>
<aside id="search" class="widget widget_search">
<?php get_search_form(); ?>
</aside>
<aside id="archives" class="widget">
<h2 class="widget-title"><?php esc_html_e( 'Archives', 'generatepress' ); ?></h2>
<ul>
<?php wp_get_archives( array( 'type' => 'monthly' ) ); ?>
</ul>
</aside>
<?php endif;
endif;
}
/**
* generate_after_right_sidebar_content hook.
*
* @since 0.1
*/
do_action( 'generate_after_right_sidebar_content' );
?>
</div><!-- .inside-right-sidebar -->
</div><!-- #secondary -->
<?php
/*
Template Name: TDI Webinars
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
get_header(); ?>
<div id="primary" <?php generate_do_element_classes( 'content' ); ?>>
<main id="main" <?php generate_do_element_classes( 'main' ); ?>>
<?php
/**
* generate_before_main_content hook.
*
* @since 0.1
*/
do_action( 'generate_before_main_content' );
while ( have_posts() ) : the_post();
get_template_part( 'content', 'single' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || '0' != get_comments_number() ) :
/**
* generate_before_comments_container hook.
*
* @since 2.1
*/
do_action( 'generate_before_comments_container' );
?>
<div class="comments-area">
<?php comments_template(); ?>
</div>
<?php
endif;
endwhile;
/**
* generate_after_main_content hook.
*
* @since 0.1
*/
do_action( 'generate_after_main_content' );
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
/**
* generate_after_primary_content_area hook.
*
* @since 2.0
*/
do_action( 'generate_after_primary_content_area' );
generate_construct_sidebars();
get_footer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment