2017-04-11 210 views
0

我試圖讓小部件顯示的東西,但不顯示在admin->外觀部分。 下面的代碼我寫在functions.php文件 下面的函數正在工作,除了小部件。 像菜單,圖標,正常工作 我累了,使小部件。 有人幫我 親切的關注。 並提前致謝。
enter image description hereWordPress的小工具不顯示在(AdminPanel)

<?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() { 
 
\t 
 
\t add_theme_support('custom-logo', array(
 
\t \t 'height'  => 100, 
 
\t \t 'width'  => 400, 
 
\t \t 'flex-width' => true, 
 
\t)); 
 

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

 
?>

回答

0

納迪姆

您需要添加register_sidebar你functions.php文件窗口小部件菜單將出現在您的管理面板區域之後。

欲瞭解更多信息,你可以訪問下面提到的鏈接。 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');

我希望它能幫助你。

+0

謝謝小子。它爲我工作。 –

+0

很高興聽到它幫助你。:) –