Skip to content

Instantly share code, notes, and snippets.

@eveevans
Created August 3, 2012 21:17
Show Gist options
  • Save eveevans/3251639 to your computer and use it in GitHub Desktop.
Save eveevans/3251639 to your computer and use it in GitHub Desktop.
WP Widget para mostrar subcategorias y post dada una cat padre
<?php
/**
* Widget Post: Marca Labs - LineaCat
* MLWidgetLineaCat Class
*/
class MLWidgetLineaCat extends WP_Widget {
function MLWidgetLineaCat() {
parent::WP_Widget(false, $name = 'ML Widget Cat', array('description' =>__( 'Muestra post y subcategorias de una categoria padre','')));
}
function widget($args, $instance) {
extract( $args );
$title = apply_filters('widget_title', $instance['title']);
$ecat = apply_filters('widget_title', $instance['ecat']);
?>
<?php echo $before_widget; ?>
<?php if ( $title ) echo $before_title . $title . $after_title; ?>
<?php /* -------------------------------- Front End ------------------------------------ */ ?>
<div class="ml-widget-cat">
<?php
$actual_categoria_id = $ecat;
$categorias = get_categories(array(
'parent' => $actual_categoria_id,
'hide_empty' => 0
));
?>
<ul>
<?php foreach($categorias as $categoria): ?> <?php /* Iterar sobre cada categoria hija */?>
<li class=''>
<h2 class="titulo"><a href="<?php echo get_category_link($categoria->cat_ID); ?>"> <?php echo ($categoria->name); ?> </a> </h2>
<?php
$posts = get_posts(array(
'numberposts' => -1,
'cat' => $categoria->cat_ID
));
?>
<ul class=""> <?php /* Posts de las subcategorias */?>
<?php foreach($posts as $elemento):?>
<li class="">
<a href="<?php echo $elemento->guid ?>"> <?php echo $elemento->post_title; ?> </a>
<?php if(has_post_thumbnail()): ?> <?php /* feature image: */ ?>
<a href="<?php the_permalink() ?>" rel="enlaces"> <?php the_post_thumbnail("thumbnail", array('class' => '')); ?></a>
<?php endif ?>
</li>
<?php endforeach ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php /* -------------------------------- fin de Front End ------------------------------------ */ ?>
<?php echo $after_widget; ?>
<?php
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['ecat'] = strip_tags($new_instance['ecat']);
return $instance;
}
function form($instance) {
$title = isset($instance['title']) ? esc_attr($instance['title']) : "";
$cantidad = empty($instance['cantidad']) ? '1' : esc_attr($instance['cantidad']);
$oby = isset($instance['oby']) ? esc_attr($instance['oby']) : "";
$ecat = isset($instance['ecat']) ? esc_attr($instance['ecat']) : "";
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:','ml-widget-cat'); ?> </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 $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('ecat'); ?>"><?php _e('Category:','ml-widget-cat'); ?> </label>
<?php wp_dropdown_categories(array(
'class' => 'widefat',
'id' => $this->get_field_id('ecat'),
'name' => $this->get_field_name('ecat'),
'hide_empty' => 0,
'hierarchical' => 1,
'selected' => $ecat
)); ?>
</p>
<?php
}
} // class MLWidgetLineaCat
add_action('widgets_init', create_function('', 'return register_widget("MLWidgetLineaCat");'));
$plugin_dir = basename(dirname(__FILE__));
load_plugin_textdomain( 'ml-widget-cat', null, $plugin_dir . '/languages/' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment