Numbered Search Results in WordPress

I needed to show search results in an ordered list for a project I am working on and was very surprised to discover how involved this is for paginated search results. Showing the results using a regular ordered list starts the list numbering over at 1 on each new page of results, even though the next set of results was being displayed.

After some horribly hacky solution involving grabbing the page number from the URL and hard coding the number of posts per page, I was led in the right direction by NicktheGeek at StudioPress who suggested getting the number of posts per page from the query string and using the paged value to get the numbers dynamically before applying the math to get the list numbers to display correctly.

A bunch of CSS was required to make the list numbers look like they were actually generated by an <ol> tag. Styling will vary depending on your theme, but one thing that may help if things are looking messed up is to display the post/page titles and contents inline.

CSS:

.search-results #content h2.entry-title,
.search-results #content .post,
.search-results #content .page {
	display: inline;
}

PHP:
For non-Genesis themes, leave out the genesis_ lines.

global $paged;
if(empty($paged)) $paged = 1;

$loop_counter = 1;

$results_per_page = get_query_var('posts_per_page');

echo '<ol class="search-list">';

genesis_before_post();

if ( have_posts() ) : while ( have_posts() ) : the_post();

	if( $paged == 1 ) {
		$real_count = $loop_counter;
	} else {
		$real_count = $loop_counter + ( $paged * $results_per_page - $results_per_page);
	}

	echo '<li><span class="listnum">' . $real_count . '.</span>';

	// The Post

	echo '</li>';

	genesis_after_post();

	$loop_counter++;

	endwhile;
	genesis_after_endwhile();

else :
	genesis_loop_else();
endif;

echo '</ol>';

Genesis: Remove/Change post info/post meta

// Remove post meta
remove_action('genesis_after_post_content', 'genesis_post_meta');

// Customize the post meta function
add_filter('genesis_post_meta', 'post_meta_filter');
function post_meta_filter($post_meta) {
if (!is_page()) {
    $post_meta = 'Filed Under:   Tagged With: ,  ';
    return $post_meta;
}}

// Remove the post info function
remove_action('genesis_before_post_content', 'genesis_post_info');

// Customize the post info function
add_filter('genesis_post_info', 'post_info_filter');
function post_info_filter($post_info) {
if (!is_page()) {
    // Spaces are added to [ shortcodes ] so that they will display in this post.
    $post_info = '[ post_date ] by [ post_author_posts_link ] at [ post_time ] [ post_comments ] [ post_edit ]';
    return $post_info;
}}

Please note that you do not need to remove a function before editing it. Hat tip to jim :)

Genesis: Custom loop arguments

remove_action('genesis_loop', 'genesis_do_loop');
add_action('genesis_loop', 'custom_do_cat_loop');

function custom_do_cat_loop() {
    global $query_args;  // any wp_query() args
    $args= array('orderby' => 'title', 'order' => 'ASC');
    genesis_custom_loop(wp_parse_args($query_args, $args));
}

Genesis: Custom Page Template

<?php // Template Name: Template Name Here

remove_action('genesis_loop', 'genesis_do_loop');
add_action('genesis_loop', 'custom_loop');

function custom_loop() {
    global $paged;
    $args = array('post_type' => 'PostType'); // any wp_query args can go here
    genesis_custom_loop( $args );

}

genesis();

Genesis: Default Post Thumbnail

// Default post thumbnail
add_filter('genesis_get_image', 'default_image_fallback', 10, 2);
	function default_image_fallback($output, $args) {
			global $post;
			if( $output || $args['size'] == 'full' )
					return $output;

			$thumbnail = CHILD_URL.'/images/thumb.jpg';

			switch($args['format']) {

					case 'html' :
							return '<img src="'.$thumbnail.'" class="alignleft post-image" alt="'. get_the_title($post->ID) .'" />';
							break;
					case 'url' :
							return $thumbnail;
							break;
				 default :
						 return $output;
							break;
			}
	}

Genesis Grid Loop

New in Genesis 1.5

CSS:

/* Featured Post Grid
------------------------------------------------------------ */

.genesis-grid-even {
    float: right;
    padding: 0 0 15px;
    width: 48%;
}

.genesis-grid-odd {
    clear: both;
    float: left;
    padding: 0 0 15px ;
    width: 48%;
}

.genesis-grid-even,
.genesis-grid-odd {
    margin: 0 0 20px;
}

PHP:

/* functions.php:
------------------------------------------------------------ */

add_image_size('grid-thumbnail', 100, 100, TRUE);

/* home.php:
------------------------------------------------------------ */

<?php
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'pretty_grid_loop_helper' );
/** Add support for Genesis Grid Loop **/
function pretty_grid_loop_helper() {
    if ( function_exists( 'genesis_grid_loop' ) ) {
        genesis_grid_loop( array(
            'features' => 2,
            'feature_image_size' => 0,
            'feature_image_class' => 'alignleft post-image',
            'feature_content_limit' => 0,
            'grid_image_size' => 'grid-thumbnail',
            'grid_image_class' => 'alignleft post-image',
            'grid_content_limit' => 0,
            'more' => __( '[Continue reading...]', 'genesis' ),
            'posts_per_page' => 6,
        ) );
    } else {
        genesis_standard_loop();
    }
}

/** Remove the post meta function for front page only **/
remove_action( 'genesis_after_post_content', 'genesis_post_meta' );

genesis();

