Help with displaying Custom Field in a post

Fiona

New Member
#1
Hi. I am using Gravity Forms and the Gravity Forms + Custom Post Types plugin to enable a form to be used to submit testimonials on a website that automatically creates a post for the testimonial. I have used the body post field for the testimonial itself, but to make it easy to use have used a custom field ("testimonialauthor") to submit the name/company/location of the person submitting the form. Ideally I would like this to display under the body text in excerpts on the testimonial archive page and in a Genesis Featured Post plugin.
I have added the following code to my Custom Functions:
add_action( 'genesis_after_content', 'add_testimonial_author' );
function add_testimonial_author() {
echo get_field('testimonialauthor');';
}​
However this breaks the site so I have obviously got it wrong! Can anyone help?
 

eric

Administrator
Cobalt Apps Developer
#2
Simply looking at your code I see what's probably breaking your site. You need to remove the trailing '; code at the end of your echo. So it should look like this:
PHP:
add_action( 'genesis_after_content', 'add_testimonial_author' );
function add_testimonial_author() {
echo get_field('testimonialauthor');
}
 

Fiona

New Member
#3
Thanks for this Eric. It is no longer broken but still doesn't work!
I have tried loads of different code, but can't get the custom field to display either in the single post or on the archive post.
I currently have:
PHP:
add_action( 'genesis_post_content', 'add_testimonial_author' );
function add_testimonial_author() {
    if (is_archive(testimonials)) {
echo get_post_custom_values( 'testimonialauthor');
}
}
But this doesn't display the custom field under my post content so something is obviously still incorrect!
I admit to being a PHP novice but am trying to learn - any help very gratefully received!
 

Fiona

New Member
#5
Thanks for the suggestion Andytc, but that still doesn't resolve it :(
The post will only appear on the archive page of testimonials, not as an individual post, but at the moment it's not working either on the post or on the archive page.
 
#6
Have you tried echoing out some plain text first to see if it's the conditional that's not working or the way you are getting the field data that isn't working ?
 
#8
You can try this to investigate

PHP:
add_action( 'genesis_post_content', 'add_testimonial_author' );
function add_testimonial_author() {
    if ( is_post_type_archive('testimonials') ) {
echo 'The conditional works';
}
}
 
#10
Ok , so try changing the hook to this -

PHP:
add_action( 'genesis_entry_content', 'add_testimonial_author' );
function add_testimonial_author() {
    if ( is_post_type_archive('testimonials') ) {
echo 'The conditional works';
}
}
 
#12
Ok , you need to double check the name of the archive you’re looking at , check it’s ‘testimonials’ (plural) and not ‘testimonial’ (singular)

You can find the template name being served using dev tools in the browser or check the url , as I know for sure that works for a CPT archive , so something at your end isn’t correct.
 

Fiona

New Member
#13
It's definitely 'testimonials' - here is the link to the category archive: http://ltplast.fionastoreydesign.co.uk/category/testimonials/
(this is a temporary test site). The first testimonial is one I inputted via my
I tried deactivating all the plugins and it still didn't work, so none of them are causing a conflict.
I also tried removing all the custom css and the other php that I have added to custom functions but that made no difference either.
I am using the SmallBizPress Dynamik skin from Cobalt Apps. However I also tried changing to 2017 theme and that also made no difference.
Here is the code I have in custom functions:
PHP:
<?php
/* Do not remove this line. Add your functions below. */
add_action( 'genesis_entry_content', 'add_testimonial_author' );
function add_testimonial_author() {
    if ( is_post_type_archive('testimonials') ) {
echo 'The conditional works';
}
}

function post_grid_filter_grid_item_excerpt($excerpt){
    
    $post_id = get_the_id();
    $post_data = get_post($post_id);
    $post_excerpt = $post_data->post_excerpt;
    //var_dump($post_excerpt);
    if(empty($post_excerpt)){
        $post_excerpt = get_the_excerpt($post_id);
    }
    return wpautop($post_excerpt);

}

add_filter('post_grid_filter_grid_item_excerpt','post_grid_filter_grid_item_excerpt');
 
#14
Ok , Excellent , now we know this is a category archive and we should use either method below - In the first example we'll use the category ID , the other is using the category name.

PHP:
add_action( 'genesis_entry_content', 'add_testimonial_author' );
function add_testimonial_author() {
    if ( is_category( '2' ) ) { // testimonials category id
echo 'The conditional works';
}
}
PHP:
add_action( 'genesis_entry_content', 'add_testimonial_author' );
function add_testimonial_author() {
   if ( is_category( 'testimonials' ) ) { // testimonials category name
echo 'The conditional works';
}
}

We can always find the correct conditional by looking at the body tag with DEV tools in the browser - as below

in Chrome - Ctrl + Shift + I

IE - F12

Look for the body Tag

cat-name.png

So hopefully that will work and you can start working on the output of the custom field :)
 

Fiona

New Member
#15
Thanks Andytc. That worked for 'The conditional works', but when I put in:
Code:
add_action( 'genesis_entry_content', 'add_testimonial_author' );
function add_testimonial_author() {
    if ( is_category( '2' ) ) { // testimonials category id
echo get_post_custom_values( 'testimonialauthor');
}
}
it returns 'Array'
 
#16
Ok , try the code below - This is for a Wordpress custom field

First we check we are on category 2 ( testimonials category id )

Then we check that the field has an entry ( !empty ) , if so we display it in the entry_footer hook with a p class for styling , if no entry then nothing gets displayed so we don't have any empty <p>'s anywhere

Change the hook to suit your needs - genesis_entry_footer , genesis_entry_content etc , etc

PHP:
add_action( 'genesis_entry_footer', 'add_testimonial_author' );
function add_testimonial_author() {
    if ( is_category( '2' ) ) { // testimonials category id
        $my_custom_meta_value = get_post_meta( get_the_ID(), 'testimonialauthor', true );
        if  (!empty( $my_custom_meta_value )) {      
        echo '<p class="testimonial-author"> '.$my_custom_meta_value.'</p>';
        }
        else {}
    }
}
Some simple CSS to get started -

CSS:
.testimonial-author {
 background: #ccc;
 padding: 8px;
 font-weight: 700;
}
 

Fiona

New Member
#17
Hi Andytc
That's perfect! The custom field now appears exactly as I want, and my css knowledge is much better than my php so I can sort out the styling! Thank you so much for all your help and for the explanations - I have learned a lot.:D:D:D:D