WordPress.org

Ready to get started?Download WordPress

Forums

Advanced Custom Fields
[resolved] Field output first tests false, then is displayed (11 posts)

  1. Z-247
    Member
    Posted 1 year ago #

    This one is weirding me out.

    I have two nearly identical functions:

    function GetSubTitle(){
    	$subTitle = get_field('sub-title');
    	if (!empty($subTitle)){
    		$output .= '<h3>'.$subTitle.'</h3>';
    		return $output;
    	} else {
    		//No subTitle, so skip the whole thing
    	}
    }

    and

    function GetGuestAuthor(){
    	$GuestAuthor = get_field('guest_author_name');
    	if (!empty($GuestAuthor)) {
    		echo '<span class="byline">'.$GuestAuthor.'</span>';
    	} else {
    		//No Author, so skip the whole thing
    	}
    }

    They both work great in most of the places I'm using them in my template. However, when using these near each other, things get weird. GetSubTitle() is testing false, GetGuestAuthor() tests true and outputs correctly, and then GetSubTitle()'s output shows up. Here's the template code:

    <h1><?php the_title(); ?></h1>
    
    <?php GetSubTitle(); ?>
    
    <?php if(in_category('articles')){ ?>
         <div class="article-details">
              <small><?php GetGuestAuthor(); ?></small>
              <small class="archive-dateline"><?php echo get_the_time('F jS, Y') ?></small>
         </div>
    <?php  } ?>

    Which then outputs:

    <h1>Title</h1>
         <div class="article-details">
              <small><span class="byline">Guest Author Name</span></small>
              <small class="archive-dateline">June 20th, 2012</small>
         </div>
    
    <h3>Article Sub-Title</h3>

    I've tried switching the GetSubTitle() function around a bit, which didn't do much. When I had it echo something during the else condition, it showed that right below the <h1> title, and still showed the requested string down below. I've also tried it with return instead of echo:

    $output .= '<h3>'.$subTitle.'</h3>';
         return $output;

    when testing true, but that didn't make difference.

    Any ideas out there? I'm nearly to the point where I'm going to cheat them back into order with CSS, although I'd obviously prefer not to.

    Thanks!

    Z-247

    http://wordpress.org/extend/plugins/advanced-custom-fields/

  2. Marventus
    Member
    Posted 1 year ago #

    Hi there.
    You seem to be using the Advanced Custom Fields plugin for fetching your custom field values.
    I never looked at the plugin's code, but if it works the way I suspect it does (by creating secondary loops) you could try adding a reset at the end of each function, like so:

    function GetSubTitle(){
    	$subTitle = get_field('sub-title');
    	if (!empty($subTitle)){
    		$output .= '<h3>'.$subTitle.'</h3>';
    		return $output;
    	} else {
    		//No subTitle, so skip the whole thing
    	}
            wp_reset_query();
    }
    function GetGuestAuthor(){
    	$GuestAuthor = get_field('guest_author_name');
    	if (!empty($GuestAuthor)) {
    		echo '<span class="byline">'.$GuestAuthor.'</span>';
    	} else {
    		//No Author, so skip the whole thing
    	}
            wp_reset_query();
    }

    If that doesn't work, try fetching your custom fields through WP functions such as get_post_meta (link) inside the main loop, see if it makes a difference.

  3. Z-247
    Member
    Posted 1 year ago #

    Thanks, Marventus. You are correct about using Advanced Custom Fields. Unfortunately, the reset query didn't do it, and neither did the get_post_meta idea, assuming the following implementation looks correct:

    function AltGetSubTitle(){
    	$pageID = get_the_ID();
    	$subTitle = $post_meta_cache[$pageID]['sub-title'];
    	echo '<h3>'.$subTitle.'</h3>';
    }
    
    function AltGuestAuthor(){
    	$pageID = get_the_ID();
    	$GuestAuthor = $post_meta_cache[$pageID]['guest_author_name'];
    	echo '<span class="byline">'.$GuestAuthor.'</span>';
    }

    Actually, this produces a new result: The subTitle is displayed after the Guest Author line in HTML, but now the GuestAuthor comes up blank... I then tried this new variation of the subTitle with the earlier $GuestAuthor = get_field('guest_author_name'); version of GuestAuthor, and while the Author is displayed again, the subTitle still comes in in the wrong place.

    Any clues there?

  4. Marventus
    Member
    Posted 1 year ago #

    Hi again.
    The following worked for me, all in single post view of course:

    1. Inside the loop (single.php template):

    $custom_field = get_post_meta($post->ID,'test_field', true);
    echo '<p>'.$custom_field.'</p>';

    2. Outside the loop:
    a. From a widget:

    <?php
        global $post;
        $custom_field = get_post_meta($post->ID,'test_field', true);
        echo '<p>'.$custom_field.'</p>';
    ?>

    b. From functions.php as a filter:

    function display_custom_field($content){
        global $post;
        $id = $post->ID;
        $custom_field = get_post_meta($post->ID,'test_field', true);
        $content .= '<p>'.$custom_field.'</p>';
        return $content;
    }
    add_filter('the_content', 'display_custom_field');

    You would of course need to replace test_field with the name of the custom field you are trying to retrieve.
    I hope that helps!

  5. Marventus
    Member
    Posted 1 year ago #

    I realized my example was not good enough because your issue seems to occur while retrieving too different custom fields, so I tried each example with two fields and they all worked:

    1. Inside the loop:

    $field1 = get_post_meta($post->ID,'test_field', true);
    $field2 = get_post_meta($post->ID,'test_field_2', true);
    echo '<p>'.$field1.'<br/>'.$field2.'</p>';

    2. Outside the loop:
    a. From a widget:

    global $post;
    $field1 = get_post_meta($post->ID,'test_field', true);
    $field2 = get_post_meta($post->ID,'test_field_2', true);
    echo '<p>'.$field1.'<br/>'.$field2.'</p>';

    b. From functions.php:

    function userf_custom_fields($content) {
        global $post;
        $field1 = get_post_meta($post->ID,'test_field', true);
        $field2 = get_post_meta($post->ID,'test_field_2', true);
        $content .= '<p>'.$field1.'<br/>'.$field2.'</p>';
        return $content;
    }
    add_filter('the_content', 'userf_custom_fields');
  6. logikal16
    Member
    Posted 1 year ago #

    @Z-247 - Your code shows the subtitle field's name being "sub-title". Are you sure it's a dash and not an underscore?

    Also, you're right in using get_field. Please do NOT use get_post_meta when retrieving ACF values. Otherwise, any custom field formatting (e.g. for date fields) won't get applied.

  7. Marventus
    Member
    Posted 1 year ago #

    @logikal16:
    The code I provided the OP with was clearly a test to see if the information was retrieved or not outside of the (your?) plugin, and not a way to retrieve ACF values.
    This is, as you know, the only way to determine whether his issue is being generated by ACF or by WP. Once the OP determines this, he will be able to make an informed decision about how to solve it.
    Cheers!

  8. logikal16
    Member
    Posted 1 year ago #

    @Marventus - No need to fray, my last comment wasn't directed at you.

    I never looked at the plugin's code, but if it works the way I suspect it does...

    My advice was based on experience. If you have 1 ACF problem and you try to (permanently) fix it with get_post_meta, you'll end up with 2 problems.

  9. Marventus
    Member
    Posted 1 year ago #

    No need to fray

    All is good.

    my last comment wasn't directed at you.

    You were referencing my last suggestion and instructing the OP not to implement it, correct? This is later confirmed by the second part of your reply.
    That's what I understood and that is why I provided an explanation as to why I had suggested that. I also think that using get_post_meta() is the most straight forward way of testing whether there is smth wrong with the plugin or not.
    Cheers!

  10. Z-247
    Member
    Posted 1 year ago #

    Sorry for the delayed reply here; I was off the grid for a couple days. I think I've cleared this up, and it may have all been my fault.

    I've tested the 'outside the loop' code from Marventus (version 2a), using sub-title as test_field/$field1 and guest_author_name as test_field2/$field2. The result is that $field1 comes back as nothing (no space, etc.) and $field2 displays as expected.

    I've also confirmed that sub-title is indeed hyphenated, and not with an underscore.

    When double-checking the ACF settings, I noticed that my test subtitle wasn't matching what I expected for my test Post. The subtitle displayed after the guest author output, the 'out of order' one, had actually been inserted into the content, not a custom field. So while I'd been seeing an expected subtitle in my HTML output, it was coming from dummy text entered over a month ago (which nicely explains why the custom field calls were failing!)

    Very sorry for the wild goose chase there- I wish I'd caught that earlier, but really appreciate both of your efforts to help me pin this down. If only there were a tip jar around here for you guys.

    Z-247

  11. Marventus
    Member
    Posted 1 year ago #

    Hi, Z-247,
    Glad you were able to make it work.
    For my code to have worked you would have had to add test custom fields to your posts, but it's completely irrelevant now.
    Happy blogging!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic