Skip to content

Instantly share code, notes, and snippets.

@congthien
Created July 6, 2022 04:42
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 congthien/6af8e1e67fc7258e2258e3757ad484bd to your computer and use it in GitHub Desktop.
Save congthien/6af8e1e67fc7258e2258e3757ad484bd to your computer and use it in GitHub Desktop.
fix portfolio sidebar in Construction theme issue
function wpcharming_get_sidebar() {
global $post;
global $woocommerce;
$post_type = get_post_type($post);
$archive_layout_setting = wpcharming_option('archive_layout');
$page_layout_admin = wpcharming_option('page_layout');
$blog_layout_admin = wpcharming_option('blog_layout');
$single_shop_layout = wpcharming_option('single_shop_layout');
$single_project_layout = wpcharming_option('project_layout');
// Single Project
if ( is_single() && $post_type == 'portfolio' ) {
$portfolio_layout_meta = get_post_meta( $post->ID, '_wpc_page_layout', true );
if ( $portfolio_layout_meta == '' || $portfolio_layout_meta == 'sidebar-default' ) {
if ( $single_project_layout == 'left-sidebar' || $single_project_layout == 'right-sidebar' ) {
get_sidebar();
}
} else {
if ( $portfolio_layout_meta == 'right-sidebar' || $portfolio_layout_meta == 'left-sidebar' ) {
get_sidebar();
}
}
return;
}
// Pages
if ( is_singular('page') && ( is_active_sidebar( 'sidebar-2') || is_active_sidebar( 'sidebar-1' ) ) ) {
$page_layout_meta = get_post_meta( $post->ID, '_wpc_page_layout', true );
if ( $page_layout_meta == '' || $page_layout_meta == 'sidebar-default' ) {
if ( $page_layout_admin == '' || $page_layout_admin == 'left-sidebar' || $page_layout_admin == 'right-sidebar' ) {
get_sidebar();
}
} else {
if ( $page_layout_meta == 'right-sidebar' || $page_layout_meta == 'left-sidebar' ) {
get_sidebar();
}
}
}
// Single Post
if ( is_single() && $post_type != 'product' ) {
if ( $blog_layout_admin != 'no-sidebar' && is_active_sidebar( 'sidebar-1' ) ) {
get_sidebar();
}
}
// Archive
if ( ( (is_archive() || is_author()) && $post_type == 'post' ) && !is_front_page() ) {
if ( $archive_layout_setting != 'no-sidebar' && is_active_sidebar( 'sidebar-1' ) ) {
get_sidebar();
}
}
// Search
if ( is_search() ) {
if ( $archive_layout_setting != 'no-sidebar' && is_active_sidebar( 'sidebar-1' ) ) {
get_sidebar();
}
}
// home
if ( is_front_page() ) {
$front_id = (int) get_option( 'page_on_front' );
$page_layout_meta = get_post_meta( $front_id, '_wpc_page_layout', true );
if ( $page_layout_meta == '' || $page_layout_meta == 'sidebar-default' ) {
if ( $page_layout_admin == '' || $page_layout_admin == 'left-sidebar' || $page_layout_admin == 'right-sidebar' ) {
get_sidebar();
}
} else {
if ( $page_layout_meta == 'right-sidebar' || $page_layout_meta == 'left-sidebar' ) {
get_sidebar();
}
}
}
// WooCommerce
if ( $woocommerce ) {
$shop_layout_meta = get_post_meta( wc_get_page_id('shop'), '_wpc_page_layout', true );
if ( is_shop() || is_product_category() || is_product_tag() ) {
if ( $shop_layout_meta == 'right-sidebar' || $shop_layout_meta == 'left-sidebar' ) {
get_sidebar();
}
}
if( is_product() ) {
if ( $single_shop_layout == 'right-sidebar' || $single_shop_layout == 'left-sidebar' ) {
get_sidebar();
} else {
// No Sidebar
}
}
}
}
/**
* Adds custom classes to main content.
*
* @param array $classes Classes for the body element.
* @return array
*/
function wpcharming_get_layout_class ( ) {
global $post;
global $woocommerce;
$post_type = get_post_type($post);
$classes = 'no-sidebar';
$page_layout_admin = wpcharming_option('page_layout');
$archive_layout_admin = wpcharming_option('archive_layout');
$blog_layout_admin = wpcharming_option('blog_layout');
$single_shop_layout = wpcharming_option('single_shop_layout');
$single_project_layout = wpcharming_option('project_layout');
// Single Project
if ( is_singular('portfolio') ) {
// from single project setting
$portfolio_layout_meta = get_post_meta( $post->ID, '_wpc_page_layout', true );
if ( $portfolio_layout_meta == '' || $portfolio_layout_meta == 'sidebar-default' ) {
if ( $single_project_layout == 'left-sidebar' || $single_project_layout == 'right-sidebar' ) {
$classes = $single_project_layout; // from theme option
} else {
$classes = 'no-sidebar';
}
} else {
if ( $portfolio_layout_meta == 'right-sidebar' || $portfolio_layout_meta == 'left-sidebar' ) {
$classes = $portfolio_layout_meta;
}
}
return $classes ;
}
// Pages
if ( is_page() && ( is_active_sidebar( 'sidebar-2' ) || is_active_sidebar( 'sidebar-1' ) ) ){
$page_meta = get_post_meta( $post->ID, '_wpc_page_layout', true );
if ( $page_meta == 'sidebar-default' || $page_meta == '' ) {
$classes = ( $page_layout_admin != '' ) ? $page_layout_admin : 'right-sidebar';
} else if ( $page_meta != '' || $page_meta != 'sidebar-default' ) {
$classes = $page_meta;
} else {
$classes = 'right-sidebar';
}
}
// Single Post
if ( is_single() && is_active_sidebar( 'sidebar-1' ) ) {
if ( $blog_layout_admin ) {
$classes = $blog_layout_admin;
} else {
$classes = 'right-sidebar';
}
}
// Archive
if ( (is_archive() || is_author()) & !is_front_page() && is_active_sidebar( 'sidebar-1' ) ) {
if ( $archive_layout_admin !== '' ){
$classes = $archive_layout_admin;
} else {
$classes = 'right-sidebar';
}
}
// Search
if ( is_search() && is_active_sidebar( 'sidebar-1' ) ) {
if ( $archive_layout_admin !== '' ){
$classes = $archive_layout_admin;
} else {
$classes = 'right-sidebar';
}
}
// Blog Page
if ( !is_front_page() && is_home() && is_active_sidebar( 'sidebar-1' )) {
if ( $blog_layout_admin ) {
$classes = $blog_layout_admin;
} else {
$classes = 'right-sidebar';
}
}
// home page
if ( is_front_page() && is_active_sidebar( 'sidebar-1' ) ) {
$front_id = (int) get_option( 'page_on_front' );
if ( $front_id != 0 ) {
$page_meta = get_post_meta( $front_id, '_wpc_page_layout', true );
if ( $page_meta == 'sidebar-default' || $page_meta == '' ) {
$classes = ( $page_layout_admin != '' ) ? $page_layout_admin : 'right-sidebar';
} else if ( $page_meta != '' || $page_meta != 'sidebar-default' ) {
$classes = $page_meta;
} else {
$classes = 'right-sidebar';
}
} else {
if ( $archive_layout_admin !== '' ){
$classes = $archive_layout_admin;
} else {
$classes = 'right-sidebar';
}
}
}
// WooCommerce
if ( $woocommerce ) {
$shop_layout_meta = get_post_meta( wc_get_page_id('shop'), '_wpc_page_layout', true );
if ( $woocommerce && is_shop() || $woocommerce && is_product_category() || $woocommerce && is_product_tag() ) {
if ( $shop_layout_meta == 'sidebar-default' || $shop_layout_meta == '') {
$classes = ( $page_layout_admin != '' ) ? $page_layout_admin : 'right-sidebar';
} elseif ( $shop_layout_meta != 'sidebar-default' || $shop_layout_meta != '') {
$classes = $shop_layout_meta;
}
else {
$classes = 'no-sidebar';
}
}
if ( $woocommerce && is_product() ) {
if ( $single_shop_layout ) {
$classes = $single_shop_layout;
} else {
$classes = 'no-sidebar';
}
}
}
return $classes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment