Archive for the ‘WordPress as CMS’ Category

How to Customize Multiple Search Result Pages in Wordpress

Thursday, July 23rd, 2009

In the previous post, we discussed how to hack the search function in Wordpress to have an additional search form that would search  subcategories. Now that you have 2 or more search forms on your site, you might need to customize the search results. Thanks to this solution, I learned how to create multiple search result pages.

Let’s say we have 2 search forms on our site:

  1. General Site-Wide Search
  2. Recipe Search – searches subcategories of the Recipe Category

The first thing we need to do is tweak the search.php template to use it as a filter that will recognize if a search is coming from the Site-Wide search form or the Recipe Search form.

1. Open Search.php and delete everything.  Add the following code:

<?php
/* Template Name: Search Results */
$search_refer = $_GET["site_section"];
if ($search_refer == ‘recipe’) { load_template(TEMPLATEPATH . ‘/recipe-search.php’); }
elseif ($search_refer == ’site-search’) { load_template(TEMPLATEPATH . ‘/site-search.php’); }; ?>

2. Open Header.php or wherever the General Site-Wide Search Form is located and add this line:

<input type=”hidden” name=”site_section” value=”site-search” />

The Site-Wide Search form will look something like this:

<form method=”get” id=”searchform” action=”<?php bloginfo(‘home’); ?>/”>
<div id=”search”>
<input type=”text” value=” ” onclick=”this.value=”;” name=”s” id=”s” />
<input type=”hidden” name=”site_section” value=”site-search” />

<input name=”" type=”image” src=”<?php bloginfo(’stylesheet_directory’); ?>/styles/<?php echo “$style_path”; ?>/search.gif” value=”Go” />
</div><!–/search –>
</form>

3. Open Recipes.php or wherever your second search is and insert this line:

<input type=”hidden” name=”site_section” value=”recipe” />

You can change the value “recipe” to whatever suits your needs. Just make sure it matches the value in search.php.

The Recipe Search form for your second search will look something like this. In my case, this second search is meant to search the subcategories of the Recipe category.  See my previous post to learn about hacking the search function to search subcategories.

<form method=”get” id=”rsearchform” action=”<?php bloginfo(‘home’); ?>/”>
<div id=”rsearch”>
<input type=”text” value=”Recipe Search… ” onclick=”this.value=”;” name=”s” id=”rs” />
<?php $categories = get_categories(‘child_of=11′);
$catlist = ”;
foreach ($categories as $cat) {
$catlist.= $cat->cat_ID.’,';
}
$catlist.’11′;
?>
<input type=”hidden” name=”cat” value=”<?php echo “$catlist”?>” />
<input type=”hidden” name=”site_section” value=”recipe” />
<input name=”" type=”image” src=”<?php bloginfo(’stylesheet_directory’); ?>/styles/<?php echo “$style_path”; ?>/search.gif” value=”Go” />
</div><!–/search –>
</form>

4. Customize the Search Results Templates

If you recall, in step 1, we added the following to the search.php page.

if ($search_refer == ‘recipe’) { load_template(TEMPLATEPATH . ‘/recipe-search.php’); }
elseif ($search_refer == ’site-search’) { load_template(TEMPLATEPATH . ‘/site-search.php’); }; ?>

Create recipe-search.php (or whatever you named this template) and customize the layout of the results page to your liking. You can use index.php to start with and customize from there.

Create site-search.php (or whatever you named this template) and customize the layout of the results page to your liking. You can use index.php to start with and customize from there.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

How to Hack the Wordpress Search Function: Search Categories and Child Categories

Tuesday, July 21st, 2009

Recently, we had a client who wanted a special recipe search. There were 2 ways to extend the search functionality – manually or dynamically.

1. Search One or More Categories Manually

If we want to search one category, we could have used this solution which lets you manually add category numbers to the search function:

[Replace value="5" with your category number.]

<form method=”get” id=”searchform” action=”<?php bloginfo(‘home’); ?>/”>
<div>
<input type=”text” value=”<?php echo wp_specialchars($s, 1); ?>” name=”s” id=”s” />
<input type=”hidden” name=”cat” value=”5″ />
<input type=”submit” id=”searchsubmit” value=”Search” />
</div>
</form>

