WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Limit # of Tags in Widget tag cloud (5 posts)

  1. polomasta
    Member
    Posted 6 years ago #

    How can I limit the number of tags that show up when I put the tag cloud widget into my dynamic sidebar?

    I dug through the widgets.php include and didn't seem to find anything there. Not sure where else to look.

    Why isn't this a default option to change in the widget?

    Thanks for the help

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    Why isn't this a default option to change in the widget?

    Because nobodys added options to that widget yet. The widgets are new-ish, and so not complete.

    wp-includes/widgets.php, line 1330:
    wp_tag_cloud();

    Modify it to this:
    wp_tag_cloud(array('number' => 45));

    Replace 45 with whatever you want.

  3. polomasta
    Member
    Posted 6 years ago #

    why is it that you always figure it out after you ask the question?

    Here are my modifications to widgets.php if anyone else would like this functionality. It gives you the ability to set the number of tags you want to include in the tag cloud.

    function wp_widget_tag_cloud($args) {
    	extract($args);
    	$options = get_option('widget_tag_cloud');
    	$title = empty($options['title']) ? __('Tags') : $options['title'];
    	$number = empty($options['number']) ? __('Tags') : $options['number'];
    
    	echo $before_widget;
    	echo $before_title . $title . $after_title;
    	wp_tag_cloud('number='.$number.'');
    	echo $after_widget;
    }
    
    function wp_widget_tag_cloud_control() {
    	$options = $newoptions = get_option('widget_tag_cloud');
    
    	if ( $_POST['tag-cloud-submit'] ) {
    		$newoptions['title'] = strip_tags(stripslashes($_POST['tag-cloud-title']));
    		$newoptions['number'] = $_POST['tag-cloud-number'];
    
    	}
    
    	if ( $options != $newoptions ) {
    		$options = $newoptions;
    		update_option('widget_tag_cloud', $options);
    	}
    
    	$title = attribute_escape( $options['title'] );
    	$number = attribute_escape( $options['number'] )
    ?>
    	<label for="tag-cloud-title">
    	<?php _e('Title:') ?> <input type="text" class="widefat" id="tag-cloud-title" name="tag-cloud-title" value="<?php echo $title ?>" /></label>
    
    	<label for="tag-cloud-number">
    	<?php _e('Number of Tags to Display:') ?> <input type="text" class="widefat" id="tag-cloud-number" name="tag-cloud-number" value="<?php echo $number ?>" /></label>
    
    	<input type="hidden" name="tag-cloud-submit" id="tag-cloud-submit" value="1" />
  4. gtiphane
    Member
    Posted 5 years ago #

    I was wondering if line 5 shouldn't read

    $number = empty($options['number']) ? __('45') : $options['number'];

    instead of

    $number = empty($options['number']) ? __('Tags') : $options['number'];

    I'd like to add options for smallest and largest font sizes because the default is too large for my cloud. Let me know if the above makes sense to you.

  5. gtiphane
    Member
    Posted 5 years ago #

    all right, I took a chance and added smallest and largest as parameters for the tag cloud widget options. It seems to work on my site, but I'm not really a developer (or want to be one?). Here are the functions in widgets.php, I don't know if someone could check the code again and use it.

    function wp_widget_tag_cloud($args) {
    	extract($args);
    	$options = get_option('widget_tag_cloud');
    	$title = empty($options['title']) ? __('Tags') : $options['title'];
    	$smallest = empty($options['smallest']) ? __('8') : $options['smallest'];
    	$largest = empty($options['largest']) ? __('22') : $options['largest'];
    	$number = empty($options['number']) ? __('45') : $options['number'];
    
    	echo $before_widget;
    	echo $before_title . $title . $after_title;
    	wp_tag_cloud('smallest='.$smallest.'&largest='.$largest.'&number='.$number.'');
    	echo $after_widget;
    }
    
    function wp_widget_tag_cloud_control() {
    	$options = $newoptions = get_option('widget_tag_cloud');
    
    	if ( $_POST['tag-cloud-submit'] ) {
    		$newoptions['title'] = strip_tags(stripslashes($_POST['tag-cloud-title']));
    		$newoptions['smallest'] = $_POST['tag-cloud-smallest'];
    		$newoptions['largest'] = $_POST['tag-cloud-largest'];
    		$newoptions['number'] = $_POST['tag-cloud-number'];
    
    	}
    
    	if ( $options != $newoptions ) {
    		$options = $newoptions;
    		update_option('widget_tag_cloud', $options);
    	}
    
    	$title = attribute_escape( $options['title'] );
    	$smallest = attribute_escape( $options['smallest'] );
    	$largest = attribute_escape( $options['largest'] );
    	$number = attribute_escape( $options['number'] );
    ?>
    	<label for="tag-cloud-title">
    	<?php _e('Title:') ?> <input type="text" class="widefat" id="tag-cloud-title" name="tag-cloud-title" value="<?php echo $title ?>" /></label>
    
    	<label for="tag-cloud-smallest">
    	<?php _e('Smallest Point Size:') ?> <input type="text" class="widefat" id="tag-cloud-smallest" name="tag-cloud-smallest" value="<?php echo $smallest ?>" /></label>
    	<label for="tag-cloud-largest">
    	<?php _e('Largest Point Size:') ?> <input type="text" class="widefat" id="tag-cloud-largest" name="tag-cloud-largest" value="<?php echo $largest ?>" /></label>
    
    	<label for="tag-cloud-number">
    	<?php _e('Number of Tags to Display:') ?> <input type="text" class="widefat" id="tag-cloud-number" name="tag-cloud-number" value="<?php echo $number ?>" /></label>
    
    	<input type="hidden" name="tag-cloud-submit" id="tag-cloud-submit" value="1" />
    <?php
    }

Topic Closed

This topic has been closed to new replies.

About this Topic