Recently, someone asked me how I would create a Genesis Page Template with a Custom Post Type using Custom Fields. One of the great things about Genesis is its ability to easily pull custom fields content. Previously I mentioned how to create a WordPress Custom Post Type Page Template in Genesis. Now, what if I want to have this page template to display custom content via custom fields?
Well here's the right way to do it via Genesis hooks:
[php]
<?php
/*
*Template Name: Author Interview
*/
remove_action('genesis_loop', 'genesis_do_loop');
add_action('genesis_loop', 'custom_do_loop');
function custom_do_loop() {
$mycpt = 'books';
$posts_per_page = 10;
global $paged;
$args = array('post_type' => $mycpt, 'posts_per_page' => $posts_per_page);
genesis_custom_loop( $args );
}
remove_action( 'genesis_post_content' , 'genesis_do_post_content' );
add_action( 'genesis_post_content' , 'genesis_do_custom_post_content' );
function genesis_do_custom_post_content() { ?>
<div id="post-<?php the_ID(); ?>" class="book-info">
<h3>Book Information</h3>
<p class="book-description"><strong>Description</strong>: <?php echo genesis_get_custom_field('_type'); ?></p>
<p class="book-pages"><strong><?php echo genesis_get_custom_field('_type'); ?></strong>: <?php echo genesis_get_custom_field('_pages'); ?> pages </p>
<p class="book-publisher"><strong>Publisher</strong>: <?php echo genesis_get_custom_field('_publisher'); ?> (<?php echo genesis_get_custom_field('_pub_date'); ?>)</p>
</div><!--end .book-info -->
<div id="post-<?php the_ID(); ?>" class="book-review">
<?php the_content(); //OR, the_excerpt(); OR, the_content_limit( (int)genesis_get_option('content_archive_limit'), __('[Read more...]', 'genesis') ); ?>
</div><!--end .book-review -->
<?php
}
genesis();
?>
[/php]
Scott says
Hello, and thanks for sharing this. One question: how do I add the page title to this? I tried removing the post title and creating a custom post title, but that adds it to every entry. Thanks, Scott.
Travis Smith says
Hello Scott,
You will need to add the page title to the genesis_before_loop hook, so it appears outside the hook as a page title.
Thanks,
Travis
chuck says
Thank you so much for your great tutorials. I have this working great. But, what I would like to do is filter this by taxonomy or category without creating a separate template. I have looked for how to do this and I cant seem to find the solution.
I have a page, I am selecting the template to use. But how to I tell the page to filter just one category or taxonomy? I have tried through urls, query_args, adding categories to the page, nothing seems to work. Maybe you can just point me in the right direction. Thank you very much for any help you can give.
thanks,
chuck
Travis Smith says
Hello Chuck,
What do you mean filter by taxonomy or category? Not really following you there.
David Hilditch says
I have the same problem as chuck – see the following two links – they produce the SAME output.
I want to use the grid loop but I want it to behave like the standard loop in that it should pull back only the relevant posts based on the /category/slug/ and the ?tag=xxx.
e.g. http://www.fitness-saver.com/category/buying-guides/?tag=cycling-2 should be different to http://www.fitness-saver.com/category/buying-guides/?tag=football but they’re not 🙁
See the problem? The grid loop shows the same content regardless of which category the page is on. There must be something fundamental both myself and Chuck are not getting.