我目前在WordPress(5.0.3版)上使用Karma主题(4.10.1版)。
主题具有仅适用于页面发布类型的所有功能(通过自定义元框;如自定义边栏,自定义子菜单,页面模板等)。但是,我希望在帖子类型sfwd课程或sfwd课程上具有相同的功能(特别是自定义边栏)。
父主题的函数将文件multi_sidebars.php require_once(TRUETHEMES_EXTENDED . '/multiple_sidebars.php');
加载到functions.php中。
multiple_sidebars.php包含;
<?php
class sidebar_generator {
function __construct(){
add_action('init',array(&$this,'init'));
add_action('admin_menu',array(&$this,'admin_menu'));
add_action('admin_print_scripts', array(&$this,'admin_print_scripts'));
//edit posts/pages <- removed to create our own with CMB Metaboxes
//add_action('edit_form_advanced', array(&$this, 'edit_form'));
//add_action('edit_page_form', array(&$this, 'edit_form'));
//custom truethemes metabox
add_action( 'add_meta_boxes', array( &$this,'truethemes_add_meta_box') );
//save posts/pages
add_action('edit_post', array(&$this, 'save_form'));
add_action('publish_post', array(&$this, 'save_form'));
add_action('save_post', array(&$this, 'save_form'));
add_action('edit_page_form', array(&$this, 'save_form'));
}
function truethemes_add_meta_box() {
add_meta_box(
'sidebar_generator',
__( 'Custom Sidebar', 'truethemes_localize' ),
array( &$this,'edit_form'),
'page',
'side',
'low'
);
}
function init(){
// Register AJAX hooks
if (current_user_can('manage_options')){
add_action('wp_ajax_add_sidebar', array(&$this,'add_sidebar') );
add_action('wp_ajax_remove_sidebar', array(&$this,'remove_sidebar') );
}
//go through each sidebar and register it
$sidebars = sidebar_generator::get_sidebars();
$tt_i = 15;
if(is_array($sidebars)){
foreach($sidebars as $sidebar){
$sidebar_class = sidebar_generator::name_to_class($sidebar);
register_sidebar(array(
'id' => "sidebar-" . ++$tt_i,
'name'=>$sidebar,
'before_widget' => '<div class="sidebar-widget '.esc_attr($sidebar_class).'">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
));
}
}
}
function admin_print_scripts(){
wp_print_scripts( array( 'sack' ));
?>
<script>
function add_sidebar( sidebar_name )
{
var mysack = new sack("<?php echo esc_url(admin_url('admin-ajax.php')); ?>" );
mysack.execute = 1;
mysack.method = 'POST';
mysack.setVar( "action", "add_sidebar" );
mysack.setVar( "sidebar_name", sidebar_name );
mysack.setVar( "sidebar_generator_nonce", "<?php echo wp_create_nonce( 'add_sidebar' )?>" );
mysack.encVar( "cookie", document.cookie, false );
mysack.onError = function() { alert('Ajax error. Cannot add sidebar' )};
mysack.runAJAX();
return true;
}
function remove_sidebar( sidebar_name,num )
{
var mysack = new sack("<?php echo esc_url(admin_url('admin-ajax.php')); ?>" );
mysack.execute = 1;
mysack.method = 'POST';
mysack.setVar( "action", "remove_sidebar" );
mysack.setVar( "sidebar_name", sidebar_name );
mysack.setVar( "sidebar_generator_nonce", "<?php echo wp_create_nonce( 'remove_sidebar' )?>" );
mysack.setVar( "row_number", num );
mysack.encVar( "cookie", document.cookie, false );
mysack.onError = function() { alert('Ajax error. Cannot add sidebar' )};
mysack.runAJAX();
//alert('hi!:::'+sidebar_name);
return true;
}
</script>
<?php
}
function add_sidebar(){
check_admin_referer( 'add_sidebar', 'sidebar_generator_nonce' );
$sidebars = sidebar_generator::get_sidebars();
$name = sanitize_title($_POST['sidebar_name']);
$id = sidebar_generator::name_to_class($name);
if(isset($sidebars[$id])){
die("alert('Sidebar already exists, please use a different name.')");
}
$sidebars[$id] = $name;
sidebar_generator::update_sidebars($sidebars);
$js = "
var tbl = document.getElementById('sbg_table');
var lastRow = tbl.rows.length;
// if there's no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
// left cell
var cellLeft = row.insertCell(0);
var textNode = document.createTextNode('".esc_js($name)."');
cellLeft.appendChild(textNode);
//middle cell
var cellLeft = row.insertCell(1);
var textNode = document.createTextNode('".esc_js($id)."');
cellLeft.appendChild(textNode);
//var cellLeft = row.insertCell(2);
//var textNode = document.createTextNode('[<a href=\'javascript:void(0);\' onclick=\'return remove_sidebar_link($name);\'>Remove</a>]');
//cellLeft.appendChild(textNode)
var cellLeft = row.insertCell(2);
removeLink = document.createElement('a');
linkText = document.createTextNode('remove');
removeLink.setAttribute('onclick', 'remove_sidebar_link(\'".esc_js($name)."\')');
removeLink.setAttribute('href', '#');
removeLink.appendChild(linkText);
cellLeft.appendChild(removeLink);
";
die( "$js");
}
function remove_sidebar(){
check_admin_referer( 'remove_sidebar', 'sidebar_generator_nonce' );
$sidebars = sidebar_generator::get_sidebars();
$name = sanitize_title($_POST['sidebar_name']);
$id = sidebar_generator::name_to_class($name);
if(!isset($sidebars[$id])){
die("alert('Sidebar does not exist.')");
}
$row_number = (int)$_POST['row_number'];
unset($sidebars[$id]);
sidebar_generator::update_sidebars($sidebars);
$js = "
var tbl = document.getElementById('sbg_table');
tbl.deleteRow($row_number)
";
die($js);
}
function admin_menu(){
add_submenu_page('themes.php', 'Sidebars', 'Sidebars', 'manage_options', 'multiple_sidebars', array(&$this,'admin_page'));
}
function admin_page(){
?>
<script>
function remove_sidebar_link(name,num){
answer = confirm("Are you sure you want to remove " + name + "?\nThis will remove any widgets you have assigned to this sidebar.");
if(answer){
remove_sidebar(name,num);
}else{
return false;
}
}
function add_sidebar_link(){
var sidebar_name = prompt("Sidebar Name:","");
add_sidebar(sidebar_name);
}
</script>
<div class="wrap">
<h2>Sidebar Generator <a href="javascript:void(0);" onclick="return add_sidebar_link()" class="add-new-h2" title="Add a sidebar">Add Sidebar</a></h2>
<p>
The sidebar name is for your use only and will not be visible to your site visitors.
A CSS class is assigned to each sidebar which can later be used for custom styling.
</p>
<br />
<table class="widefat page" id="sbg_table" style="width:600px;">
<tr>
<th>Name</th>
<th>CSS class</th>
<th>Remove</th>
</tr>
<?php
$sidebars = $this->get_sidebars();
if(is_array($sidebars) && !empty($sidebars)){
$cnt=0;
foreach($sidebars as $sidebar){
$alt = ($cnt%2 == 0 ? 'alternate' : '');
?>
<tr class="<?php echo esc_attr($alt)?>">
<td><?php echo esc_html($sidebar); ?></td>
<td><?php echo esc_html(sidebar_generator::name_to_class($sidebar)); ?></td>
<td><a href="javascript:void(0);" onclick="return remove_sidebar_link('<?php echo esc_attr($sidebar); ?>',<?php echo (int)($cnt+1); ?>);" title="Remove this sidebar">remove</a></td>
</tr>
<?php
$cnt++;
}
}else{
?>
<tr>
<td colspan="3">No Sidebars defined</td>
</tr>
<?php
}
?>
</table>
<br />
<div class="add_sidebar">
<a href="javascript:void(0);" onclick="return add_sidebar_link()" title="Add a sidebar">+ Add Sidebar</a>
</div>
</div>
<?php
}
/**
* for saving the pages/post
*/
function save_form($post_id){
$is_saving = '';
if(isset($_POST['sbg_edit']))
$is_saving = $_POST['sbg_edit'];
if(!empty($is_saving)){
delete_post_meta($post_id, 'sbg_selected_sidebar');
delete_post_meta($post_id, 'sbg_selected_sidebar_replacement');
add_post_meta($post_id, 'sbg_selected_sidebar', $_POST['sidebar_generator']);
add_post_meta($post_id, 'sbg_selected_sidebar_replacement', $_POST['sidebar_generator_replacement']);
}
}
function edit_form(){
global $post;
$post_id = $post;
if (is_object($post_id)) {
$post_id = $post_id->ID;
}
$selected_sidebar = get_post_meta($post_id, 'sbg_selected_sidebar', true);
if(!is_array($selected_sidebar)){
$tmp = $selected_sidebar;
$selected_sidebar = array();
$selected_sidebar[0] = $tmp;
}
$selected_sidebar_replacement = get_post_meta($post_id, 'sbg_selected_sidebar_replacement', true);
if(!is_array($selected_sidebar_replacement)){
$tmp = $selected_sidebar_replacement;
$selected_sidebar_replacement = array();
$selected_sidebar_replacement[0] = $tmp;
}
?>
<table class="form-table cmb_metabox"><tbody><tr class="_cmb_sub_menu_title"><td colspan="2"><h5 class="cmb_metabox_title"><?php _e('Sidebar','truethemes_localize'); ?></h5><p class="cmb_metabox_description">
<p class="cmb_metabox_description"><?php _e('Display a custom sidebar by selecting it from the dropdown list below.','truethemes_localize'); ?><br /><br /><strong><?php _e('Please note:','truethemes_localize'); ?></strong> <?php _e('this page must be using a sidebar-ready page template in order to properly display a sidebar.','truethemes_localize'); ?><br /><?php _e('(ie. "Left Sidebar" page template)','truethemes_localize'); ?><br /><br /><strong><?php _e('Need to create a new Sidebar?','truethemes_localize'); ?></strong><br /><a href="<?php echo admin_url('admin.php?page=multiple_sidebars'); ?>"><?php _e('Appearance > Sidebars','truethemes_localize'); ?></a></p>
</td>
</tr>
<tr class="truethemes_custom_sidebar"><td>
<input name="sbg_edit" type="hidden" value="sbg_edit" />
<?php
global $wp_registered_sidebars;
//var_dump($wp_registered_sidebars);
for($i=0;$i<1;$i++){ ?>
<select name="sidebar_generator[<?php echo $i?>]" style="display: none;">
<option value="0"<?php if($selected_sidebar[$i] == ''){ echo " selected";} ?>>WP Default Sidebar</option>
<?php
$sidebars = $wp_registered_sidebars;// sidebar_generator::get_sidebars();
if(is_array($sidebars) && !empty($sidebars)){
foreach($sidebars as $sidebar){
if($selected_sidebar[$i] == $sidebar['name']){
echo "<option value='{$sidebar['name']}' selected>{$sidebar['name']}</option>\n";
}else{
echo "<option value='{$sidebar['name']}'>{$sidebar['name']}</option>\n";
}
}
}
?>
</select>
<select name="sidebar_generator_replacement[<?php echo $i?>]">
<option value="0"<?php if($selected_sidebar_replacement[$i] == ''){ echo " selected";} ?>>— Select Custom Sidebar —</option>
<?php
$sidebar_replacements = $wp_registered_sidebars;//sidebar_generator::get_sidebars();
if(is_array($sidebar_replacements) && !empty($sidebar_replacements)){
foreach($sidebar_replacements as $sidebar){
if($selected_sidebar_replacement[$i] == $sidebar['name']){
echo "<option value='{$sidebar['name']}' selected>{$sidebar['name']}</option>\n";
}else{
echo "<option value='{$sidebar['name']}'>{$sidebar['name']}</option>\n";
}
}
}
?>
</select>
<?php } ?>
</td></tr></tbody></table>
<?php
}
/**
* called by the action get_sidebar. this is what places this into the theme
*/
public static function get_sidebar($name="0"){
if(!is_singular()){
if($name != "0"){
dynamic_sidebar($name);
}else{
dynamic_sidebar();
}
return;//dont do anything
}
global $wp_query;
$post = $wp_query->get_queried_object();
$selected_sidebar = get_post_meta($post->ID, 'sbg_selected_sidebar', true);
$selected_sidebar_replacement = get_post_meta($post->ID, 'sbg_selected_sidebar_replacement', true);
$did_sidebar = false;
//this page uses a generated sidebar
if($selected_sidebar != '' && $selected_sidebar != "0"){
echo "";
if(is_array($selected_sidebar) && !empty($selected_sidebar)){
for($i=0;$i<sizeof($selected_sidebar);$i++){
if($name == "0" && $selected_sidebar[$i] == "0" && $selected_sidebar_replacement[$i] == "0"){
//echo "\n\n<!-- [called $name selected {$selected_sidebar[$i]} replacement {$selected_sidebar_replacement[$i]}] -->";
dynamic_sidebar();//default behavior
$did_sidebar = true;
break;
}elseif($name == "0" && $selected_sidebar[$i] == "0"){
//we are replacing the default sidebar with something
//echo "\n\n<!-- [called $name selected {$selected_sidebar[$i]} replacement {$selected_sidebar_replacement[$i]}] -->";
dynamic_sidebar($selected_sidebar_replacement[$i]);//default behavior
$did_sidebar = true;
break;
}elseif($selected_sidebar[$i] == $name){
//we are replacing this $name
//echo "\n\n<!-- [called $name selected {$selected_sidebar[$i]} replacement {$selected_sidebar_replacement[$i]}] -->";
$did_sidebar = true;
dynamic_sidebar($selected_sidebar_replacement[$i]);//default behavior
break;
}
//echo "<!-- called=$name selected={$selected_sidebar[$i]} replacement={$selected_sidebar_replacement[$i]} -->\n";
}
}
if($did_sidebar == true){
echo "";
return;
}
//go through without finding any replacements, lets just send them what they asked for
if($name != "0"){
dynamic_sidebar($name);
}else{
dynamic_sidebar();
}
echo "";
return;
}else{
if($name != "0"){
dynamic_sidebar($name);
}else{
dynamic_sidebar();
}
}
}
/**
* replaces array of sidebar names
*/
function update_sidebars($sidebar_array){
$sidebars = update_option('sbg_sidebars',$sidebar_array);
}
/**
* gets the generated sidebars
*/
public static function get_sidebars(){
$sidebars = get_option('sbg_sidebars');
return $sidebars;
}
public static function name_to_class($name){
$class = str_replace(array(' ',',','.','"',"'",'/',"\\",'+','=',')','(','*','&','^','%','$','#','@','!','~','`','<','>','?','[',']','{','}','|',':',),'',$name);
return $class;
}
}
$sbg = new sidebar_generator;
function generated_dynamic_sidebar($name='0'){
sidebar_generator::get_sidebar($name);
return true;
}
?>
当我在功能page
中更改sfwd-courses
sfwd-lessons
或truethemes_add_meta_box() {
时,它可以工作,但是,不适用于页面文章类型。另外,当我更新父主题时,代码将被替换。
因此,我想将具有sfwd-courses
或sfwd-lessons
帖子类型(或所需的类)的函数添加到我的functions.php中,但是,当我这样做时,网站会导致空白页。
我在这件事上迷失了。有人可以帮我吗?非常有义务!