个性化阅读
专注于IT技术分析

是否可以重新注册未注册的WordPress侧边栏?

我正在自定义Atmosphere Pro主题, 这是一个Genesis子主题。 Atmosphere会在其functions.php文件中注销边栏。

我可以注释掉注销侧边栏的行, 但是, 我试图避免触摸主题本身, 而是使用自定义插件覆盖主题。

我不能只做register_sidebar(‘sidebar’);因为否则Wordpress会将其视为全新的侧边栏。

有没有办法过滤掉unregister_sidebar(‘sidebar’);在functions.php中还是重新注册未注册的侧边栏?

作为参考, 这是Atmosphere functions.php文件中需要重写的代码:

 // Remove sidebars.
 unregister_sidebar( 'sidebar' );
 unregister_sidebar( 'sidebar-alt' );

 // Remove site layouts.
 genesis_unregister_layout( 'content-sidebar' );
 genesis_unregister_layout( 'sidebar-content' );
 genesis_unregister_layout( 'content-sidebar-sidebar' );
 genesis_unregister_layout( 'sidebar-content-sidebar' );
 genesis_unregister_layout( 'sidebar-sidebar-content' );

 // Force full-width-content layout setting.
 add_filter( 'genesis_site_layout', '__genesis_return_full_width_content' );

还有我在插件中的代码:

<?php
/*
   Plugin Name: Atmosphere Pro Theme Sidebars
   Plugin URI: https://my.studiopress.com/themes/atmosphere/
   Version: 1.0
   Author: Rose Fisher
   Description: Enables left or right aligned sidebars in the StudioPress Genesis Atmosphere Pro child theme
   Text Domain: atmosphere-pro-theme-sidebars
   License: GPLv3
  */

defined( 'ABSPATH' ) || die( 'No direct access' );

$theme = wp_get_theme( 'atmosphere-pro' );
if ( $theme->exists() ) { //Check and make sure the theme is set to Atmosphere Pro

    // Add sidebars back - this currently registers a whole new sidebar as sidebar-1
    register_sidebar( 'sidebar' );

    function APTS_get_site_layouts_back() {
     genesis_register_layout( 'content-sidebar', array(
        'label' => __('Content/Sidebar', 'genesis'), 'img' => get_bloginfo('template_url') . '/lib/admin/images/layouts/cs.gif'
    ) );
        genesis_register_layout( 'sidebar-content', array(
        'label' => __('Sidebar/Content', 'genesis'), 'img' => get_bloginfo('template_url') . '/lib/admin/images/layouts/sc.gif'
    ) );
}
add_action( 'init', 'APTS_get_site_layouts_back' );

    // Force full-width-content layout setting only on front page.
    remove_filter( 'genesis_site_layout', '__genesis_return_full_width_content' ); //first remove full width

    function APTS_full_width_frontpage() {
        if ( is_front_page() ){
        add_filter( 'genesis_site_layout', '__genesis_return_full_width_content' ); //then add it back only if it's the front page
        }
    }
    add_action ( 'genesis_meta', 'APTS_full_width_frontpage' );

    // Add back the sidebar CSS after the Atmosphere CSS (based on the Parallax Pro sidebar CSS)
    function APTS_add_sidebar_css() {
            if ( !is_front_page() ) {
                wp_enqueue_style( 'atmosphere-pro-sidebars', get_stylesheet_directory_uri() .'/css/sidebars.css', array('atmosphere-pro') );
            }
    }
    add_action('init', 'APTS_add_sidebar_css', 99);

}

参考:https://victorfont.com/enable-sidebars-in-atmosphere-pro/


#1


问题是register_sidebar(‘sidebar’);一个人是行不通的。边栏的ID必须明确说明。

创世记使用的ID是’sidebar’, 因此代码如下(请注意’id’=>’sidebar’)-

// Restore the sidebar
function APTS_register_sidebar() {
    register_sidebar( array( 'name' => __( 'Sidebar', 'theme-slug' ), 'id' => 'sidebar' ) );
}
add_action( 'wp_loaded', 'APTS_register_sidebar' );

万一它对其他人有用, 这是用于将左右侧边栏还原到”大气儿童”主题的完整代码, 并且可以对其进行更改以对其他”创世纪”子主题和其他侧边栏执行相同的操作。

<?php
/*
Plugin Name: Atmosphere Pro Theme Sidebars
Plugin URI: https://my.studiopress.com/themes/atmosphere/
Version: 1.0
Author: Rose Fisher
Author URI: http://www.bespokehosting.space
Description: Enables left or right aligned sidebars in the StudioPress Genesis Atmosphere Pro theme on the blog archive and blog posts.
Text Domain: atmosphere-pro-theme-sidebars
License: GPLv3

Atmosphere Pro Theme Sidebars is free software: you can redistribute it 
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 2 of the
License, or any later version.

Atmosphere Pro Theme Sidebars is distributed in the hope that it
will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with {Plugin Name}. If not, see {URI to Plugin License}.
*/

defined( 'ABSPATH' ) || die( 'No direct access' );

// Check and make sure the theme is set to Atmosphere Pro
$theme = wp_get_theme( 'atmosphere-pro' );
if ( $theme->exists() ) {
    // Restore the sidebar
    function APTS_register_sidebar() {
        register_sidebar( array( 'name' => __( 'Sidebar', 'theme-slug' ), 'id' => 'sidebar' ) );
    }
    add_action( 'wp_loaded', 'APTS_register_sidebar' );

    // Put the site layout picker back to the theme and post editors
    function APTS_get_site_layouts_back() {
        genesis_register_layout( 'content-sidebar', array(
            'label' => __('Content/Sidebar', 'genesis'), 'img' => get_bloginfo('template_url') . '/lib/admin/images/layouts/cs.gif'
        ) );
        genesis_register_layout( 'sidebar-content', array(
            'label' => __('Sidebar/Content', 'genesis'), 'img' => get_bloginfo('template_url') . '/lib/admin/images/layouts/sc.gif'
        ) );
    }
    add_action( 'init', 'APTS_get_site_layouts_back' );

    // Check if the sidebar is actually being used so we don't unneccessarily create sidebar space
    if ( is_active_sidebar( 'sidebar' ) ) {

        // Remove sidebar from homepage and single pages
        function APTS_remove_sidebar_from_home() {
            if ( is_front_page() || is_page() ) {
                remove_action( 'genesis_after_content', 'genesis_get_sidebar' ); // Remove default Genesis sidebar
            }
        }
        add_action( 'genesis_meta', 'APTS_remove_sidebar_from_home' );

        // Add the CSS for the sidebar (pulled from Parallax Pro) only on the blog archive and posts 
        function APTS_add_sidebar_css() {
            if ( is_single() || is_home() ) {
                wp_enqueue_style( 'atmosphere-pro-sidebars', plugin_dir_url( __FILE__ ) .'css/sidebars.css', array('atmosphere-pro') );
            }
        }
        add_action( 'wp_enqueue_scripts', 'APTS_add_sidebar_css' );

        // Force Genesis full-width-content layout setting only on front page.
        function APTS_full_width_frontpage() {
            remove_filter( 'genesis_site_layout', '__genesis_return_full_width_content' ); //first remove the filter
            if ( is_front_page() ) {
                add_filter( 'genesis_site_layout', '__genesis_return_full_width_content' ); //then add it back only if it's the front page
            }
        }
        add_action ( 'init', 'APTS_full_width_frontpage' );

    }

}
赞(0) 打赏
未经允许不得转载:srcmini » 是否可以重新注册未注册的WordPress侧边栏?
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

微信扫一扫打赏