Connect with us

News

How to: Limiting the posts in you archive widget

Published

on

I have show you how to create a custom widget in a previous post. But how about adding and additional “Archives” widget that won’t list the last 3 years (if you’ve been around that long) in month form stretching down your whole sidebar?

Well, lets take a look at the original code found in the widgets.php file in the ./wp-includes/ folder.

<?php

/**
* Display archives widget.
*
* @since 2.2.0
*
* @param array $args Widget arguments.
*/
function wp_widget_archives($args) {
extract($args);
$options = get_option('widget_archives');
$c = $options['count'] ? '1' : '0';
$d = $options['dropdown'] ? '1' : '0';
$title = empty($options['title']) ? __('Archives') : apply_filters('widget_title', $options['title']);

echo $before_widget;
echo $before_title . $title . $after_title;

if($d) {
?>
<select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo attribute_escape(__('Select Month')); ?></option> <?php wp_get_archives("type=monthly&format=option&show_post_count=$c"); ?> </select>
<?php
} else {
?>
<ul>
<?php wp_get_archives("type=monthly&show_post_count=$c"); ?>
</ul>
<?php
}

echo $after_widget;
}

/**
* Display and process archives widget options form.
*
* @since 2.2.0
*/
function wp_widget_archives_control() {
$options = $newoptions = get_option('widget_archives');
if ( isset($_POST["archives-submit"]) ) {
$newoptions['count'] = isset($_POST['archives-count']);
$newoptions['dropdown'] = isset($_POST['archives-dropdown']);
$newoptions['title'] = strip_tags(stripslashes($_POST["archives-title"]));
}
if ( $options != $newoptions ) {
$options = $newoptions;
update_option('widget_archives', $options);
}
$count = $options['count'] ? 'checked="checked"' : '';
$dropdown = $options['dropdown'] ? 'checked="checked"' : '';
$title = attribute_escape($options['title']);
?>
<p><label for="archives-title"><?php _e('Title:'); ?> <input class="widefat" id="archives-title" name="archives-title" type="text" value="<?php echo $title; ?>" /></label></p>
<p>
<label for="archives-count"><input class="checkbox" type="checkbox" <?php echo $count; ?> id="archives-count" name="archives-count" /> <?php _e('Show post counts'); ?></label>
<br />
<label for="archives-dropdown"><input class="checkbox" type="checkbox" <?php echo $dropdown; ?> id="archives-dropdown" name="archives-dropdown" /> <?php _e('Display as a drop down'); ?></label>
</p>
<input type="hidden" id="archives-submit" name="archives-submit" value="1" />
<?php } ?>

Simple enough? Or not..

Anyway lets just add on and change a few things. Then will add the final code to your functions.php file.

Okay, starting with the first function: wp_widget_archive and rename to widget_archive_limit

Should look like this now:

