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:
- General Site-Wide Search
- 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.
Posted in Code Snippets, WordPress as CMS | Tags: customize, multiple, Pages, results, search, wordpress | 1 Comment »
Thursday, June 4th, 2009
I wanted to create a simple directory of non-profit organizations. To do so, I wanted to use pages for the directory, rather than posts so that I could separate the static directory listings from the dynamic blog posts. I didn’t want to have to exclude tons of categories from feedburner and the main loop.
So, I started exploring the whole family in Wordpress – grandparents, parents, and children. Translation for those not yet used to seeing Wordpress analagous to the family in My Big Fat Greek Wedding: Pages, sub-pages, and sub-sub pages
Simple Directory Setup
- Directory Page (grandparent): Displays list of Non-Profit Organization Categories (ex. social, environment, health, etc.)
- Category Page (parent): Show title and excerpt of each Organization within a category (ex. Environment Organizations)
- Single Organization Page (child/current): Show content about a single organization(ex. SaveTheEarth – made up org for this example)
Here’s how to do it
- Create a Page Called Directory. This will be the Directory Page (grandparent)
- Find the Page ID. Let’s say the Page ID = 5. Depending on how you want to display the category info, you can
- Manually add the name of each category, a short description and a link
- Open up page.php so we can setup The Category Page (Parent Page). We want to show title and excerpts of each organization for each category.
- Add this code to page.php – Checks if we’re on a sub page / child page of the Directory (Page ID =10) and if so, list the pages in alphabetical order with excerpts. For the excerpt, I’m using the plugin Limit Posts since it didn’t work with The Excerpt Reloaded.<?php
$current = $post->ID;
$parent = $post->post_parent;
$grandparent_get = get_post($parent);
$grandparent = $grandparent_get->post_parent;
?>
<?php if ( $post->post_parent==”10″ ){ ?>
<?php $pageChildren = $wpdb->get_results(“SELECT * FROM $wpdb->posts WHERE post_parent = “.$post->ID.” AND post_type = ‘page’ ORDER BY post_title ASC”, ‘OBJECT’); ?>
<h2 class=”titles”><a href=”<? php echo get_permalink($pageChild->ID); ?>”> <? php echo $pageChild->post_title; ?></a></h2>
<?php the_content_limit(280, “”); ?><div class=”readmore”><a href=”<?php echo get_permalink($pageChild->ID); ?>” rel=”bookmark” title=”Permanent Link to <? php echo $pageChild->post_title; ?>”>Read More »</a></div>
(make sure to delete the space between <? and php - I had to to do that so it wouldn’t execute in this post)
- Single Organization Page (Child Page) On page.php, after the code you added in step 5, add this code that will check to see if we’re on the grandchild page. This will be the actual organization’s page. In our example, the SaveTheEarth Page. This is very helpful information in case you want to add a different style or add special items in the sidebar, etc.
<?php if ( $pageChildren ) : foreach ( $pageChildren as $pageChild ) : setup_postdata( $pageChild ); ?>
7. Set up how the rest of the pages on your site will look:
On page.php, after the code in step 6, add this code which instructs every other page in the site to act normally and display the content
<?php endforeach; ?>
<?php else : ?>
<?php the_content(); ?>
<?php endif; ?>
You can see the full page.php code here
You can see the directory in action here
I figured this out using the following helpful posts:
* http://wordpress.org/support/topic/186206
* http://wpguru.co.za/page/display-title-excerpt-of-child-page
Posted in Code Snippets | Tags: child, directory, grandparent, Pages, parent, wordpress | 10 Comments »
Thursday, January 10th, 2008
Recently I wrote about a WordPress plugin that allows you to exclude certain pages from appearing in your list of Pages without coding your template files. The My Page Order WordPress plugin gives you additional Page flexibility by allowing you to manually order your Pages without coding.
Update Feb. 17, 2008:
I just used this plugin for the first time, and it’s fantastic! All you have to do is upload it and activate it, and then go to Manage > My Page Order. There, you have an Ajaxy menu that shows you all your pages. You just drag and drop them until they’re in the order you want, and then click the “Click to Order Pages” button. You can also order subpages in the same way.

This is a plugin that can save you tons of time when you have five or more pages that you need to reorder.
My Page Order WordPress plugin
Posted in Plugins | Tags: admin, Pages | 4 Comments »
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.

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!
Posted in WordPress as CMS | Tags: CMS, navigation, Pages | 18 Comments »
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>>
Posted in Plugins, WordPress as CMS | Tags: CMS, Pages | 7 Comments »
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.
Posted in WordPress as CMS | Tags: categories, Pages | 13 Comments »