2. Search Subcategories Dynamically

However, we wanted to dynamically search only the subcategories of the Recipe Category. The reason being that if you’re already in the Recipes section of the site, seeing a recipe labeled as Recipes is not helpful. However, seeing a recipe labeled as Dessert (mmm..) and Breakfast is helpful. So, each recipe that the client adds will be in a subcategory of recipes (ex. Dessert), but they won’t have to check off the checkbox next to the Recipes category itself.

Anyway, I’ve been looking for this solution for a long time and now, thanks to Ilan Cohen, I present it to you.

[Replace the number 5 with the parent category.]

<form method=”get” id=”searchform” action=”<?php bloginfo(‘home’); ?>/”>
<div id=”search”>
<input type=”text” value=”Search… ” onclick=”this.value=”;” name=”s” id=”s” />
<?php $categories = get_categories(‘child_of=5′);
$catlist = ”;
foreach ($categories as $cat) {
$catlist.= $cat->cat_ID.’,';
}
$catlist.’5′;
?>
<input type=”hidden” name=”cat” value=”<?php echo “$catlist”?>” />
<input name=”" type=”image” src=”<?php bloginfo(’stylesheet_directory’); ?>/styles/<?php echo “$style_path”; ?>/search.gif” value=”Go” class=”btn” />
</div><!–/search –>
</form>

The code searches through all the child categories of Category 5 (Recipes). Then, you’ll see we added an additional “5″ in this line: $catlist.’5′; just in case the client puts a recipe in the Recipes category, and not one of its subcategories.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

How to rename widgetized sidebars in Wordpress

Thursday, November 20th, 2008

You can manage multiple sidebar widgets in WordPress. To do so, you go to the Widgets page in the Admin, and select the Sidebar you want to manage. If you have 1 widgetized sidebar, the name “Sidebar 1″ is not a big deal for managing it. But what if you have 5 or more, and they’re named Sidebar 1, Sidebar 2, etc. Ah yes, now what was that wily sidebar 3 for?

Why would someone have so many sidebars to begin with? Well, remember that you can also add widget-ready sidebars to Wordpress footers or anywhere in your design, to give extra content management options to clients.

Recently, we had a client with way too many widgetized sidebars to keep track of, so we had to find a new solution to change the standard widget sidebar names like “sidebar 1″ or “sidebar 2″ to something more meaningful like “Left sidebar” and “Right sidebar” in the admin area. I dug around the web and found 2 very helpful posts:

Here’s what I learned:

1. Go into your themes’s function.php file, or if it doesn’t exist, create it.

Add the following code:

<?php

if (function_exists(‘register_sidebar’))

{

register_sidebar(array(

‘before_widget’ => ‘<li>’,

‘after_widget’ => ‘</li>’,

‘before_title’ => ‘<h4>’,

‘after_title’ => ‘</h4>’,

‘name’ => ‘Left sidebar’

));

register_sidebar(array(

‘before_widget’ => ‘<li>’,

‘after_widget’ => ‘</li>’,

‘before_title’ => ‘<h4>’,

‘after_title’ => ‘</h4>’,

‘name’ => ‘Right sidebar’

));

}

?>

2a. Add a widgetized sidebar to your theme. Open up your theme’s sidebar file (for exampe, l_sidebar.php) and look for the first <ul> or <ul id=”sidebar”> or something similar, and add the following code:

<?php if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(‘Left sidebar’) ) : ?>

2b. If your sidebar is already widgetized, find the following code

<?php if ( function_exists(‘dynamic_sidebar’) && dynamic_sidebar(1) ) : else : ?>

and replace it with

<?php if ( !function_exists(‘dynamic_sidebar’) || !dynamic_sidebar(‘Left sidebar’) ) : ?>

Then, find the closing </ul> at the very bottom of the file, and immediately before that, place

<?php endif; ?>

3. Now, if you go into your admin panel, under Design>Widgets, you’ll see the new names like in the image below.

