Community Forums › Forums › Archived Forums › Design Tips and Tricks › Custom Meta Box and Styling
- This topic has 13 replies, 2 voices, and was last updated 10 years, 8 months ago by David Chu.
-
AuthorPosts
-
June 25, 2013 at 2:59 pm #47711joyvMember
Hi all,
I am working on a site that we created custom post types with custom meta boxes on. We added the code to the functions file to display the meta boxes on the back-end but need help adding them to the single post template and wrapping them in css and also showing the field titles.
I have had no luck researching this and any help would be appreciated!
remove_action('genesis_post_content', 'genesis_do_post_content'); add_action('genesis_post_content', 'my_own_fields', 1); function my_own_fields () { genesis_custom_field('myfield1'); genesis_custom_field('myfield2'); genesis_custom_field('myfield3'); } <div class="amw-one-half amw-first">Serves Alcohol</div><div class="amw-one-half"><?php get_custom_field('ecpt_amw_alcohol', TRUE); ?></div>
June 25, 2013 at 3:53 pm #47718David ChuParticipantHi,
I actually wrote a blog article about displaying custom fields in Genesis, but I didn't add any HTML, just the values. Some echo statements could take care of that part.As for the single post template, because I use Genesis I usually avoid using a new template file, instead relying on Genesis behind the scenes. I just use WP variables to see what kind of page I'm on, and only then conditionally pump out my custom fields. I would put something like this in functions.php, no template file change. I'm coding off the top of my head, so caution advised.
add_action('genesis_before_content', 'dc_check_page_type'); function dc_check_page_type() { if ( is_single() ) { remove_action('genesis_post_content', 'genesis_do_post_content'); add_action('genesis_post_content', 'my_own_fields'); } } function my_own_fields () { echo 'myfield1: ' . genesis_custom_field('myfield1'); echo 'myfield2: ' . genesis_custom_field('myfield1'); }
You can take it from there. (I think the editor mangled a few of my underscores... arggh).
Dave
Dave Chu 路 Custom WordPress Developer – likes collaborating with Designers
June 25, 2013 at 4:07 pm #47721joyvMemberThanks Dave! I'll give it a try.
June 25, 2013 at 4:42 pm #47726David ChuParticipantJoy,
You bet. 馃檪 Here it is again, and I saw why the underscores disappeared... when you edit a block of PHP, the editor sticks styling on the bits, and makes its own decisions. Can't tell I'm a coder, can you? 馃檪 To be sure, you can do new template files if you prefer that... just some Genesis conventions may be needed. In Carrie's article you'll see some of that. Note the "genesis()" command at the very bottom that ties in the framework.add_action('genesis_before_content', 'dc_check_page_type'); function dc_check_page_type() { if ( is_single() ) { remove_action('genesis_post_content', 'genesis_do_post_content'); add_action('genesis_post_content', 'my_own_fields'); } } function my_own_fields () { echo 'myfield1: ' . genesis_custom_field('myfield1'); echo 'myfield2: ' . genesis_custom_field('myfield1'); }
Dave Chu 路 Custom WordPress Developer – likes collaborating with Designers
June 25, 2013 at 5:17 pm #47731joyvMemberDave,
I inserted the above code making sure to update with my field names and ensure that all underscores are in place. Previewed a single post page and the fields are not displaying 馃檨
I would love to implement your solution but there is something I don't seem to understand or I am getting wrong.
June 25, 2013 at 8:44 pm #47739David ChuParticipantJoy,
I don't usually take it this far here, but it's an interesting case, and you asked nicely. 馃槈 I'll probably add this to my custom field article.My code actually ran OK even though it had mistakes. But the function echoed the value out, and I wanted the fieldname bit to show first. So I used the non-echo version of the function, and also corrected a couple typos. I also moved the first action to earlier in the Genesis sequence. It worked nicely, and I can't break it.
add_action('genesis_before', 'dc_check_page_type'); function dc_check_page_type() { if ( is_single() ) { remove_action('genesis_post_content', 'genesis_do_post_content'); add_action('genesis_post_content', 'my_own_fields'); } } function my_own_fields () { $junk1 = genesis_get_custom_field('myfield1'); $junk2 = genesis_get_custom_field('myfield2'); echo 'myfield1: ' . $junk1; echo ' myfield2: ' . $junk2; }
Interesting how if you copy from the block, you get the invisible underscores. Just typical WYSIWYG editor dung, I guess. 馃檪
Make sure your custom fields are populated for the Post you're testing (but you knew that!).
Also, when I'm viewing an archive, sometimes I forget that that's not "single", that is, a single Post.
You could also remove the block that checks for is_single - if you run it against "everything" and it works, then the ID isn't making it to the is_single test for some reason (like a manual SQL query can do that, if that's happening somewhere).
And obviously you'd want some conditional logic so you don't have field labels always showing up. That's going to be up to you. 馃檪
If none of that works, there may be some custom code in that theme that's bollixing the loop! Or maybe you're using a plugin for custom fields and it's not hooked up right.
D
Dave Chu 路 Custom WordPress Developer – likes collaborating with Designers
June 25, 2013 at 9:07 pm #47742joyvMemberYOU ARE A GENIUS!!! Absolutely amazing and beautiful!
The conditional logic is the cherry on top 馃檪
I have to say the best ppl are on the StudioPress forums!
June 25, 2013 at 9:12 pm #47743joyvMemberThis reply has been marked as private.June 26, 2013 at 7:39 am #47771David ChuParticipantThanks! 馃檪
I'm glad that helped. You'll find that the same idea can apply using the other hooks. Since you were willing to dive into hooks, you'll be WAY ahead of others at customizing/building sites! Between hooks and filters, the immense power of Genesis emerges.
Like the rest of us, when Genesis 2 comes out, you'll have some new hooks to learn for HTML5, but it's all similar principles, and I'm not worried about it at all. It will also be backward-compatible for those who don't want to tackle HTML5 yet.
Dave
Dave Chu 路 Custom WordPress Developer – likes collaborating with Designers
July 12, 2013 at 8:59 pm #50514joyvMemberConditional logic is the cherry on top but I can't figure it out! boo
Can you take a look and point me in the right direction?
This is what I have.....
/** Add meta fields to single post type template */ add_action('genesis_before', 'dc_check_page_type'); function dc_check_page_type() { if ( is_single() ) { add_action('genesis_post_content', 'my_own_fields'); } } function my_own_fields () { echo '<div class="ecpt_meta">'; $junk1 = '<div class="ecpt_field_content">' . genesis_get_custom_field('ecpt_productdescription') . '</div>'; $junk2 = '<div class="ecpt_field_content">' . genesis_get_custom_field('ecpt_brandname') . '</div>'; $junk3 = '<div class="ecpt_field_content">' . genesis_get_custom_field('ecpt_product-dimensions') . '</div>'; $junk4 = '<div class="ecpt_field_content">' . genesis_get_custom_field('ecpt_skunumber') . '</div>'; echo '<div class="ecpt_field_name">' . 'Product Description:' . '</div>' . $junk1; echo '<div class="ecpt_field_name">' . 'Manufacturer: ' . '</div>' . $junk2; echo '<div class="ecpt_field_name">' . 'Product Dimensions: ' . '</div>' . $junk3; echo '<div class="ecpt_field_name">' . 'Product SKU: ' . '</div>' . $junk4; echo '</div>'; }
July 12, 2013 at 9:01 pm #50515joyvMemberSorry, forgot to say what I am trying to do!
Product Description, Manufacturer, Product Dimensions, and Product Sku are all fields/Titles and if left blank I would like the title to not display. But how? I am lost.....
July 13, 2013 at 8:41 am #50540David ChuParticipantI re-orged it to use fewer fields. This is the basic idea, and then just make an IF block for each field. I'm sure there are fancier ways to code this, but it will do.
add_action('genesis_post_content', 'my_own_fields'); function my_own_fields() { if ( genesis_get_custom_field('crud') ) { echo 'title: ' . genesis_get_custom_field('crud'); } }
Enjoy, Dave
Dave Chu 路 Custom WordPress Developer – likes collaborating with Designers
July 15, 2013 at 7:27 pm #50899joyvMemberThank you so very, very much! I have learned a ton with the examples you have been able to provide.
Without your guidance we would have never figured this out! You are the bomb!
July 16, 2013 at 8:09 am #50943David ChuParticipantJoy,
It's been a joy. 馃檪 We'll see you around!All the best, Dave
Dave Chu 路 Custom WordPress Developer – likes collaborating with Designers
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.