/*
Understanding the Parameters
------------------------------------------------------------ *//*

‘features’
This is the number of posts that will show at the top of the page.

‘feature_image_size’
This is the size of the featured image in the features section to be shown (set to 0, which will return no image).

‘feature_image_class’
This is where classes are assigned to the featured image in the features section which can be used for styling.

‘feature_content_limit’
This is where you can specify the number of characters of the post to show in the features section (set to 0, which will return the full content).

‘grid_image_size’
This is the size of the image in the grid section to be shown (set to 0, which will return nothing).

‘grid_image_class’
This is where classes are assigned to the featured image in the grid section which can be used for styling.

‘grid_content_limit’
This is where you can specify the number of characters of the post to show in the grid section (set to 0, which will return the post excerpt).

‘more’
This is where you can specify the text that is displayed when using the content limit option.

‘posts_per_page’
This is were you can determine how many posts are shown on each page.
*/

Genesis: Custom search button and search form text

// Search form text
add_filter('genesis_search_text', 'custom_search_text');
function custom_search_text($text) {
    return esc_attr('Search...');
}

// Add custom text for search button
add_filter('genesis_search_button_text', 'custom_search_button_text');
function custom_search_button_text($text) {
    return esc_attr('Go');
}

Genesis Footer Content

Customize the footer text

add_filter('genesis_footer_creds_text', 'custom_footer_creds_text');
function custom_footer_creds_text($creds) {
    $creds = '&copy;' . date('Y') . ' '
		. get_bloginfo('name') .
		'. All rights reserved. Powered by <a href="http://wordpress.org/">WordPress</a>.';
    echo $creds;
}

Remove Footer Content

This will remove the footer credits text and ‘back to top’ link.

add_filter('genesis_footer_output', 'footer_output_filter', 10, 1);
function footer_output_filter($footer_content) {
    $footer_content = '';
    return $footer_content;
}

Replace Footer With A WordPress Custom Menu

This will replace the default footer with credit text and a WordPress custom navigation menu.
Ex. © Copyright 2011 LBnuke. All rights reserved.    Home | About | Archives | Contact

remove_action( 'genesis_footer', 'genesis_do_footer' );
add_action( 'genesis_footer', 'child_do_footer' );
function child_do_footer() {
	$creds = '&copy; Copyright '. date('Y') . ' ' . get_bloginfo('name') . ' . All rights reserved.';
	$footernav = wp_nav_menu( array('menu' => 'footer' ));
	echo $footernav;
	?>
	<p class="creds"><?php echo $creds; ?></p>
<?php }

Genesis: Custom superfish arguments

// functions.php
function custom_superfish_menu() {
    if ( genesis_get_option('nav_superfish') ) {
			wp_deregister_script('superfish-args');
			wp_enqueue_script('superfish-args', CHILD_URL.'/superfish.args.js', array('jquery'), '1.0', TRUE);
    }
}
add_action('get_header', 'custom_superfish_menu');
// superfish.args.js
jQuery(document).ready(function($) { 

	$('#nav ul.superfish, #subnav ul.superfish, #header ul.nav, #header ul.menu').superfish({
		delay:       0,		// delay on mouseout
		speed:       'fast',
		animation:   {opacity:'show',height:'show'},	// fade-in and slide-down animation
		autoArrows:    false,
		dropShadows: false,		// disable drop shadows
		pathClass:	'current-menu-item',
		onInit:	function(){
			$(this).addClass('my-custom-class');
		}
	});

});

Genesis: Parent Page Templates

The template below displays child posts below any page content of the parent post with featured image thumbnails. Images are linked to child page. If there is an image before any page content on the child page, it will not display in order to avoid breaking the layout on posts with large images.

To display the images on the right side, change the class on this line (~ line 17):

genesis_image( array( 'format' => 'html', 'size' => genesis_get_option( 'image_size' ), 'attr' => array( 'class' => 'alignleft post-image' ) ) );

from

'class' => 'alignleft post-image'

to

'class' => 'alignright post-image'

Parent Page Template:

<?php // Template Name: Parent-Image-Left

add_action('genesis_after_loop', 'bd_child_pages');
function bd_child_pages(){
		global $post;
?>

<div id="child-pages">

	<?php query_posts( 'post_type=page&post_parent='.$post->ID.'&orderby=menu_order&order=ASC' ); ?>
	<?php if( have_posts() ) : while ( have_posts() ) : the_post(); ?>

		<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

<?php
	echo '<a href="' . get_permalink($post->ID) . '" >';
	    genesis_image( array( 'format' => 'html', 'size' => genesis_get_option( 'image_size' ), 'attr' => array( 'class' => 'alignleft post-image' ) ) );
	echo '</a>';

	$content = apply_filters( 'the_content', get_the_content() );

	preg_match('#<p[^>]*>(.*)</p>#isU', $content, $matches);
	$content = preg_replace('/<img[^>]+\>/i', '', $matches[0]);

	echo $content;

	$count = post_word_count();
	if( sizeof( explode( " ", $content) ) < $count ) { ?>
	<div class="readmore">
		<a href="<?php the_permalink(); ?>">Read more &hellip;</a>
	</div>
	<?php	} ?>

<div class="clear"></div>
<?php endwhile; endif; ?>
</div><!--end child-pages-->

<?php }

genesis();

functions.php:

// Post word count
function post_word_count() {
    ob_start();
    the_content();
    $wcount= ob_get_clean();
    return sizeof(explode(" ", $wcount) );
    ob_end_clean();
}

Changelog
2/23/2011: Added links to featured images and ob_end_clean() to word count function.