Now you can easily manage your Widgets without trying to guess which one is which.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

13 plugins that will make WordPress into a CMS

Monday, January 14th, 2008

Recently I posted about why I think WordPress is a CMS. This led to a pretty interesting discussion in the comments on the topic, but it was unresolved.

Here’s some more fuel for the fire: I just came across Josh Byers’ blog, thanks to his amazing Custom Admin Branding Plugin. He has a post there that lists the plugins he uses to transform WordPress into a full-fledged content management system (CMS). Aside from the fact that it’s good to know about the plugins he mentions, it’s interesting to note that by adding a few plugins, Josh feels that his WordPress sites can be considered CMSs, as opposed to just blogs.

Here are some of the plugins that he says he uses to make WordPress into a CMS. Visit his blog to see the rest of them, and read his concise descriptions of each one, and why he uses them:

Here are a few more tweaks and plugins that I think help make WordPress into a great CMS:

  • The WordPress static home page option – obviously. The fact that the home page of your blog can be a static Page by selecting a single option in the admin changes WordPress from a blog into a CMS. I don’t know why people always ignore this.
  • Page Links To Plugin: gives more flexibility to WordPress Pages by allowing you to link them to other pages, like category pages.
  • Role Manager Plugin: better and more precise role management in WordPress
  • Future Posts Calendar Plugin: this plugin is a savior. It gives me an overview of the days when posts are scheduled to be published, so I can make sure that posts are spread out evenly across the week, instead of all of them ending up on Tuesday (for example).
  • Admin Drop-down Menus Plugin: also a life saver (not the candy). Can you imagine getting to the Write Post page with just one click, instead of clicking on Manage, waiting, and then clicking on Post? Will, this plugin turns everything into drop-down menus, so submenus are just one click away.
  • All in One SEO pack, Google Sitemaps Generator Plugin and Meta Robots WordPress plugin: just upload and activate these WordPress plugins, and you’ll have a serious SEO advantage over so many other sites.

I think that the whole argument over whether WordPress is a CMS boils down to the lack of an exact definition of what a CMS is. So, according to my definition of a CMS, which holds no weight whatsoever, WordPress meets the criteria.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

ZDNet says WordPress not clunky, but also not CMS

Friday, December 21st, 2007

Larry Dignan over at ZDNet writes about the media’s relationship with CMS systems, particularly his own past experiences with custom-built CMS systems. He says that “when it comes to ease of use, a blog platform beats or [sic] average CMS hands down.” So he asks why it is that he’s always getting stuck with some clunky, Frankenstein-like CMS system when he could happily and easily use something like WordPress. And he basically asks if people in the media industry will ever figure out that they don’t have to reinvent the wheel, since it already exists.

But what struck me the most about this article was the Update at the end, where he says that he was corrected by Dennis Howlett, who pointed out that WordPress isn’t actually a CMS.

This is something that I really don’t get. WordPress manages content, does it not? Then why isn’t it a CMS? How come I can call the awful, clunky systems that I used before WordPress CMS systems, even though they don’t have even half of the functionality and features of WordPress.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

WordPress challenge: getting class current_page_item to work when home page is not blog

Wednesday, December 5th, 2007

Sorry for the confusing title, but there is an issue that we face over and over when using WordPress as a CMS, and have not been able to solve. When we are using WordPress as a CMS, our Blog page doesn’t pick up the current_page_item class and therefore its link on the nav bar isn’t highlighted like the other pages are. How can we get it to change?

I know that the above might not make sense, so here is a detailed description of the problem:

If you use WordPress as a CMS, you generally create the Pages you want to appear on your nav bar under Write > Write Page. One of those Pages is the Home page, for example, and you create another Page for your blog posts called Blog. Then, you go to Options > Reading, and select one of those pages for your front page from the drop-down list, in this case you would select the Home page, and another page for your blog posts, in this case the Blog page.

options-read1.png

You create a style in your style sheet called current_page_item which causes the current page that the viewer is on to appear differently in the nav bar or list of pages. For example, you want the background color of that page on the nav bar to change from green to purple.

