WordPress Consultant and Genesis Developer

Exclude categories from WordPress search results

Excluding a category (or categories) from the WordPress search results is easy peezy, however, that doesn’t stop most of the snippets I’ve found from doing it wrong.

<?php

add_filter( 'pre_get_posts', 'ja_search_filter' );
/**
* Exclude category 7 from search results.
*
* @since ?.?.?
* @author Jared Atchison
* @link https://gist.github.com/1300302
*
* @param WP_Query $query Existing query object
* @return WP_Query Amended query object
*/
function ja_search_filter( $query ) {

if ( $query->is_search && !is_admin() )
$query->set( 'cat','-7' );

return $query;

}

Taking advantage of the template_include filter

If you are like me, you often want to control what template file WordPress uses. Some of the methods I’ve seen around the internet try to use template_redirect. Others are even worse and employ sticking tons of conditionals inside WordPress template files (eg category.php).

Well I am here to tell you there is a much easier way. It’s fairly unknown and sparsely documented. It’s the template_include filter. [Read more...]

Fetch total custom post type count

I recently was working on my own Genesis CRM (similar to what Bill Erickson has released).

Unlike the other CRMs out there I decided to create a custom post type for my contacts instead of doing a bunch of magic to use posts. In a few places throughout the app I needed to pull the total number of contact entries in the database.

Turns out, it is very easy to do! Just a few lines of code, which you can see below. Make sure and change contact to the name of your post type.

function ja_total_post_count() {
	global $wpdb;
	$numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'contact'");
	if (0 < $numposts) $numposts = number_format($numposts);
	return $numposts;
}
// To use...
echo 'Number of total contacts: ' . ja_total_post_count();

Removing menus in WordPress 3.1+

Removing menus from the dashboard has always been a pain in the ass. It usually involved digging through $menu trying to find what you want to unset.

Until now! Well, almost now. WordPress 3.1 has two new nifty functions: remove_admin_menus() and remove_submenu_page(). Removing menu items is now as easy as…

add_action('admin_menu', 'remove_admin_menus');
function remove_admin_menus(){
	remove_menu_page('edit.php'); // Posts
	remove_menu_page('upload.php'); // Media
	remove_menu_page('link-manager.php'); // Links
	remove_menu_page('edit-comments.php'); // Comments
	remove_menu_page('edit.php?post_type=page'); // Pages
	remove_menu_page('plugins.php'); // Plugins
	remove_menu_page('themes.php'); // Appearance
	remove_menu_page('users.php'); // Users
	remove_menu_page('tools.php'); // Tools
	remove_menu_page('options-general.php'); // Settings
}

Rotating banners in WordPress with jQuery

Important Note:

This post is quite dated as it is from 2009. Everything mentioned below will still work, however, since this post was written there have been several plugins released that are worth considering first. Make sure and check out the list below!

These are the ones I recommend. If these do not fit the bill there are tons to choose from on WordPress.org

Recently I had a project that needed to have a rotating banner on the main page. Most rotating banners out there are connected to feature posts or articles. WooThemes and some of Matt Brett’s projects often use this method.

This wasn’t going to cut it for me. I needed to have rotating banners that were not associated to any posts, could link off site, be updated/maintained by the client, and preferably not use any custom field voodoo. The last thing I wanted to do was use a post category since it would be major over kill.

So I came up with a solution that fit all my requirements above and works great. Figured I would share what I did in case anyone else needs a similar solution.

The rotating banners were accomplished using:

Let’s get started here.

Step 1: setup a link category

When you’re logged into the WP admin panel, click on Links on the left (under Media, above Pages). The Links section will expand and now we need to create a Link Category.

add_link_cat

Add your Banner category

After you have added your Banner category (or whatever you decide to name it) you want to find the ID for that category. There are 2 ways you can do this. First, place you cursor over the category and look in browser status bar.

find_cat

The second option is to use Sivel’s Simply Show IDs plugin which will tell you the ID. Either way, just make sure you note what the ID is for your category since we will need it later.

Step 2: add some banners

Now we will add some links to the category. Click Add New under Links.

add_link

The name and description is for your reference and will not be shown or used. Make sure you place the link in the category you created. Lastly you will need to put the location of the image in the Image Address field. The easiest way is to upload your banners using the WordPress Media manager. If you go that route your image location will be something similar to http://yoursite.com/wp-contents/uploads/11/09/banner.jpg.

Step 4: downloading  and moving jQuery Carousel

Now that we have the banner category created and some links/images in the category we need to get things setup behind the scenes.

First, download jQuery Carousel.

Secondly, unzip the files. You will see a bunch of files, most of which we won’t need.

Open up your theme folder (/wp-contents/themes/yourtheme/) and create a folder inside called js.

Now we need to move jquery.jcarousel.pack.js or jquery.jcarousel.js (look in the /libs/) to this folder. The path should be similar to /wp-contents/themes/yourtheme/js/jquery.jcarousel.pack.js

Note: It is up to you which version of jCarousel you use. I prefer the packed version since it is smaller and we will not need to do any editing.

After the jCarousel javascript file has been moved we need to move over the CSS. Open jquery.jcarousel.css, copy the contents, and paste it into your theme’s style.css.

So to recap, you should have:
/wp-contents/themes/yourtheme/js/jquery.jcarousel.pack.js
/wp-contents/themes/yourtheme/style.css (should contain the CSS from jquery.jcarousel.css)

Step 5: setting up jCarousel and jQuery

Once you have added the necessary jCarousel CSS to your theme’s style.css you will need to edit it to meet the dimensions of your banner.

/* @group jcarousel */

.jcarousel-banners {
 border: 1px solid #cfcfcf;
 margin:0 0 30px 0;
}

.jcarousel-container {
 position: relative;
}

.jcarousel-clip {
 z-index: 2;
 padding: 0;
 margin: 0;
 overflow: hidden;
 position: relative;
}

.jcarousel-list {
 z-index: 1;
 overflow: hidden;
 position: relative;
 top: 0;
 left: 0;
 margin: 0;
 padding: 0;
}

.jcarousel-list li,
.jcarousel-item {
 float: left;
 list-style: none;
 /* We set the width/height explicitly. No width/height causes infinite loops. */
 width: 578px;
 height: 130px;
}

/**
 * The buttons are added dynamically by jCarousel before
* the</pre>
<ul>
<ul>list (inside the</ul>
</ul>
<div>described above) and
 * have the classnames "jcarousel-next" and "jcarousel-prev".
 */
.jcarousel-next {
 display: none;
}
.jcarousel-prev {
 display: none;
}

.jcarousel-banners .jcarousel-list li,
.jcarousel-banners .jcarousel-item {
 position: relative;
 float: left;
 list-style: none;
 width: 578px;
 height: 130px;
}

.jcarousel-banners .jcarousel-container-horizontal {
 width: 578px;
}

.jcarousel-banners .jcarousel-clip-horizontal {
 width: 578px;
 height: 130px;
}

.jcarousel-banners .jcarousel-item {
 width: 578px;
 height: 130px;
}

Above is what the CSS looks like for my website. The size of the banners is 578x130px. Change this to the size of your banners. You might need to do some further tweaking later.

Now the CSS is done and jCarousel is in place we need to tell WordPress to use jQuery and jCarousel when it loads a page.

Open up functions.php inside your theme directory. You may have to create it if your theme does not have one. Now add this:

if (!is_admin()) {
wp_enqueue_script('jquery');
wp_enqueue_script('jcarousel','/wp-content/themes/yourtheme/js/jquery.jcarousel.js',false,false);
}

WordPress includes many scripts out of the box, one of them being jQuery, so there is no need for us to download it. The code snippet tells WordPress – as long as we are not in the admin panel – to load jQuery and the jCarousel script.

Step 6: finishing things up on the front end

Your banners have been created, the files have been moved, and WordPress now knows to use jQuery and jCarousel when a page loads – almost done!

The last thing we need to do is add the code to grab the banners and make them work.

Open up  index.php or home.php (the location you want to place your banners)  in your theme directory and use this snippet:

</pre>
<div id="banners">
<ul>
<ul><!--?php wp_list_bookmarks( 'categorize=0&category=49<&title_li=&before=
	<li-->&after=</ul>
</ul>
<ul>&show_images=1&show_description=0' ); ?></ul>
</div>
<pre>

This snippet will grab links you created earlier and only show the images. Make sure you change category=49 to your category ID.

Lastly, open header.php in your theme directory. Place the code snippet below in between the <head> </head> tags – usually right after you see wp_head();

jQuery(document).ready(function($) {
$('#banners').jcarousel({
scroll: 1,
auto: 4,
wrap: 'last',
animation: 'slow',
buttonNextHTML: null,
buttonPrevHTML: null
});
});

The jQuery that comes with WordPress runs in non conflict mode, which is why do not use the typical document ready call. jCarousel is very configurable, so I suggest you check out all the different options and make sure your settings are set for the functionality you desire.

Step 7: Profit.

That’s it! If you followed all the steps right you should have a rotating banner on your site.

I found this to be a better solution that any of the plugins I found. We didn’t have to do anything crazy, use posts, or write any plugins. Normally I would provide a demo of this in action but I have a feeling the client I did this for would rather not be mentioned.

If you have an questions or problems, feel free to leave a comment.

If you do expereince problems, a few things to double check/tweak:

  • view your source to make sure WP is calling jQuery and jCarousel
  • check to make sure you have the correct category ID
  • modify the CSS if things don’t render correctly