post

How to Make a Genesis Grid Archive Template for Tags

To display posts from a specific tag or tags, you need to add a ‘tag’ argument to the $grid_args array.

In the StudioPress tutorial about categories, you are given this example for your home.php.

<?php
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'child_grid_loop_helper' );
/** Add support for Genesis Grid Loop **/
function child_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,
            'cat' => '6,7' //enter your category IDs here separated by commas in ' '
        ) );
    } else {
        genesis_standard_loop();
    }
}

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

genesis();

Simply change the ‘cat’ argument to ‘tag’. And you can have the customization you want. OR, you can grab it dynamically by making the following changes:

<?php
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'child_grid_loop_helper' );
/** Add support for Genesis Grid Loop **/
function child_grid_loop_helper() {
    $term = get_query_var( 'term' );
    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,
            'tag' => $term
        ) );
    } else {
        genesis_standard_loop();
    }
}

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

genesis();

You can save this as tag.php to make all of your tag archives as a grid. Or, you can simply apply this to a single tag by naming it tag-{SLUG}.php or tag-{ID}.php (see WordPress Codex for Tag Templates for more information).

post

How to Make a Custom Taxonomy Genesis Grid Archive Template

First, catch up on the Grid Loops by reading these posts:

Now, that you have a basic understanding of the Grid, let’s apply this to a custom taxonomy archives.

First, you need a new document saved as taxonomy-{taxName}.php (for more information, see the WordPress Codex). So if you have a custom taxonomy of “Book Type,” registered as ‘book_type,’ then the file name will be taxonomy-book_type.php.

Second, create the header, just like a page template. This is primarily for your organization and your information. So I just model it after the page template just to be consistent.

<?php
/*
Template Name: Book Type Taxonomy Archive
*/

Third, you want to set all your customizations like remove post meta, post info, page layout, etc.

Fourth, you want to include the Grid Looper Helper function. This is a standard protocol for the Genesis Grid Loop:

<?php

remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'wps_grid_loop_helper' );
/** Add support for Genesis Grid Loop **/
function wps_grid_loop_helper() {
    global $grid_args, $post;
    $taxonomy = 'book_type'; //change me
    $term = get_query_var( 'term' );
    $term_obj = get_term_by( 'slug' , $term , $taxonomy );
    $cpt = 'wps_books'; //change me

    if ( function_exists( 'genesis_grid_loop' ) ) {
		$grid_args_tax = array(
			'features' => 0,
			'feature_image_size' => 'book_featured',
			'feature_image_class' => 'aligncenter post-image',
			'feature_content_limit' => 100,
			'grid_image_size' => 'book_thumb',
			'grid_image_class' => 'aligncenter post-image',
			'grid_content_limit' => 0,
			'more' => '',
			'posts_per_page' => 10,
			'post_type' => $cpt,
			'paged' => get_query_var('paged') ? get_query_var('paged') : 1,
			'tax_query' => array(
				array(
					'taxonomy' => $taxonomy,
					'field' => 'slug',
					'terms' => array( $term_obj->slug ),
				)
			)
		);

			printf('<h2 class="book-section"> %s Books</h2>' , $term_obj->name );
			genesis_grid_loop($grid_args_tax);
	} else {
		genesis_standard_loop();
	}
}

Now, this can be rather complicated and intimidating, so let me break this down for you. While everything is the same as the previous Grid posts, there is one major change and that is the ‘tax_query’ (WordPress Codex). But just for those readers, who didn’t take my advice to read the previous articles, let me briefly explain the $grid_args_tax (for those who know the basic args, skip the next paragraph).

‘features’ refers to the number of featured posts in the grid loop. So if you want to display 10 but feature 2, you would set this number to 2, posts_per_page to 10 and Genesis will do the math to ensure that everything is kosher. However, in our example, we want zero features for the taxonomy listing. ‘feature_image_size’ and ‘grid_image_size’ are the post thumbnail size that you want the image to appear as. ‘feature_image_class’ and ‘grid_image_class’ refer to the CSS image class for styling. ‘feature_content_limit’ and ‘grid_content_limit’ refers to the number of characters allowed in the content. See the StudioPress articles for futher explanations.

Now ‘tax_query’ refers to the taxonomy args. It must take an array of arrays of the following args:

  • taxonomy (string): the taxonomy
  • field (string): select taxonomy term by ‘id’ or ‘slug’
  • terms (int/string/array): taxonomy terms
  • operator (string): ‘IN’, ‘NOT IN’, ‘AND’

In our example, we have the template for a predetermined specific taxonomy and the template dynamically grabbing the appropriate taxonomy term (so someone may be searching for a ‘Fiction’ Book Type). If you wanted, it could also fetch the taxonomy dynamically, if you wanted all of your taxonomy archives to be on the grid. To do this, simply add the following code: $taxonomy = get_query_var( 'taxonomy' );. For tags and categories, you probably could use this same method; however, there is also a simpler method which I will explain in a later post (Tags and Same Categories and Different Categories).

So, after you set the post type (‘post’, ‘page’, or some other custom post type registered name, e.g., ‘wps_books’) and the taxonomy (which is the same as the latter half of the file name, so in this example ‘book_type’), the function first grabs the slug of the term: $term = get_query_var( 'term' ); (more information: get_query_var). Then it calls for the genesis custom grid loop.

Now obviously, with this custom post type, I am going to want to determine my own grid loop content. To do this, I simply make these additions:

 <?php
