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

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

News

Working on a new theme called WordCult

Published

on

So I have been really busy, and haven’t been able to put up a new post since I got back from WordCamp Denver.

WordCult Screenshot

WordCult Screenshot

Working on some clients site’s and also a WordPress theme!

I have finished about 80% of the theme which is based off my current theme located on my personal blog site: TheFrosty. TheFrosty is using version 0.1 of the theme, which has many faults and bugs. I have fixed many of them, and probably added a few others.

In version 0.2 I’ve added a new jQuery “featured posts” loader and the option for sticky posts. I have also fixed a lot of CSS errors, it should W3C comply :).

Also in the newer version I have tried to add more to the admin panel, in ways of options.

If you would like to download this theme and test it out before I release it to the community please let me know. I would love to get some feedback or ideas on what you’ve got to say. Just use the contact form or send me a message on Twitter.

Download it:

[download#2#format=1]
[download#1#format=1]
Download Version 0.3

Once you’ve got the theme..

Let me know what you think! Leave you comments and feedback. I am also trying to get a forum up on the site as well.
Thanks!
Frosty

Update for 0.2:

I’ve updated my personal site: TheFrosty to the latest version of WordCult (0.2). I’ve already found some small bugs and CSS fixes that need to be taken care of. Also I don’t think that the Adsense display is working correctly.
If you’ve noticed any issues please contact me or leave a comment.

 

Update for 0.2.1:

The new version, 0.2.1 brings in some integration from Justin Tadlocks Widgets Reloaded plugin. It’s fully integrated into the theme. So you’ll notice some widgets disappear and be replaced by others. If you need to get them back Justin makes a plugin that will “release” the old widgets on your new theme install.

 

Update: 0.3

Get the newest version of WordCult, download Version 0.3 from this page.

Continue Reading

News

WordCamp Denver 2009 Boradcast

Published

on

Well hello to everyone, hope you are going to have a great weekend! I am heading to Denver Colorado tonight for the first WordCamp. I have decided to help out some here a bit, and I will be using my web cam to broadcast live inside the Denver Art Museum. Plus I am video recording with my HDD camera, so that someone can put all the video together in the end and publish it to WordPress TV

Morning Update:

Looks like there is not a good location to broadcast live from the morning session. Will have to wait till the afternoon.

Afternoon Update:

Got in the technical meet late, didn’t get a very good seat against the wall in the corner. So the angle of broadcast would not work so good. Also uStream isn’t picking up my web cam 🙁
I beleive you can view the rest of WordCamp Denver (Blogger Trac) at http://bitwirelive.com

 

Continue Reading

Random Search Terms

Title

Recent Posts: Fully Net Worth . com

Angel Strawbridge Net Worth: How Rich is Angel Strawbridge Actually?

Angel Strawbridge Net Worth: How Rich is Angel Strawbridge Actually?

Angel Strawbridge is a designer, author who appears in the popular TV series called Escape to the Chateau along with her husband Dick Strawbridge where they work on renovating a massive Chateau they had bought in France after moving from England. Angel Strawbridge net worth is under review and is not available at the moment. […]

Alexandra Grant Net Worth: How Rich is Keanu Reeves’ Girlfriend?

Alexandra Grant Net Worth: How Rich is Keanu Reeves’ Girlfriend?

Alexandra Grant is an American art designer who examines language and written texts through painting, drawing, and sculpture. She is recently in the news after she was spotted holding hands with famous actress Keanu Reeves. Alexandra Grant net worth currently is under review Grant was born in 1973, in Fairview Park, Ohio. Her parents divorced […]

Pete Buttigieg Net Worth: 5 Facts You Should Know

Pete Buttigieg Net Worth: 5 Facts You Should Know

Pete Buttigieg is the mayor of South Bend, Indiana. He is also an American war veteran and a former consultant. In the recent days, Buttigieg has been growing his name with him announcing candidacy for the Presidential election in 2020. Then President Obama called him as one of four Democrats who represented the future of […]

Yandy Smith Net Worth

Yandy Smith Net Worth

Yandy Smith Net Worth. The American celeb Yandy Smith has a net value of $15 million. She is a reality TV star and music producer. Yandy started her professional career working in the entertainment industry. She started working as an executive assistant in a company called Violator Management. Some of her clients include high-profile musicians […]

Trending