新的Wordpress主题并没有在管理区域显示小部件


New Wordpress theme is not showing widgets in admin area

我正在创建新的Wordpress主题。它可以工作,但不能在管理面板中显示窗口小部件栏。这是我的代码:

<?php get_header(); ?>
<div class="wrapper">
    <!--Navigation start-->
    <div class="navigation_content">
        <nav>
        <ul>
<?php $args = array(
            'depth'       => 0,
            'sort_column' => 'menu_order, post_title',
            'menu_class'  => 'menu',
            'include'     => '',
            'exclude'     => '',
            'echo'        => true,
            'show_home'   => true,
            'link_before' => '',
            'link_after'  => '' );
?>
        <li class=""><?php wp_page_menu( $args ); ?></li>
        </ul>
        </nav>
    </div>
    <!--Navigation start-->
    <!-- body content start-->
    <div class="body_content">

    <?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>
    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <!--end post header-->
    <div class="entry clear">
    <?php if ( function_exists( 'add_theme_support' ) ) the_post_thumbnail(); ?>
    <p><?php the_content(); ?></p>
    <?php //edit_post_link(); ?>
    <?php wp_link_pages(); ?>
    </div><!--end entry-->
     </div><!--end post-->
    <?php endwhile; /* rewind or continue if all posts have been fetched */ ?>
    <?php else : ?>
    <?php endif; ?>
 <?php get_sidebar(); ?>
<?php get_footer(); ?>

这里是我注册小部件的功能文件代码:

function ccp_widgets_init() {
    register_sidebar( array(
        'name'          => __( 'Main Widget Area', 'ccp' ),
        'id'            => 'sidebar-1',
        'description'   => __( 'Appears in the footer section of the site.', 'ccp' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget'  => '</aside>',
        'before_title'  => '<h3 class="widget-title">',
        'after_title'   => '</h3>',
    ) );

我是不是漏了代码?

感谢

在ccp_widgets_init函数后面的functions.php中添加以下代码。

add_action( 'widgets_init', 'ccp_widgets_init' );

还有另一种方法。

一定要使用儿童主题

在您的儿童主题文件夹中:

  1. 创建一个名为widgets的文件夹
  2. 把你的小工具放在那里

即。

wp-content/themes/my-child-theme/widgets/my_widget.php

在你的小部件的末尾,不需要将它"挂钩"到任何类似的操作中,就像大多数帖子所说的

function register__my_widget() {
register_widget( 'my_widget_class_name_exends_WP_Widget_class' );
}
add_action( 'widgets_init', 'register__my_widget' );

所以只需在末尾用一行将其注册为正常即可:-register_widget('my_widget_class_name_exends_WP_widget_class')请参见以下内容:

    <?php function get_recent_post( $beforewidget, $afterwidget, $beforetitle, $aftertitle ){ ?>
    <?php echo $beforewidget; ?>
    <?php echo $beforetitle; ?>Recent Posts<?php echo $aftertitle; ?>
    <ul class="rp-widget">
        <?php query_posts( array ( 'category_name' => 'blog', 'posts_per_page' => -1 ) );
                while ( have_posts() ) : the_post(); ?>
        <li>....

    ....very boring widget code, yawn...

    ...instance ) {
    // outputs the content of the widget
    get_recent_post( $args['before_widget'], $args['after_widget'], $args['before_title'], $args['after_title'] );
    }
    }
    register_widget( 'my_widget_class_name_exends_WP_Widget_class' );

重要的一位是最后一行——"register_widget"位。

Nb。所有的小部件都扩展了WP_widget类(我认为它是一个类-应该在java/c++中),所以您可以注册类名

3.然后在您的子主题函数.php中添加以下行

get_template_part('widgets/my_widget');

你应该很时髦!

Nb:

  1. 添加到子主题函数.php的行应添加到任何函数之外
  2. 路径应该存在,即小部件/
  3. 它应该是文件名减去.php部分

(使用单独的文件夹并不重要,但它有助于保持代码的有序性,因此,如果您在子主题中添加了一个widgets文件夹,并将my_widget.php放在其中,那么这一点是有效的。

如果你没有将窗口小部件文件夹添加到你的子主题文件夹中,那么你只需要使用

get_template_part('my_widget');

在您的子函数中.php)

这样做有什么好处

  • 您可以将代码分离,使其更加模块化
  • 您最终不会拥有怪物大小的functions.php文件
  • 它应该更容易维护或修改