Wordpress 小部件未显示在(AdminPanel)上

Wordpress Widget is not display on (AdminPanel)

我试图制作用于显示内容的小部件,但未显示在 admin->appearance 部分。 下面的代码我写在 functions.php 文件上 除小部件外,以下功能正常工作。 喜欢菜单,图标,工作正常 我厌倦了制作小部件。 任何人在这里帮助我 亲切的问候。 并提前致谢。

<?php
class jpen_Category_List_Widget extends WP_Widget {
  // php classnames and widget name/description added
  function __construct() {
    $widget_options = array(
      'classname' => 'jpen_category_list_widget',
      'description' => 'Add a nicely formatted list of categories to your sidebar.'
    );
    parent::__construct( 
      'jpen_category_list_widget', 
      'Simple Blog Theme Category List', 
      $widget_options 
    );
  }
  // create the widget output
  function widget( $args, $instance ) {
    
    $title = apply_filters( 'widget_title', $instance[ 'title' ] );
    $categories = get_categories( array(
      'orderby' => 'name',
      'order'   => 'ASC'
      ) );
    $cat_count = 0;
    $cat_col_one = [];
    $cat_col_two = [];
    foreach( $categories as $category ) {
      $cat_count ++;
      $category_link = sprintf( 
          '<li class="list-unstyled"><a href="%1$s" alt="%2$s">%3$s</a></li>',
          esc_url( get_category_link( $category->term_id ) ),
          esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ),
          esc_html( $category->name )
      );
      if ($cat_count % 2 != 0 ) {
        $cat_col_one[] = $category_link;
      } else {
        $cat_col_two[] = $category_link;
      }
    }
    echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title'];
    ?><div class="row">
      <div class="col-lg-6"><?php
        foreach( $cat_col_one as $cat_one ) {
          echo $cat_one;
        } ?>
      </div>

      <div class="col-lg-6"><?php 
        foreach( $cat_col_two as $cat_two ) {
          echo $cat_two;
        } ?>
      </div>

    </div><?php
    echo $args['after_widget'];
  }
  function form( $instance ) { 
    $title = ! empty( $instance['title'] ) ? $instance['title'] : ''; ?>
    <p>
      <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
      <input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $title ); ?>" />
    </p>
    <p>This widget displays all of your post categories as a two-column list (or a one-column list when rendered responsively).</p>
  <?php }
  // Update database with new info
  function update( $new_instance, $old_instance ) { 
    $instance = $old_instance;
    $instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
    return $instance;
  }
}
// register the widget
function jpen_register_widgets() { 
  register_widget( 'jpen_Category_List_Widget' );
}
add_action( 'widgets_init', 'jpen_register_widgets' );
////////////////////
add_theme_support('menu');
function register_newtheme_menu(){
    register_nav_menus(
            array(
                'main-menu'=>_('Main Menu')
            )
    );
    
}

add_action('init','register_newtheme_menu');


function add_menuclass($ulclass) {
   return preg_replace('/<a /', '<a class="navigation-link w-nav-link" ', $ulclass);
}
add_filter('wp_nav_menu','add_menuclass');

function theme_prefix_setup() {
 
 add_theme_support( 'custom-logo', array(
  'height'      => 100,
  'width'       => 400,
  'flex-width' => true,
 ) );

}
add_action( 'after_setup_theme', 'theme_prefix_setup' );

?>

纳迪姆

您需要在您的 functions.php 文件中添加 register_sidebar 只有当小部件菜单出现在您的管理面板区域后。

有关更多信息,您可以访问下面 link。 https://codex.wordpress.org/Function_Reference/register_sidebar

例如,将下面的代码粘贴到您的 functions.php 文件中

function customtheme_widgets_init() {
register_sidebar( array(
    'name'          => __( 'Sidebar', 'customtheme' ),
    'id'            => 'sidebar-1',
    'description'   => __( 'Add widgets here to appear in your sidebar.', 'customtheme' ),
    'before_widget' => '<section id="%1$s" class="widget %2$s">',
    'after_widget'  => '</section>',
    'before_title'  => '<h2 class="widget-title">',
    'after_title'   => '</h2>',
) );

add_action( 'widgets_init', 'customtheme_widgets_init' );

希望对你有所帮助