Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.88 KB | None | 0 0
  1. // initialise widget
  2. function sjc_silo_widget_init() {
  3.     if ( function_exists( 'wp_register_sidebar_widget' ) && function_exists( 'wp_register_widget_control' ) ) {
  4.         wp_register_sidebar_widget( 'sjc_silo_latest_widget2', 'Latest Silo Posts', 'sjc_silo_latest_widget' );
  5.     }
  6. }
  7. add_action( 'plugins_loaded', 'sjc_silo_widget_init' );
  8.  
  9. // create widget
  10. function sjc_silo_latest_widget( $args ) {
  11.     $sidebar_category = get_the_category();
  12.     // if we have a category and we're on a normal post / page / cpt page call the function
  13.     if ( $sidebar_category[0] && ! is_front_page() && is_single() ) {
  14.         extract( $args );
  15.         sjc_silo_latest_content( $sidebar_category[0], $before_widget, $after_widget );
  16.     }
  17. }
  18.  
  19. // echo the sidebar - called by widget, can also be called directly
  20. function sjc_silo_latest_content( $this_sidebar_category = null, $before_widget = null, $after_widget = null ) {
  21.     global $post;
  22.     if ( ! empty( $this_sidebar_category ) ) :    
  23.         $posts_in_this_category = get_posts( 'category__in=' . $this_sidebar_category->cat_ID . '&exclude=' . $post->ID );
  24.         if ( count( $posts_in_this_category ) > 0 ) { ?>
  25.             <?php echo $before_widget; ?>
  26.             <h2 class="widget-title">Latest from <?php echo $this_sidebar_category->cat_name; ?></h2>
  27.             <ul style="margin-bottom:10px;">
  28.             <?php foreach ( $posts_in_this_category as $sidebar_value ) : ?>
  29.                 <li><a href="<?php echo get_permalink( $sidebar_value->ID ); ?>" title="<?php echo $sidebar_value->post_title; ?>"><?php echo $sidebar_value->post_title; ?></a></li>
  30.             <?php endforeach; ?>
  31.             </ul>
  32.             <a href="<?php echo get_category_link($this_sidebar_category->term_id ); ?>">More from <?php echo $this_sidebar_category->cat_name; ?></a>
  33.             <?php echo $after_widget; ?>
  34.         <?php
  35.         }
  36.     endif;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement