Customize WordPress Tag Cloud Widget

Yesterday I was working on developing custom template for one Czech guy. One of the things from the list is to do was customizing the WordPress Tag Cloud Widget for Categories. During this project there was a lot specific request, unique coding and tricks witch I’m gonna explain you here at WPError in next several days.

I was supposed to setup the Tag Cloud Widget to show only 11 categories by count from the categories list. Okay it might looks strange as we are talking about Tag cloud widget and categories in cloud, but let me explain it for you a little. The tag cloud is old WordPress function, but since WordPress 2.8 update it’s upgraded and it’s working with all taxonomies witch are by default tags and categories plus any other custom taxonomies.

[php]
// Parameters accepted by tag cloud widget
// @url http://codex.wordpress.org/Function_Reference/wp_tag_cloud
$args = array(
‘smallest’ => 8,
‘largest’ => 22,
‘unit’ => ‘pt’,
‘number’ => 45,
‘format’ => ‘flat’,
‘separator’ => \\"\n\\",
‘orderby’ => ‘name’,
‘order’ => ‘ASC’,
‘exclude’ => null,
‘include’ => null,
‘topic_count_text_callback’ => default_topic_count_text,
‘link’ => ‘view’,
‘taxonomy’ => ‘post_tag’,
‘echo’ => true,
‘child_of’ => null
);
[/php]

Limit Tag Cloud Widget to 11 items and Order by Count for Categories

Tag Cloud Widget

To achieve the following widget look I was suposed to edit few tag cloud parameters:
1. Customize Font Size of items (smallest and largest)
2. Customize the order
3. Custom taxonomy select
 
 
 
 
 
 

#1 working tag cloud widget code with array

[php]
add_filter(‘widget_tag_cloud_args’,’set_number_tags’);
function set_number_tags($args) {
$args = array(‘smallest’ => 11, ‘largest’ => 11, ‘number’ => 11, ‘orderby’ => ‘count’, ‘taxonomy’ => ‘category’);
return $args;
}
[/php]

#2 working tag cloud widget code without array

[php]
add_filter( ‘widget_tag_cloud_args’, ‘wpe_widget_tag_cloud_args’ );
function wpe_widget_tag_cloud_args( $args ) {
$args[‘smallest’] = 11;
$args[‘largest’] = 11;
$args[‘number’] = 11;
$args[‘orderby’] = ‘count';
$args[‘taxonomy’] = ‘category';
return $args;
}
[/php]

Here bellow I will write and explain you few other WP Tag Cloud snippets witch you can simply use simply by past into function.php file.

Changing the Number of Tags to Display on Widget

[php]
add_filter( ‘widget_tag_cloud_args’, ‘wpe_widget_tag_cloud_args’ );
function wpe_widget_tag_cloud_args( $args ) {
$args[‘number’] = 20;
return $args;
}
[/php]

Changing the Largest and Smallest Font Sizes

Keys that we are going to use are smallest, largest and unit. As we want to display pixels on font size instead of points we have to use unit key aswell.
[php]
add_filter( ‘widget_tag_cloud_args’, ‘wpe_widget_tag_cloud_args’ );
function wpe_widget_tag_cloud_args( $args ) {
$args[‘largest’] = 8;
$args[‘smallest’] = 12;
$args[‘unit’] = ‘px';
return $args;
}
[/php]

Excluding A Tag/Term From The Tag Cloud

Exclude key is comma separated term_id list to exclude. For example ‘exclude’ => 25,36 or $args[‘exclude’] = array(25, 36);. This means that tags with the term_id 25 or 36 will NOT be displayed on the widget.
[/php]
add_filter( ‘widget_tag_cloud_args’, ‘wpe_widget_tag_cloud_args’ );
function wpe_widget_tag_cloud_args( $args ) {
$args[‘exclude’] = array(25, 36);
return $args;
}
[php]

