Community Forums › Forums › Archived Forums › General Discussion › How to display a Custom Field value in archive
Tagged: Archived Content, Custom fields
- This topic has 5 replies, 2 voices, and was last updated 9 years, 3 months ago by mikeferg.
-
AuthorPosts
-
January 3, 2015 at 10:09 am #135930mikefergMember
Hello All,
I am very new to Genesis and I am just starting to get a feel for it.
So far I have found out how to display my posts as a grid and also how to have the post title display below the post thumbnail on archived pages.
Here is the question: I have added a custom field to my post called 'price'. How would get this custom field to show with archived content? So that each post in the archive grid will have 'Thumbnail', 'Title' and 'Price'.
Thanks in advance for any help.
http://www.starg8.co.uk/genesis/
MikeJanuary 3, 2015 at 12:58 pm #135937Ren VenturaMemberYou could do something like add it to the Post Info. If that doesn't work, see the Visual Hook Guide for the various areas you can hook into. Also, since you only want it to display on the archive, you can use is_post_type_archive().
http://my.studiopress.com/snippets/post-info/
http://genesistutorials.com/visual-hook-guide/blog/
http://codex.wordpress.org/Function_Reference/is_post_type_archive
Web & Software Developer & Blogger | RenVentura.com | Follow Me on Twitter @CLE_Ren
January 4, 2015 at 4:49 am #135994mikefergMemberHi Ren, thanks for the reply.
I worded my question poorly. The custom field needs to display in both post and post archive.
I have got it to display in the post by adapting this bit of code and placing it in child functions.php:
add_action( 'genesis_entry_header', 'custom_field_before_content', 15 );
/**
* @author Brad Dalton
* @example http://wpsites.net/
* @copyright 2014 WP Sites
*/
function custom_field_before_content() {
if ( is_singular(array( 'post', 'page')) && genesis_get_custom_field('price') ) :
echo '<div class="price">£ '. genesis_get_custom_field('price') .'</div>';
endif;
}I am now looking for a similar bit of code to echo a custom field in a post archive. That is where I am stuck now.
I followed your links but was unable to find the solution I am looking for. I am still getting to grips with how Genesis does things so the solution may well be staring me in the face but I just can't see it.
Cheers
MikeJanuary 4, 2015 at 2:38 pm #136055Ren VenturaMemberAh, okay. Use get_post_type() instead of is_singular(). For example:
if ( 'post' == get_post_type() ) { // Code }
get_post_type() will return true if whatever being shown is either singular or an archive. Basically, it is equal to:
if ( is_singular( 'post' ) || is_post_type_archive( 'post' ) )
Web & Software Developer & Blogger | RenVentura.com | Follow Me on Twitter @CLE_Ren
January 5, 2015 at 11:38 am #136117mikefergMemberThanks Ren, I think this topic can close now.
January 6, 2015 at 8:58 am #136187mikefergMemberRen, I forgot to post up my block of code should anyone else wish to do something similar.
// Adds custom field "price" to single posts and post archives
add_action( 'genesis_entry_header', 'custom_field_before_content', 15 );
function custom_field_before_content() {
if ( 'post' == get_post_type() )
echo '<div class="price">£ '. genesis_get_custom_field('price') .'</div>';
} -
AuthorPosts
- The topic ‘How to display a Custom Field value in archive’ is closed to new replies.