function widget_archives_limit($args) {

Then under the this line:

$title = empty($options['title']) ? __('Archives') : apply_filters('widget_title', $options['title']);

Add:

$limit = empty($options['limit']) ? __('Limit') : apply_filters('widget_limit', $options['limit']);

At this line:

<?php wp_get_archives("type=monthly&format=option&show_post_count=$c&limit=$limit"); ?>

We have added in &limit=$limit. The same goes for the second wp_get_archives:

<?php wp_get_archives("type=monthly&show_post_count=$c&limit=$limit"); ?>

At the end of this code add:

wp_register_sidebar_widget('archives limit', __('Archives Limit'), 'widget_archives_limit', $widget_ops);

The whole code should look like this:

function widget_archives_limit($args) {
	extract($args);
	$options = get_option('widget_archives');
	$c = $options['count'] ? '1' : '0';
	$d = $options['dropdown'] ? '1' : '0';
	$title = empty($options['title']) ? __('Archives') : apply_filters('widget_title', $options['title']);
	$limit = empty($options['limit']) ? __('Limit') : apply_filters('widget_limit', $options['limit']);

	echo $before_widget;
	echo $before_title . $title . $after_title;

	if($d) {
?>
		<select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo attribute_escape(__('Select Month')); ?></option> <?php wp_get_archives("type=monthly&format=option&show_post_count=$c&limit=$limit"); ?> </select>
<?php
	} else {
?>
		<ul>
		<?php wp_get_archives("type=monthly&show_post_count=$c&limit=$limit"); ?>
		</ul>
<?php
	}

	echo $after_widget;
}

wp_register_sidebar_widget('archives limit', __('Archives Limit'), 'widget_archives_limit', $widget_ops);

That takes care of the widget its self, now we need to create the controls. Once again find the second function and change wp_widget_archive_control to widget_archives_limit_control

Find this line:

$newoptions['title'] = strip_tags(stripslashes($_POST["archives-title"]));

and add this line below:

$newoptions['limit'] = strip_tags(stripslashes($_POST["archives-limit"]));

Then find this line:

$title = attribute_escape($options['title']);

and add this line below:

$limit = attribute_escape($options['limit']);

Finally look for this line:

<label for="archives-dropdown"><input class="checkbox" type="checkbox" <?php echo $dropdown; ?> id="archives-dropdown" name="archives-dropdown" /> <?php _e('Display as a drop down'); ?></label>
			</p>

and add this line below:

<p><label for="archives-limit"><?php _e('Limit (enter a number):'); ?> <input class="widefat" id="archives-limit" name="archives-limit" type="text" value="<?php echo $limit; ?>" /></label></p>

then register the widget controls:

wp_register_widget_control('archives limit', __('Archives Limit'), 'widget_archives_limit_control' );

The code should look like this:

function widget_archives_limit_control() {
	$options = $newoptions = get_option('widget_archives');
	if ( isset($_POST["archives-submit"]) ) {
		$newoptions['count'] = isset($_POST['archives-count']);
		$newoptions['dropdown'] = isset($_POST['archives-dropdown']);
		$newoptions['title'] = strip_tags(stripslashes($_POST["archives-title"]));
		$newoptions['limit'] = strip_tags(stripslashes($_POST["archives-limit"]));
	}
	if ( $options != $newoptions ) {
		$options = $newoptions;
		update_option('widget_archives', $options);
	}
	$count = $options['count'] ? 'checked="checked"' : '';
	$dropdown = $options['dropdown'] ? 'checked="checked"' : '';
	$title = attribute_escape($options['title']);
	$limit = attribute_escape($options['limit']);
?>
			<p><label for="archives-title"><?php _e('Title:'); ?> <input class="widefat" id="archives-title" name="archives-title" type="text" value="<?php echo $title; ?>" /></label></p>
			<p>
				<label for="archives-count"><input class="checkbox" type="checkbox" <?php echo $count; ?> id="archives-count" name="archives-count" /> <?php _e('Show post counts'); ?></label>
				<br />
				<label for="archives-dropdown"><input class="checkbox" type="checkbox" <?php echo $dropdown; ?> id="archives-dropdown" name="archives-dropdown" /> <?php _e('Display as a drop down'); ?></label>
			</p>
			<p><label for="archives-limit"><?php _e('Limit (enter a number):'); ?> <input class="widefat" id="archives-limit" name="archives-limit" type="text" value="<?php echo $limit; ?>" /></label></p>
			<input type="hidden" id="archives-submit" name="archives-submit" value="1" />
<?php } 

wp_register_widget_control('archives limit', __('Archives Limit'), 'widget_archives_limit_control' );

The final code:

/**
 * Display archives widget.
 *
 * @since 2.2.0
 *
 * @param array $args Widget arguments.
 */
function widget_archives_limit($args) {
	extract($args);
	$options = get_option('widget_archives');
	$c = $options['count'] ? '1' : '0';
	$d = $options['dropdown'] ? '1' : '0';
	$title = empty($options['title']) ? __('Archives') : apply_filters('widget_title', $options['title']);
	$limit = empty($options['limit']) ? __('Limit') : apply_filters('widget_limit', $options['limit']);

	echo $before_widget;
	echo $before_title . $title . $after_title;

	if($d) {
?>
		<select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo attribute_escape(__('Select Month')); ?></option> <?php wp_get_archives("type=monthly&format=option&show_post_count=$c&limit=$limit"); ?> </select>
<?php
	} else {
?>
		<ul>
		<?php wp_get_archives("type=monthly&show_post_count=$c&limit=$limit"); ?>
		</ul>
<?php
	}

	echo $after_widget;
}

wp_register_sidebar_widget('archives limit', __('Archives Limit'), 'widget_archives_limit', $widget_ops);

/**
 * Display and process archives widget options form.
 *
 * @since 2.2.0
 */
function widget_archives_limit_control() {
	$options = $newoptions = get_option('widget_archives');
	if ( isset($_POST["archives-submit"]) ) {
		$newoptions['count'] = isset($_POST['archives-count']);
		$newoptions['dropdown'] = isset($_POST['archives-dropdown']);
		$newoptions['title'] = strip_tags(stripslashes($_POST["archives-title"]));
		$newoptions['limit'] = strip_tags(stripslashes($_POST["archives-limit"]));
	}
	if ( $options != $newoptions ) {
		$options = $newoptions;
		update_option('widget_archives', $options);
	}
	$count = $options['count'] ? 'checked="checked"' : '';
	$dropdown = $options['dropdown'] ? 'checked="checked"' : '';
	$title = attribute_escape($options['title']);
	$limit = attribute_escape($options['limit']);
?>
			<p><label for="archives-title"><?php _e('Title:'); ?> <input class="widefat" id="archives-title" name="archives-title" type="text" value="<?php echo $title; ?>" /></label></p>
			<p>
				<label for="archives-count"><input class="checkbox" type="checkbox" <?php echo $count; ?> id="archives-count" name="archives-count" /> <?php _e('Show post counts'); ?></label>
				<br />
				<label for="archives-dropdown"><input class="checkbox" type="checkbox" <?php echo $dropdown; ?> id="archives-dropdown" name="archives-dropdown" /> <?php _e('Display as a drop down'); ?></label>
			</p>
			<p><label for="archives-limit"><?php _e('Limit (enter a number):'); ?> <input class="widefat" id="archives-limit" name="archives-limit" type="text" value="<?php echo $limit; ?>" /></label></p>
			<input type="hidden" id="archives-submit" name="archives-submit" value="1" />
<?php } 

wp_register_widget_control('archives limit', __('Archives Limit'), 'widget_archives_limit_control' );

News

Welcome

Published

on

Welcome 6

Hello, welcome to a new WordPress community site. Join the ever growing and oh so popular WP Cult. Here we will try to keep you updated with the latest theme’s, plugins, tips & tricks and news.

We will try and showcase the best that WordPress has to offer, as well as the poor and vulnerable states which need your help as cult followers.

Join us, join us today! 🙂

Welcome 7
Welcome 23
Continue Reading

News

Possible photo tagging plugin update

Have you tried to download any of the community tagging plugins with no prevail?

Published

on

I recently posted a comment in response to a post on Justin Tadlock’s WordPress site about custom taxonomies. Then was asked by a reader how I integrated my custom taxonomies with one of Matt Mullenwegs “tagging” plugin.

  1. Community Tags
  2. Matt’s Community Tags

Anyway, I tried to install and use both plugins before with no luck. But decided to download Community Tags and try my luck again. With some hacking, I got it working. For a demo you can check out and tag (someone you know or recognize only, please) a picture over at my photography site, http://thefrosty.com.

Anyway, before I get carried away, I had a reader email me and ask how I integrated the plugin. And I thought I would ask you if you would be interedted in a re-re-release of the plugin.

At present time I do have some array errors, but everything works just fine as is.

Your comment feedback would be great! Thanks!

Continue Reading

News

Think outside the box

Start to think outside the box. UnBox is a new WordPress theme for the willing. Using jQuery in fluidity.

Published

on

Officially released today, this theme is a custom WordPress theme for those who want something different.

Built with loads a jQuery tricks and features. This theme is available for purchase. And dhould be up on ThemeForest as soon as they process the files.

For now, you can check out the working demo, and if you like the theme, use this link to [wp_eStore_buy_now:product_id:2:end].

[wp_eStore:product_id:2:end].

Continue Reading

Random Search Terms

Title

Recent Posts: Fully Net Worth . com

Brandi Redmond Net Worth: How Rich is Brandi Redmond Actually?

Brandi Redmond Net Worth: How Rich is Brandi Redmond Actually?

Brandi Redmond is an American reality television star best known for being a cast member of the show ‘The Real Housewives of Dallas’. As of 2019, Brandi Redmond net worth is estimated to be $4 million. Brandi was born on July 10, 1978, which makes her currently 41 years of age. She was born and […]

Matt Higgins Net Worth: 5 Interesting Facts You Should Know

Matt Higgins Net Worth: 5 Interesting Facts You Should Know

Matt Higgins is a celebrated man, who came from the downsides of life to the upper echelons of the world. An investor, businessman, innovator, top executive, and business advisor. Although he had a hard early life at the beginning, he never let background shackles deter him as he steadily moved his way to success. He’s […]

Dorian Rossini Net Worth: How Rich is Dorian Rossini Actually?

Dorian Rossini Net Worth: How Rich is Dorian Rossini Actually?

Dorian Rossini is a singer and an internet personality. He is best known for being a viral sensation on the internet for the controversy of taking Selfies with him. One moment in an interview would roar the internet on fire as the incident made him famous almost overnight. As of 2019, Dorian Rossini’s net worth […]

9lokkNine Net Worth: How Rich is the Rapper Actually?

9lokkNine Net Worth: How Rich is the Rapper Actually?

Jacquavius Dennard Smith, who is better known by his stage name as 9lokkNine, is an American rapper. He is best known for his hit songs such as “10 Percent” and a collaboration on a song called “223s” with the controversial rapper named YNW Melly which was an even bigger hit. The latter one was able […]

Trending