<h3>Including A Tag/Term From The Tag Cloud</h3>
Include key is exact opposite to the exclude key, same comma separated term_id list but this time to include in widget function results. For example ‘include’ => 18, 38, 40 or $args[‘include’] = array(18, 38, 40);. This means that tags with the term_id 18 or 38 or 40 will be the only displayed terms on the widget.
[php]
add_filter( ‘widget_tag_cloud_args’, ‘wpe_widget_tag_cloud_args’ );
function wpe_widget_tag_cloud_args( $args ) {
$args[‘include’] = array(25, 36);
return $args;
}
[/php]

Display only one category on home page

display only one category on home pageWell nowadays I found that there are many people looking for a plugin, snippet or hack to control displaying posts on the home page. This is snippet focused on displaying posts on the homepage from chosen category or categories.

So here is the snippet code or hack, now you just have to paste the code below in your functions.php file. Please be sure that you paste the code under the php tags.
 

 

 

PHP – Display only one category on home page

[php]
function my_category_queries( $query ) {
// only homepage and is the main query
if ($query->is_home() && $query->is_main_query()){
// display only posts in category with slug ‘articles’
$query->set(‘category_name’, ‘articles’);
// avoid sticky posts
$query->set(‘post__not_in’, get_option( ‘sticky_posts’ ) );
}
}<br />
add_action( ‘pre_get_posts’, ‘my_category_queries’ );

[/php]

Conclusion:
As you can see at the beginning of the function we are using the conditional functions is_home() witch is used for The Main Page and is_main_query() Is Main Query witch affect only to the main loop, and the function will proceed if both of conditions are true.
[php]$query->is_home() && $query->is_main_query()[/php]

The first $query-> ensures that all the posts are displayed with the category slug articles, this includes all Sticky Posts with any category, too.
[php]$query->set(‘category_name’, ‘articles’)[/php]

But then with the second $query-> set it to get all the posts with the category slug articles except for the posts that are sticky posts.
[php]$query->set(‘post__not_in’, get_option( ‘sticky_posts’ ) )[/php]

Additional notes:

Category Parameters
[php]
cat (int) – use category id.
category_name (string) – use category slug (NOT name).
category__and (array) – use category id.
category__in (array) – use category id.
category__not_in (array) – use category id.
[/php]

Let’s explain those parameters on our example now, well the first two commands works pretty same but the difference is that on first one we use Category ID’s and second one slugs. So basically if we wish to query the posts from two categories on the homepage we can simply update the code to ‘articles,affiliate-programs’ and function will call posts from the both categories on the homepage loop.

PHP – Display multiple categories on homepage

[php]
function my_category_queries( $query ) {
// only homepage and is the main query
if ($query->is_home() && $query->is_main_query()){
// display only posts in category with slug ‘articles’
// and slug ‘affiliate-programs’
$query->set(‘category_name’, ‘articles,affiliate-programs’);
// avoid sticky posts
$query->set(‘post__not_in’, get_option( ‘sticky_posts’ ) );
}
}

add_action( ‘pre_get_posts’, ‘my_category_queries’ );
[/php]

The example will be with the cat command instead of category_name will look like example bellow where the 25 and 36 are the category id’s.
[php]$query->set(‘cat’, ‘25,36’);[/php]

Different Background on each WordPress Post or Page

You may want to have a different background on your WordPress website. For example, you may have a celebrity blog and want to include an image of that celebrity on each of there pages or you may have a site that covers different topics and want to have a different color to represent each category.

This is easy to do with the plugin called Background Per Page.

Simply (in your WordPress dashboard) go to Plugins > Add New and search for Background Per Page, or download Background Per Page plugin, extract and upload the folder to the /wp-content/plugins/ directory. Then just Activate the plugin trough ‘Plugins’ menu in WordPress.

Download Background Per Page

How to Add Google AdSense into WordPress Part 2

