1

在查看了一些教程后,我创建了一个自定义主题,但在保存/显示小部件时遇到了一些问题。每当我在管理面板中拖放一个小部件时,如果我刷新页面,它就会消失并且什么也没有显示。我已经用谷歌搜索了几个小时,但我并不接近。希望这里有人看到同样的问题

一旦我从管理页面保存并重新加载,这个区域总是显示为空(也没有任何小部件处于活动状态) 在此处输入图像描述

我检查过的东西:

  1. 验证 ID 不在 Caps 中
  2. 验证 < ?php get_sidebar(); ? > 包含在 page.php 中
  3. 禁用所有插件
  4. 将主题切换到 wordpress 默认以确认小部件确实在工作
  5. 启用辅助功能模式(保存后不显示)
  6. 验证最新版本的 Jquery

我已将其包含在我的functions.php中:

// Register Widget
function ecma_widgets_init() {
    register_sidebar( array(
        'name'          => __( 'Main Widget Area', 'ecma' ),
        'id'            => 'custom1',
        'description'   => __( 'Appears in the footer section of the site.', 'ecma' ),
        'before_widget' => '<div>',
        'after_widget'  => '</div>',
        'before_title'  => '<h3 class="blue">',
        'after_title'   => '</h3>',
    ) );

    register_sidebar( array(
        'name'          => __( 'Secondary Widget Area', 'ecma' ),
        'id'            => 'custom2',
        'description'   => __( 'Appears on posts and pages in the sidebar.', 'ecma' ),
        'before_widget' => '<div>',
        'after_widget'  => '</div>',
        'before_title'  => '<h3 class="blue">',
        'after_title'   => '</h3>',
    ) );
}
add_action( 'widgets_init', 'ecma_widgets_init' );

边栏.php

<?php
/**
 * The sidebar containing the secondary widget area, displays on posts and pages.
 *
 * If no active widgets in this sidebar, it will be hidden completely.
 *
 */

if ( is_active_sidebar( 'home_right_sidebar' ) ) : ?>
            <div id="sidebar">
                <?php dynamic_sidebar( 'custom1' ); ?>
            </div><!-- .widget-area -->
<?php endif; ?>         
4

2 回答 2

2

我发现 $control_ops['id_base'] 和 parent::__construct() 的第一个参数应该是相同的。

class MostRecentWidget extends WP_Widget {

    public function __construct() {
        $widget_ops = array(
            'classname' => 'most-recent-widget',
            'description' => 'Displays Most Recent in sidebar'
        );
        $control_ops = array(
            'id_base' => 'most-recent-widget'
        );
        parent::__construct('most-recent-widget', 'Most Recent Widget', $widget_ops, $control_ops);
    }
}
于 2014-04-04T16:36:12.597 回答
0

虽然从头开始编写主题是可能的,但在我看来,使用像下划线这样的入门主题一定更容易。这样一来,大部分基本结构都得到了处理,并且您可以确定任何破坏的都是您在其之上放置的附加代码,从而使故障排除变得更加容易。

于 2013-08-09T16:25:58.280 回答