Archive for the ‘Web Applications’ Category
Posted: July 1st, 2009 | Author: Henrie Media Inc. | Filed under: Web Applications | 3 Comments »
If you are a WordPress developer, or user, there may come a time where you will want to use the WordPress query_posts() function to retrieve your blog posts rather than the standard loop. For example, I wanted to exlude a category that contains all of my Twitter Tweets (I know, but run with in). So I put in the following code:
if (is_home()) {
query_posts(“cat=-52″);
}
The problem is that this also messed up the pagination of the page. To fix this issue I added this code:
if (is_home()) {
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
query_posts(“cat=-52&paged=$paged”);
}
Let me explain what I’ve added, and why.
query_posts() is a powerful function, but in this situation it has a flaw: it overrides nearly everything in the standard posts object query, including what the paged offset is. To get proper pagination with query_posts() we need to recreate it through the ‘paged’ parameter or query. Best way to do this is to ask WordPress for the “page” we happen to be on, and use that as our ‘paged’ value.
The $paged = line above uses what’s called a ternary operator to test whether the ‘paged’ query variable is available. If it is, $paged receives its value. If it isn’t, we assume we’re on the first page and assign it ’1′. Then we tell query_posts() what page we’re on with the addition of &paged=$paged.
Shazam! Fixed. Let me know if this helped you as well! Comments are always welcomed!
Print, Email, Share, Enjoy!
Posted: August 6th, 2008 | Author: Henrie Media Inc. | Filed under: Web Applications | No Comments »
Have you ever wanted to exclude a category from the main page or the RSS feed of your WordPress blog? This is the easiest way I have found to do this, without installing a plug-in.
Put the following code anywhere in your theme’s template functions.php file. For example, if your theme is called “travel” then you would probably find this file here – wp-content/themes/travel/functions.php. If for some reason this file doe not exisit, just create it and save it in your theme’s folder.
Copy and paste the following into the functions.php file -
function exclude_category($query) {
if ( $query->is_feed || $query->is_home ) {
$query->set(‘cat’, ‘-1′);
}
return $query;
}
add_filter(‘pre_get_posts’, ‘exclude_category’);
The code above excludes the category with the id of “1″ from both the RSS feed and from the main page of your blog. If you want to exclude multiple categories, simply add a comma ( , ) and a dash ( – ) followed by the category ID. For example: ‘-2, -3, -4′. You can locate the category id by logging into the WordPress dashboard, clicking on manage, then categories.
If you just want to exclude a category from the main page and not the RSS feed, simply replace “( $query->is_feed || $query->is_home )” with “( $query->$query->is_home )” or to remove it from just the RSS feed “( $query->is_feed)”.
That’s all there is to it! Happy coding!
Print, Email, Share, Enjoy!
Posted: August 5th, 2008 | Author: Henrie Media Inc. | Filed under: Web Applications | 1 Comment »
So I have wanted to have dynamically generated META Code (title, description, keywords) for my WordPress blog for sometime now. I tried all of the plug-ins and frankly, they just didn’t get me where I wanted to go. I also didn’t want to do all the extra work of maintaining separate text for the META fields – I wanted them dynamically generated.
So I sat down and wrote the following code that I think you might find helpful to increase your blog’s SEO (Search Engine Optimization) value.
All of these changes should occur in your header.php file of your WordPress blog’s template files.
First, I wanted to dynamically create the <title> tag with the blog’s name as the default, but include the category, archive or page title when I was browsing a category, archive or page. Here is the code to do that. Simply cut and paste this code, replacing your current <title></title> tag.
<title><?php if (is_home()): bloginfo(‘name’); elseif (is_category()): bloginfo(‘name’); print(” – “); single_cat_title(); elseif (is_day() || is_month() || is_year()): bloginfo(‘name’); print(” – “); the_time(‘F Y’); else: bloginfo(‘name’); print(” – “); the_title(); endif; ?></title>
Next, i wanted to dynamically create the <META description> tag to make it unique to each page of my blog. Specifically I wanted it to display the current category or archive I was browsing or if I was browsing a page I wanted it to dynamically create the description from the first 20 words of my post. Here is the code to do that. Simply cut and paste this code, replacing the current <META description=”" /> tag.
<meta name=”description” content=”<?php if (have_posts()&& is_single()):while(have_posts()):the_post(); the_excerpt_rss(20,2); endwhile; elseif (is_category()): single_cat_title(); print(” category of the “); bloginfo(‘name’); elseif (is_day() || is_month() || is_year()): the_time(‘F Y’); print(” archive of the “); bloginfo(‘name’); else: bloginfo(‘description’); endif; ?>” />
NOTE: You can change the number of words contained in the page description by changing the first number of the_excerpt_rss() function. So in my example, I have it set to 20 words – the_excerpt_rss(20,2). If I wanted 25 words, I would change it to this – the_excerpt_rss(25,2).
Finally, the <META keywords>. I wanted to keep the keywords consistent from category to archive to page, only adding additional category names or archive attributes. Here is the code to do that. Simply cut and paste this code, replacing the current <META keywords=”" /> tag.
<meta name=”keywords” content=”<?php if (is_category()): single_cat_title(); print(“, “); elseif (is_day() || is_month() || is_year()): the_time(‘F’); print(“, “); the_time(‘Y’); print(“, “); endif;?>travel, blog, destination, information, see, do, help, advice, reviews, save, savings, ideas, business, golf” />
That’s all there is to it! To make it easier for you, here is all of the change in once place so you just copy and paste once! Happy SEO!
<title><?php if (is_home()): bloginfo(‘name’); elseif (is_category()): bloginfo(‘name’); print(” – “); single_cat_title(); elseif (is_day() || is_month() || is_year()): bloginfo(‘name’); print(” – “); the_time(‘F Y’); else: bloginfo(‘name’); print(” – “); the_title(); endif; ?></title>
<meta name=”description” content=”<?php if (have_posts()&& is_single()):while(have_posts()):the_post(); the_excerpt_rss(20,2); endwhile; elseif (is_category()): single_cat_title(); print(” category of the “); bloginfo(‘name’); elseif (is_day() || is_month() || is_year()): the_time(‘F Y’); print(” archive of the “); bloginfo(‘name’); else: bloginfo(‘description’); endif; ?>” />
<meta name=”keywords” content=”<?php if (is_category()): single_cat_title(); print(“, “); elseif (is_day() || is_month() || is_year()): the_time(‘F’); print(“, “); the_time(‘Y’); print(“, “); endif;?>add, more, keywords, here” />
Print, Email, Share, Enjoy!
Posted: May 23rd, 2008 | Author: Henrie Media Inc. | Filed under: Web Applications | 5 Comments »
I just installed WordPress as my default Content Management System (CMS) on Henriemedia.com. WordPress is an awesome open-source web application the provides an easy to use – yet extremely robust – interface and set of tools. I highly recommend using it for your next blog or CMS needs (shameless plug).
Anyways, I installed WordPress in the root of my website and kept an administrative subdirectory from my previous website design, which is protected with a password using the “Protected Directory” function of Apache. For the sake of this article, we will say that subdirectory is called “travel” (there is no such directory on my website so please do not try and find it).
I then configured the WordPress settings for “Permalinks” to help with the Search Engine Optimization (SEO) value of my website. Everything was going along great. I started adding content and I was on my way – until I tried accessing the “travel” protected directory I setup. Every time I tried to access this folder I would get a 404 error. The reason the 404 error was displaying is because the .htaccess file, which was automatically created by the Permalinks settings, was causing the problem.
WordPress created .htaccess file looks like this -
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
What it does is check to see if the requested filename is a regular file or a real directory. If it’s neither of the two then redirect to /index.php, which is a WordPress entry. So Apache will load the file or directory instead of WordPress index.php. However, the fact that the requested directory is password protected with its own directory .htaccess file seems to cause Apache to think it’s not really a directory or file, thus satisfying the 2 tests and invoking WordPress’s index.php – and thus the 404 error.
After days (and I do mean days) of searching the forums on WordPress.org and many other forums, boards and blogs, I found two solutions to this problem.
Please use one OR the other, do not implement both!
First, the .htaccess solution. NOTE: If you are not comfortable editing the .htaccess file, please scroll down and use the PHP solution. You need to added the following two lines of code to your .htaccess file:
ErrorDocument 401 /[path_to_file]/[file_name].html
ErrorDocument 403 /[path_to_file]/[file_name].html
Remember to change the [path_to_file] and {file_name].html values to the location of your custom error documents. So your the root .htaccess file should now look like this -
ErrorDocument 401 /[path_to_file]/[file_name].html
ErrorDocument 403 /[path_to_file]/[file_name].html
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Upload the .htaccess file and you should be up and running. Now I have seen some instances where this solution does not work, for whatever reason, or people do not feel comfortable editing their .htaccess files. If this is the case, please use the PHP solution below.
Second, the PHP solution. You need to make the following changes to the “index.php” file that is in the root directory of your WordPress installation. When you open this file, it should have the following code -
<?php
/* Short and sweet */
define('WP_USE_THEMES', true);
require('./wp-blog-header.php');
?>
Make sure you keep a backup copy of the “index.php” file just in case (you never know)!
All you have to do is replace the original code with this code -
<?php
/* Short and sweet */
$request_filename = $_SERVER['DOCUMENT_ROOT'].$_SERVER['REQUEST_URI'];
$this_dir = dirname(__FILE__).’/';
if($request_filename!=$this_dir && $request_filename!=__FILE__ && (is_file($request_filename) || is_dir($request_filename))) {
// we are not supposed be here!
die;
}
// load WordPress in /wp
define(‘WP_USE_THEMES’, true);
require(‘./wp-blog-header.php’);
?>
Now save the file and upload the new “index.php” file to the root folder of your WordPress installation directory and your done!
What the PHP solution does is it makes sure the request is not for the home directory or index.php itself – this is because you want WordPress to handle these.
Then it tests if the request is a regular file or a real directory – it dies if that’s the case. Basically you are doing what the two RewriteCond lines in .htaccess supposed to do.
Now your password protected directory correctly asks for password instead of showing WordPress’s 404 error page. All WordPress permalinks work. All non-protected directories work. My “travel” subdirectory is now usable again – YAHOO!
I must give credit where credit is due. I found the .htaccess solution on Textpattern.com and the PHP solution on ju-ju.com.
I hope one of these solutions work for you so you don’t have to waste as much time as I did!
As WordPress’s motto says “Code is Poetry”. Please leave a comment if you find this post to be helpful.
Print, Email, Share, Enjoy!