In Part 1 I showed you how to add Google AdSense into your WordPress widgets. In part 2 I will show you how to add Google AdSense directly into your theme templates.

This means once its added it will continually add you code directly into each new page or post. In Part 3 i will show you which AdSense plugins are the best to use.

List of UK Towns, Cities & Counties for WordPress Categories & Database

Lots of people are forever searching the web for a list of UK towns, cities and counties.

Once they find it they are then stuck on how to use this info in there WordPress installation.

I have put together a video guide along with a CSV file for you to download all in the correct format for you to use straight away. it will take you know more than 5 minutes to use.

First off you can download a copy of the UK Towns, Cities and Counties Database Free Here.

You’ll need this free plugin, Go to Plugins > Add New and search for this in your WordPress dashboard WPEssence Bulk Categories

 

How to Add Google AdSense into WordPress Themes Part 1

Over a few tutorials i will show you how to add Google AdSense directly into your theme.
In Part 1 i will show you a really simple way to add your Adsense into a widgeted area. Then in part 2 i will show you how to add it directly into a theme file e.g. so that it adds AdSense directly into every post you make.

This is the YouTube video i have created showing you how to do this, If you find it useful please like it, add a positive comment for me and share it on your social network. All your support is appreciated.

Parse error: syntax error, unexpected T_VARIABLE in wpincludes /functions.php on line 192 WordPress 3.5

There seems to be a lot of people experiencing a problem after upgrading to WordPress 3.5

If you get the following error (or very similar)

Parse error: syntax error, unexpected T_VARIABLE in wpincludes /functions.php on line 192

Then it is believed your wordpress upgrade has been hacked.
This has now been confirmed as a definite hack. To confirm, YOUR WEBSITE HAS BEEN HACKED!

You can review this forum for help on fixing this error

Some people are simply replacing the functions.php file with a new copy of this file, however there is likely to be a number of files in your root directory (take a look for files that just start with a load of numbers)

It is recommend (strongly recommended) to fix this error permanently and to remove the hackers files from your website that you work through the following resources. (I can help if required but i do charge $100 for this as its a lot of work)

Ill update this post when a patch from wordpress is released or further advice is received.

Best WordPress Tag Plugins & Widgets

With so many plugins available for WordPress its often frustrating trying to find the one you want to do the thing you need it to do.

I’m creating a list of the best WordPress Tag Plugins here. I’ll add more to it as i find and use more myself. Feel free to add a comment on the ones you like.

Taxonomy List Widget

If you want to show a list of your Tags with the number of times they have been used in list format then this plugin does that for you. You can simply add it as a widget to any widget-ed area like a sidebar.

Ultimate Tag Cloud Widget

If you prefer a Tag Cloud then the Ultimate tag cloud widget is the one for you. When you hover over the tag it also shows the number of times you have used that tag across all your posts.

Moving WordPress Sidebar from Right to Left Twenty Twelve Theme

In WordPress Twenty Twelve Theme many people find it difficult to move the sidebar from the right side to the left side.

Well it is easy if you know how and i have put together instructions along with a video for this for you.

Simply add this code to the bottom of your style.css file and save and refresh your page to see the changes.

/*– move sidebar to the left fix by wperror.com –*/
@media screen and (min-width: 600px) {
.site-content {
float: right;
}
.widget-area {
float: left;
}
}

/* for IE8 and IE7 —————-*/
.ie .site-content {
float: right;
}
.ie .widget-area {
float: left;
}

If you are not using the Twenty Twelve them then try using this code instead of the above code

@import url(‘../twentytwelve/style.css’);

/*– move sidebar to the left fix by wperror.com –*/
@media screen and (min-width: 600px) {
.site-content {
float: right;
}
.widget-area {
float: left;
}
}

/* for IE8 and IE7 —————-*/
.ie .site-content {
float: right;
}
.ie .widget-area {
float: left;
}

Still unsure, then watch this video.