add_action('genesis_before_post', 'wps_custom_grid');
function wps_custom_grid() {
	remove_action('genesis_post_content', 'genesis_grid_loop_content');
	add_action('genesis_post_content', 'wps_grid_loop_content');
}

function wps_grid_loop_content() {
global $_genesis_loop_args, $wp_query;
//do something
}

For an excellent post on customizing the content, see Bill Erickson’s post, Customizing the Genesis Grid Loop Content.

Some other good grid functions include the following:

<?php
// Add some extra post classes to the grid loop so we can style the columns
add_filter( 'genesis_grid_loop_post_class', 'wps_grid_loop_post_class' );
/**
 * Add some extra body classes to grid posts.
 *
 * Change the $columns value to alter how many columns wide the grid uses.
 *
 * @author Gary Jones
 * @link http://dev.studiopress.com/genesis-grid-loop-advanced.htm
 *
 * @global array $_genesis_loop_args
 * @global integer $loop_counter
 * @param array $classes
 */
function wps_grid_loop_post_class( $grid_classes ) {
    global $_genesis_loop_args, $loop_counter;

    // Alter this number to change the number of columns - used to add class names
    $columns = 3;

    // Only want extra classes on grid posts, not feature posts
    if ( $loop_counter >= $_genesis_loop_args['features'] ) {

        // Add genesis-grid-column-? class to know how many columns across we are
        $grid_classes[] = sprintf( 'genesis-grid-column-%s', ( ( $loop_counter - $_genesis_loop_args['features'] ) % $columns ) + 1 );

        // Add size1of? class to make it correct width
        $grid_classes[] = sprintf( 'size1of%s', $columns );
    }
    return $grid_classes;
}

// Make sure the first page has a balanced grid
add_action( 'genesis_loop', 'wps_bal_grid_loop' );
function wps_bal_grid_loop() {
    global $query_string, $paged, $grid_args, $_genesis_loop_args;

    if ( 0 == $paged )
        // If first page, add number of features to grid posts, so balance is maintained
        $grid_args['posts_per_page'] += $grid_args['features'];
    else
        // Keep the offset maintained from our page 1 adjustment
        $grid_args['offset'] = ( $paged - 1 ) * $grid_args['posts_per_page'] + $grid_args['features'];
}
post

How to Find Out What Functions Do In WordPress (& Elsewhere) Using Text Editor like Notepad++

So it became apparent to me as I kept asking people and searching the WordPress Codex where such and such function was defined or used. Now, I have a process that I have refined over time that I want to share. It may not be the best process but it works and works well.

First, as with any web design or development, you need a good text editor. I use Notepad++ since I haven’t forked over for a Mac (anyone who wishes to buy me one, please feel free!). Plus Notepad++ is free!

So for example, say I am looking over nav-menus.php from the WordPress core. On line 67, I come across a function called wp_get_object_terms(); So if I want to know what this is, I do one of two things or sometimes both.

  1. First, search the WordPress Codex.
  2. Second, search the WordPress Core Code (online or off).

In this example, the Codex has good information. However, sometimes (though rare) you will see that the Codex is not completely filled out or brought up-to-date or easy to understand without seeing the function.

With the second option, I typically used Yoast’s PHPXref (ensuring that I am searching the correct WordPress version) until I was on a plane recently and refused to pay the $10 bucks for wifi since I had everything on my laptop. So then I turned to my text editor. (In the past, I used Windows XP with indexing to find such files; however, I’m now running Windows 7 and the search functions are horrible [and I haven't taken the time to try to figure out what's wrong with Windows 7 search ability or how to improve it]).

So if you open Notepad++, the third menu item is Search. If you click Find in Files a search menu will appear.

Finding WordPress Functions with NotePad++

Click Image for Larger Image

If you hit Ctrl+F as I often do, then select the tab that says Find in Files. Fill in function wp_get_object_terms in the Find what: input box, since I want to find where it’s defined to see its accepted variables so I can use it. Then I select the file structure that I want it to search, so you could select the entire WordPress install so it searches wp-admin, wp-includes, and wp-content or just pick one of those folders.

Finding WordPress Functions with NotePad++

Click Image for Larger Image

A quick search reveals that this function is defined in taxonomy.php in the wp-includes folder. The great thing about the search is that it typically returns the entire line that its found, so NotePad++ told me what I was looking for:

function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {

.

Finding WordPress Functions with NotePad++

Click Image for Larger Image

So if I want to see the entire function to try to determine what the function does or returns if anything, I need to open the file. While Notepad++ finds the function, it’s not a simple click and open that file. So I have to navigate to the file, open it and go to line 1778 to see the full function. One of the great things about WordPress is that there are a lot of comments informing the user what the function does. For this one particular function, there are 30 lines about this function including the following vital information:

 * @package WordPress
 * @subpackage Taxonomy
 * @since 2.3.0
 * @uses $wpdb
 *
 * @param int|array $object_ids The ID(s) of the object(s) to retrieve.
 * @param string|array $taxonomies The taxonomies to retrieve terms from.
 * @param array|string $args Change what is returned
 * @return array|WP_Error The requested term data or empty array if no terms found. WP_Error if $taxonomy does not exist.

This tells me that $object_ids is either an integer or an array of integers; $taxonomies is a string or an array of strings, and $args is an array of strings that can affect what is returned. It also tells me that this function returns an array or a WP_Error message.

Finding WordPress Functions with NotePad++

Click Image for Larger Image

 

So now, there’s an offline version or way to search your WordPress files for function calls, function definitions, etc.