WordPress.org

Support

Support » Plugins and Hacks » [Resolved] [Plugin: Business Directory Plugin] Category Listing Widget

[Resolved] [Plugin: Business Directory Plugin] Category Listing Widget

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author businessdirectoryplugin

    @businessdirectoryplugin

    Let me see what I can dig up from my developer…

    Thanks! 🙂

    Plugin Author businessdirectoryplugin

    @businessdirectoryplugin

    try wpbdp_the_directory_categories(). That’s the one my developer said to use.

    Thanks mate!

    Here is some code I used to build a very simple widget.

    1) Put this at the bottom of the widgets.php file in the business-directory-pluig folder.

    class WPBDP_CategoriesWidget extends WP_Widget {
    
        public function __construct() {
            parent::__construct(false,
                                _x('Business Directory - Categories', 'widgets', 'WPBDM'),
                                array('description' => _x('Displays a list of the latest categories in the Business Directory.', 'widgets', 'WPBDM')));
        }
    
        public function form($instance) {
            if (isset($instance['title']))
                $title = $instance['title'];
            else
                $title = _x('Directory Categories', 'widgets', 'WPBDM');
    
        }
    
        public function update($new_instance, $old_instance) {
            $new_instance['title'] = strip_tags($new_instance['title']);
            return $new_instance;
        }
    
        public function widget($args, $instance) {
    		$title = ( $instance['title'] ) ? $instance['title'] : 'Directory Categories';
            extract($args);
            //$title = apply_filters( 'widget_title', $instance['title'] );
    
            echo $before_widget;
            if ( ! empty( $title ) )
                echo $before_title . $title . $after_title;
            echo wpbdp_the_directory_categories();
            echo $after_widget;
        }
    
    }

    2) Add registration of widget into wpbusdirman.php around line 788

    register_widget(‘WPBDP_CategoriesWidget’);

    All done, should appear in widgets section. Please note I am very new to programming wordpress and there could be problems, so use at your own discretion.

    Plugin Author businessdirectoryplugin

    @businessdirectoryplugin

    Side note: Any custom changes like this will be lost if you upgrade the core plugin from WordPress.org, so be sure to save them somewhere safe!

    Can you suggest a better way?

    Is there a way to show the listings of each category by their post titles within this?

    Plugin Author businessdirectoryplugin

    @businessdirectoryplugin

    I’m not sure I understand what you mean. Categories don’t have post titles, they have category names. 🙂

    And @wmuckell, I don’t have a better suggestion for adding arbitrary widgets to the plugin right now, we don’t have an extension mechanism for it.

    Sorry for not being clear. I want to list in the widget area something like this:

    CATEGORY NAME
    -Business Name
    -Business Name
    CATEGORY NAME
    -Business Name
    -Business Name

    Right now, the widget code above only lists the Category Title

    Plugin Author businessdirectoryplugin

    @businessdirectoryplugin

    To make that happen, you’d have to hack the output of wpbdp_the_directory_categories(); to query each listing for its category and then output as you wish above. But there’s no set API to do what you’re asking for today with BD.

    Okay, that’s pretty close to what I did, except I made it a separate function in my own plugin. This can be added to the theme function.php file also. This will place a widget in the widgets section to show the category and the associated posts. Someone else may find this useful:

    <?php //function to query BD categories and associated posts
    class WPBDP_ListingsWidget extends WP_Widget {
    	function WPBDP_ListingsWidget()
    	{
    		$widget_ops = array('classname' => 'WPDBP_ListingsWidget', 'description' => 'Display Business Directory Listings Organized by Categories' ); $this->WP_widget('WPDBP_ListingsWidget', 'Business Directory - Category Listings', $widget_ops);
    	}
    	function form($instance)
    	{
    	$instance = wp_parse_args( (array) $instance, array('title' => '') );
    	$title = $instance['title'];
    ?>
    	  <p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
    <?php
      }
    
      function update($new_instance, $old_instance)
      {
    	$instance = $old_instance;
    	$instance['title'] = $new_instance['title'];
    	return $instance;
      }
      function widget($args, $instance)
      {
    	extract ($args, EXTR_SKIP);
    
    	echo $before_widget;
    	$title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
    
    	if (!empty($title))
    		echo $before_title . $title . $after_title;;
    
    		// Widget Output Code
    		$post_type = 'wpbdp-listing';
    		$tax = 'wpbdm-category';
    		$tax_terms = get_terms($tax);
    		if ($tax_terms) {
    			foreach ($tax_terms as $tax_term) {
    			$args = array(
    				'post_type' => $post_type,
    				"$tax" => $tax_term->slug,
    				'post_status' => 'publish',
    			);
    
    		$my_query=null;
    		$my_query = new WP_Query($args);
    
    		if($my_query->have_posts() ) :
    				echo $tax_term->name; ?>
    				<ul class="taxlist">
    				<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
    
    					<li id="post-<?php the_ID(); ?>">
    						<a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    					</li>
    
    				<?php endwhile; // end of loop ?>
    
    				</ul>
    
    			<?php else : ?>
    			<?php endif; // if have_posts()
    			wp_reset_query();
    
    		} // end foreach #tax_terms
    	}
    	echo $after_widget;
    	}
    }
    add_action('widgets_init', create_function('', 'return register_widget("WPBDP_ListingsWidget");') );
    ?>
Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘[Resolved] [Plugin: Business Directory Plugin] Category Listing Widget’ is closed to new replies.