我试图使小部件显示某些内容, 但未在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' );
?>
#1
纳德姆
仅在该小部件菜单出现在管理面板区域中之后, 才需要在functions.php文件中添加register_sidebar。
有关更多信息, 你可以访问下面的链接。 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' );
希望对你有帮助。
评论前必须登录!
注册