Last active
July 10, 2016 08:25
-
-
Save tatianepires/b1ee485c85489c0e6dae to your computer and use it in GitHub Desktop.
Code for WordPress that adds a widget to select categories to be linked.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Add to WordPress a widget to select the categories to be displayed in a sidebar. | |
* The links are ordered by the number of posts of each category. | |
* Author: Tatiane Pires | |
* Author URI: http://tatianepires.com.br/ | |
* Version: 1.0 | |
*/ | |
/* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 2 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
*/ | |
/* | |
* Widget class | |
*/ | |
class tps_categories_list_widget extends WP_Widget { | |
function __construct() { | |
parent::__construct( | |
'tps_categories_list_widget', | |
'Links de Categorias', | |
array( 'description' => 'Exibe uma lista com os links das categorias selecionadas' ) | |
); | |
} | |
// Creating widget front-end | |
// This is where the action happens | |
public function widget( $args, $instance ) { | |
$title = apply_filters( 'widget_title', $instance['title'] ); | |
$categoriesList = maybe_unserialize($instance['categories']); | |
// before and after widget arguments are defined by themes | |
echo $args['before_widget']; | |
if ( ! empty( $title ) ) echo $args['before_title'] . $title . $args['after_title']; | |
tps_categories_list_html($categoriesList); | |
echo $args['after_widget']; | |
} | |
// Widget Backend | |
public function form( $instance ) { | |
if ( isset( $instance['title'] ) ) { $title = $instance['title']; } | |
else { $title = __( 'New title' ); } | |
$selectedCat = maybe_unserialize( $instance['categories'] ); | |
// All categories | |
$catArgs = array( | |
'type' => 'post', | |
'child_of' => 0, | |
'orderby' => 'name', | |
'order' => 'ASC', | |
'hide_empty' => 1, | |
'hierarchical' => 1, | |
'taxonomy' => 'category', | |
'pad_counts' => false | |
); | |
$categories = get_categories($catArgs); | |
// Widget admin form | |
?> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> | |
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> | |
</p> | |
<p> | |
<label><?php _e( 'Categories:' ); ?></label> | |
<?php | |
echo '<ul class="cat-checklist category-checklist">'; | |
foreach($categories as $category) { | |
if( is_array($selectedCat) ) | |
if( in_array($category->term_id, $selectedCat) ) { $checked = 'checked="true"'; } else { $checked = ''; } | |
echo '<li class="selectit">'; | |
echo '<input type="checkbox" name="'.$this->get_field_name( 'categories' ).'[]" id="cat-'.$category->term_id.'" value="'.$category->term_id.'" '.$checked.' />'; | |
echo '<label for="cat-'.$category->term_id.'">'.$category->name.'</label>'; | |
echo '</li>'; | |
} | |
echo '</ul>'; | |
?> | |
</p> | |
<?php | |
} // function form | |
// Update widget: replace old instance | |
public function update( $new_instance, $old_instance ) { | |
return $new_instance; | |
} // function update | |
} // class tps_categories_list_widget | |
/* | |
* Register and load the widget | |
*/ | |
function tps_categories_list_load_widget() { | |
register_widget( 'tps_categories_list_widget' ); | |
} | |
add_action( 'widgets_init', 'tps_categories_list_load_widget' ); | |
/* | |
* HTML for widget front-end | |
*/ | |
function tps_categories_list_html($categoriesList) { | |
if( is_array($categoriesList) ) { | |
// order by the number of posts in each category | |
$categoriesListString = implode(', ', $categoriesList); | |
$catArgs = array( | |
'orderby' => 'count', | |
'order' => 'DESC', | |
'style' => 'list', | |
'show_count' => 0, | |
'hide_empty' => 1, | |
'use_desc_for_title' => 1, | |
'child_of' => 0, | |
'include' => $categoriesListString, | |
'hierarchical' => 0, | |
'title_li' => '' | |
); | |
echo '<ul>'; | |
wp_list_categories( $catArgs ); | |
echo '</ul>'; | |
} else { | |
return; | |
} | |
} // tps_categories_list_html | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Save tps-widget-select-categories-to-link.php on your template folder and add the following to the file functions.php of your theme:
require_once( get_template_directory() . '/tps-widget-select-categories-to-link.php' );