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' );
Continue Reading
1 Comment

1 Comment

  1. Jeana

    January 31, 2009 at 5:06 pm

    I might want that, put it on my site??

    Jeana´s last blog post..::Paris Hilton and Her BFF::

You must be logged in to post a comment Login

Leave a Reply

News

And the winners who receive a free book are.

Published

on

And the Winner is..

See, I really did pull the winner out of a hat, lol. Well In my post “Get a copy of Blog Blazers” I said that two people would win copies. Well I’ve got a third now, so I know only a few people commented so, the chances are far greater that you could win! On with the show.

The three commentors that have won a copy of  Blog Blazers are:

  1. aminhers
  2. Ralph
  3. Peter
Continue Reading

News

Get a copy of the book Blog Blazers!

Published

on

blog-blazers-book-coverHey everyone, I’ve got two copies of Blog Blazers that I want to give away to you, the readers!

I got some copies from WordCamp Denver. And I would know like to give them to you. Please just leave a comment, and I will pick two people to receive a copy from WPCult. I am going to allow comments up till the 25th.

Have a great day!

 

 

 

Continue Reading

Cult

In case you missed it, ma.tt is all new!

Published

on

You should head over the the newly redesigned site of Matt Mullenweg, the inventor of WordPress! His site is all new for the Spring season, and he has been tounting many of us with quick screen shots at WordCamp Las Vegas & WordCamp Denver. But finally, and I guess a little delayed, the new theme has launched.

I like it! What do you think?

Continue Reading

Random Search Terms

Title

Recent Posts: Fully Net Worth . com

Ceaser Emanuel Net Worth: How Rich is Black Ink Crew Star?

Ceaser Emanuel Net Worth: How Rich is Black Ink Crew Star?

David Emmanuel, who is better known as Ceaser Emmanuel, is the CEO of the tattoo shop franchise called Black Ink Crew. The show also has a reality television series of the same name on VH1 which has operated for a total of 8 seasons so far. As of 2019, Ceaser Emanuel net worth is estimated […]

Bernie Sanders net worth 2020.

Bernie Sanders net worth 2020.

Bernie Sanders net worth Introduction Bernie Sanders is an American politician who, since 2007, has been a junior U.S. senator from Vermont and U.S. One of the wealthiest guys to enter the potential presidential election of 2020 is Senator Bernie Sanders. Because of the intense emphasis of the political bourgeois on the role of ‘millionaires […]

Leah Messer Net Worth: How Rich is the Teem Mom Star Actually?

Leah Messer Net Worth: How Rich is the Teem Mom Star Actually?

Leah Messer is a television personality who is best known for being a part of the cast of the hit reality television show called Teen Mom 2. As of 2019, Leah Messer net worth is estimated to be $80,000. Messer grew up as a country girl from a small town called Elkview, West Virginia. She […]

Deval Patrick net worth.

Deval Patrick net worth.

Deval Patrick net worth Deval Patrick is an American politician who was born in Chicago, Illinois, in 1956. His father was a musician and left the family when Patrick was young. His mother raised him at the Robert Taylor Homes area near Chicago’s southern side. When he was in the 8th grade, Deval was recruited […]

Trending