Now, here’s where the problem lies: all pages on the nav bar change from green to purple when the user is on that page…except the Blog page! For some reason, that Blog page does not pick up the current_page_item class.

So my question is: does anyone know of a solution to this problem?

Thanks!

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Display posts from specific categories on a Page

Thursday, November 8th, 2007

You may want to display posts from a specific category on the homepage or other pages of your site. For example, you might want to have a box that displays the latest three posts from your News category, or have links to your latest podcasts.

In a previous post, I gave the code that would display the latest posts from a specific category in the sidebar. However, that code will not work in the main content part of the page. In order to display posts from a specific category in the main content section of a page, you need to use a variation on the WordPress Loop, which is as follows:
<?php query_posts('category_name=special_cat&showposts=10'); ?>
<?php while (have_posts()) : the_post(); ?>
<!-- Do special_cat stuff... -->
<?php endwhile;?>


To use this code, change the category name to whatever the name is of the relevant category, and change the number of posts displayed from 10 to whatever number you want. Where it says <!-- Do special_cat stuff... -->, put in all the code you usually use to display the title, date, category, etc. – whatever you need.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Relooking at the Page Links To Plugin

Thursday, June 21st, 2007

Since I discovered the Page Links To plugin, it’s become one of the plugins that I use most on my sites, particularly those where I’m using WordPress to create a CMS. I’ve reviewed it already (see the review here), but it seems that they have added a great features which was lacking in the previous version.

First, let’s just review what this plugin does: it allows you to create a Page, and then have that page link to any URL anywhere on the web. I mostly use it to link Pages in my navigation bar to Categories, or posts, but you could theoretically link a Page to any other page on the web.

It seems that in the new version, Pages links in the navigation are now highlighted when you click on them, even if they are linking using the plugin. This means that if you set a CSS style so that the current page you are on looks different in the navigation, this will apply also when the Page links using this plugin.

I highly recommend checking out this plugin…

Page Links To>> 

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Using different style sheets for different templates

Sunday, June 3rd, 2007

I’m creating a site where the pages are divided into two sections: Hebrew and English. I need the Hebrew pages to use a template that basically looks the same as the English pages, but everything is switched around to right-to-left.

I created a copy of the style sheet and called it style-hebrew.css. In the copy, I made all the relevant changes so that everything is flipped around, which basically meant writing direction: rtl in a bunch of places, and floating things to the right instead of left, and vice versa. But now I needed a way to call this style sheet on the Hebrew pages.

First, I created a page template called hebrew-page.php. In this template, I replaced

<?php get_header(); ?>

with

<?php include ('header-hebrew.php'); ?>

Then I copied the header.php file and renamed it header-hebrew.php. In this file, I replaced the usual call to the style sheet:

<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />

with the call to my new Hebrew style sheet:

<link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/style-hebrew.css" type="text/css" media="screen" />

Now, in the admin, whenever a Hebrew page is created, they just have to make sure to select the Hebrew Page template from the Page Template drop-down menu on the right-hand panel.

This method can be used for any site that needs to call different styles for different pages or categories. Just create another style sheet and header.php files, and call them in the appropriate places.

There may be a more efficient way to do this, but I have yet to discover it.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

List only child Pages of a specific parent Page

Wednesday, May 9th, 2007

I am building a site for a client, and I want to list all the child Pages, which will be many, of one of their parent pages in the sidebar. To help you understand better, this client provides many services, so I wanted there to be a parent Page called “Services,” and this parent Page will have a list of child Pages, each of which goes into detail about each specific service.

I couldn’t find any information on how to do this, but then I came across a tag for categories that allows you to only list the subcategories of a specific category. This tag is as follows:

<?php wp_list_cats('child_of=8, 14'); ?>

By using this tag, you would only list the child categories of the categories with IDs of 8 and 14. So I decided to see if this would work with Pages, and it did!

So here’s the code I put in the sidebar to list only the child Pages of the Services Page, sorted alphabetically and omitting the title of “Pages”:

<?php wp_list_pages('child_of=2&sort_column=post_title&title_li=') ?>

Hurray!

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
Premium News Themes