Community Forums › Forums › Archived Forums › Design Tips and Tricks › Post types with ACF and Genesis
Tagged: ACF, Custom fields, genesis, Post Type
- This topic has 9 replies, 3 voices, and was last updated 9 years, 8 months ago by Porter.
-
AuthorPosts
-
July 31, 2014 at 9:23 am #116444kristieMember
Hi Everyone. I'm customizing the Parallax theme and am working with the Advanced Custom Fields plugin an am noticing a difference in behaviour when running the code on a standard PHP page vs a Genesis PHP page.
Using Advanced Custom Fields, I have created a new custom field called Staff, with the following attributes:
name - text
location - Relationship to another custom field called location
email - Email
description - Wisiwyg EditorWhen displaying this information on my single-staff.php page, all fields are displaying correctly except for the location 'Post' field.
When accessing the Post object, the variable does not get populated with the Location Post. Instead, it still references the parent Staff post. Here is my code:
----------------remove_action( 'genesis_entry_header', 'genesis_post_info', 12 ); remove_action( 'genesis_sidebar', 'genesis_do_sidebar' ); remove_action( 'genesis_entry_content', 'genesis_do_post_content'); add_action('genesis_entry_content', 'ladc_do_post_content'); function ladc_do_post_content() { $posts = get_field('location'); if( $posts ): ?> <ul> <?php foreach( $posts as $post): ?> <?php setup_postdata($post); ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <span>Custom field from $post: <?php the_field('url'); ?></span> </li> <?php endforeach; ?> </ul> <?php wp_reset_postdata(); ?> <?php endif; ?> <?php } genesis();
----------------------
This results in the following output:Jane Smith Custom field from $post:
Jane Smith Custom field from $post:The title is the title of the Staff post not the Location post, and the attribute of url is blank. If I call the_field() on a Staff attribute, it returns a value.
I removed all Genesis related code to see what impact Genesis was having and ran this:
---------------$posts = get_field('location'); if( $posts ): ?> <ul> <?php foreach( $posts as $post): ?> <?php setup_postdata($post); ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <span>Custom field from $post: <?php the_field('url'); ?></span> </li> <?php endforeach; ?> </ul> <?php wp_reset_postdata(); ?> <?php endif; ?> <?php }
--------------
This resulted in the expected information from the Location post.
Has anyone used ACF with Genesis to access Post Types? The same behaviour occurs if I set the 'location' type to be Post instead of 'Relationship'. Is there another way to access a Post object when using Advanced Custom Fields with Genesis? Is there different plugin that is recommenced for Custom Field creation on Genesis? or do I need to code them from scratch?
http://localhostAugust 1, 2014 at 7:27 am #116562David ChuParticipantHi,
I don't know the exact answer without being "in there" and tearing into code, but I can give you an idea.To me this looks like what happens when an experienced WP developer begins using Genesis. They're used to having separate template files for everything. and Genesis templates look funny. ๐
When I use CPT's from any source, I end up pulling out the Genesis loop and then making a custom loop. Here are a couple examples to give you ideas:
http://genesissnippets.com/genesis-custom-loop/
second oneDave
Dave Chu ยท Custom WordPress Developer – likes collaborating with Designers
August 3, 2014 at 11:42 am #116856kristieMemberThank you for your response David. You are spot on. I am quite used to the standard WP style, and the Genesis templates do look funny ๐ So bear with me.
I took a look a the examples you posted, and couldn't quite adjust them to fit my problem, so I'm not sure if I'm not doing something right, or if I'm not explaining my problem clearly enough.
I'm able to successfully pull data out of the Post type, except for when the field returns a Post Object.
The page this code is going on is for a single Post Item (i.e. One Staff Member). Within that Single post item, is a relationship to another Custom Post type, Location, which could contain multiple posts. (NOTE: I also tried this with a Single Post Type as the "Field Type", therefore removing any need for a loop and the same problem occurs).
I am able to access all the other fields from the Staff object, without a problem.
Let me try a different angle on the problem. I have made some adjustments to the setup and code, so instead of defining a Custom Field Type Group with the field set to "Relationship, which could have multiple Post Types attached, I have changed it to be a Field Type of "Post Object" so only one is returned.
New code:
remove_action( 'genesis_entry_content', 'genesis_do_post_content'); add_action('genesis_entry_content', 'ladc_do_post_content'); function ladc_do_post_content() { ?> <div> <h3><a href="<?php the_permalink(); ?>">Staff Title: <?php the_title(); ?></a></h3> <p>Staff Description: <?php echo get_field('description'); ?></p> <?php $post_object = get_field('location'); if( $post_object ): // override $post $post = $post_object; setup_postdata( $post ); ?> <h3><a href="<?php the_permalink(); ?>">Location Title: <?php the_title(); ?></a></h3> <p>Location Phone Number: <?php the_field('phone_number'); ?></p> </div> <?php wp_reset_postdata(); ?> <?php endif; ?> <?php } genesis();
The code to access the Location Post Object from the parent Staff object was pulled from here: http://www.advancedcustomfields.com/resources/field-types/post-object/
The result of this code displays the Staff Title and Description, as expected, but when displaying the Location object fields, it displays the title of the parent object (Staff) again and nothing for the field "phone_number" as it is only a valid field for the Location object, not for the Staff object. If I replaced that field with a valid Staff field, it would return successfully.
So it seems that the ACF sample code to access the Post Object and call the setup_postdata() on it, does not work when running through the Genesis engine, and therefore the original Staff Object is not being overwritten.
I hope this helps clarify the issue. Please let me know if I can provide any further information.
Cheers, Kristie
August 3, 2014 at 4:57 pm #116875PorterParticipantI'm not really experienced enough to help with this exact issue, but I'm going to go ahead and post my most recent code using ACF. I very recently (last week) set this up, so perhaps something in my code will help you.
<?php /** * Template Name: Directory Listing * Description: Used for "directory" pages. */ function location_category_loop() { //Set the name of the page (Bars, Restaurants, etc), to a variable, so we can use it later to find all pages with the "location_category" custom field set to the same name. $this_page_title = get_the_title(); $args = array( 'sort_order' => 'ASC', 'sort_column' => 'post_title', 'meta_query' => array( array( 'key' => 'location_category', 'value' => $this_page_title, 'compare' => 'LIKE' //Checks the custom fields array of "location_category" to see if the page name (variable we saved earlier) is a match to any of the values in the array. ) ), 'post_type' => 'page', 'post_status' => 'publish' ); $pages = get_posts($args); ?> <div class="entry"> <?php if($pages) { foreach ($pages as $page) { ?> <div class="full-width-post" id="post-<?php $page->ID; ?>"> <?php echo(get_the_post_thumbnail($page->ID, array(320,180), array("class" => "post_thumbnail"))); ?> <div class="directory-title"> <h2><a href="<?php echo(get_permalink($page->ID)); ?>" rel="bookmark" title="<?php echo(get_the_title($page->ID)); ?>"><?php echo(get_the_title($page->ID)); ?></a></h2> </div> <div class="city-icon"> <?php //Grab the value for the custom fields "city" field, and compare it against known values to determine which city icon to show (Burlington, South Burlington, Winooski, Essex, etc). $testing = get_field('city', $page->ID); switch ($testing) { case 'burlington': ?><img src="http://anightinburlington.com/wordpress/wp-content/themes/genesis-anib/images/city-icon-burlington.png" /><?php break; case "south-burlington": echo("the location is south burlington"); break; case "winooski": ?><img src="http://anightinburlington.com/wordpress/wp-content/themes/genesis-anib/images/city-icon-winooski.png" /><?php break; default: echo("the location is default"); } ?> </div> <div class="directory-entry"> <?php echo(get_post_meta($page->ID, '_yoast_wpseo_metadesc', true)); ?> </div> </div> <?php } } ?> </div> <?php wp_reset_postdata(); } remove_action( 'genesis_loop', 'genesis_do_loop' ); add_action( 'genesis_loop', 'location_category_loop' ); genesis(); ?>
Basically what this is, is a custom template, with a custom loop. I'm looping through my pages (not posts), and grabbing all pages that contain the same page name (Bars, Restaurants, etc) as the ACF field of "location_category". I also check for which "city" is used, and use a case switch statement to display an icon for that city.
Kind of shooting in the dark as to if this will help you or not, but I had a few issues myself, and this is what I ended up with. Hope it helps, if not, happy hunting ๐
August 3, 2014 at 8:08 pm #116916kristieMemberThanks Porter! I will definitely bookmark this code to help me in future, but I don't think it's quite the problem I'm having right now.
I have a Custom Post type (Staff) that has another Post Type (Location, also custom) as an attribute. While I can access the parent Post type I'm unable to get the attributes in the child Post Type, using the sample code that ACF provides.
It would be closer if instead of a text city field on your Page, you assigned a 'location' Custom Post Type, which would then contain information such as city, etc. I'm getting all the parent attributes OK, but can't access the child Post Type attributes within it.
Staff Custom Field
- Name (text)
- Position (text)
- Location (Post Type) --> Location Custom FieldLocation Custom Field
- Map (Google Map)
- Logo (Image)
- Phone Number (text)If accessing the Location Custom Field independently, I can access all elements. Only when I access it as a child custom field from Staff, does it not behave as expected.
I hope this adds some clarity.
August 4, 2014 at 8:47 am #116999David ChuParticipantKristie,
That's quite a complex situation. Again, not being in your system, I'm flying mostly blind. And something as complex as dealing with 2 types of CPT's together, AND in a plugin that I don't use... though I'm happy to point someone in a direction, there are now enough difficulties and details that it would exceed the bounds of my free help, I'm afraid.Good luck, Dave
Dave Chu ยท Custom WordPress Developer – likes collaborating with Designers
August 4, 2014 at 1:58 pm #117044kristieMemberThanks Dave. It is a strange one, and one quite specific to Advance Custom Fields Plugin. I'm hoping that someone else has experienced the same thing and has some advice, rather than someone to fix it for me. I'm also open to advice on the best practices around creating custom post types and using Genesis. Is there another Plugin people would recommend? Worst case I go back and remove the plugin and build them from scratch.
August 4, 2014 at 2:43 pm #117052PorterParticipantACF also has a decently active forum, have you tried posting there as well?
So if I'm understanding, you essentially can't access the attributes of your custom post type? You can go one level deep, but not two?
If so, have you tried echoing the first object, and the attributes separately? If so, what do you get?
August 4, 2014 at 3:58 pm #117059kristieMemberHey Porter.
I have posted on the ACF forum as well. I'll see what comes back there.
You would think I'd tried echoing out the attributes, but I don't think I had, so thanks for the tip! The location attribute (ie, child custom post type) is returned as an array. And it has all the data in it! So worst case now comes down to me iterating through that array to get the datapoints I need.
I think there is something funky going on with Genesis and the setup_postdata( $post ) method. As mentioned, when I remove all the genesis stuff, the code works perfectly. So I'll move ahead with iterating through the array, as I think that's going to be the most viable solution for now.
Thank you!
August 4, 2014 at 4:01 pm #117060PorterParticipantNot a problem! I primarily do game development for web (Flash, Unity), but I've been teaching myself the specifics of PHP / WordPress as of late. I too had a bit of trouble when dealing with arrays as a returned object, and didn't realize it was potentially specific to Genesis.
I'd love for someone experienced to clear that up, as it's confusing to at least a number of us.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.