- This topic has 5 replies, 2 voices, and was last updated 2 years, 7 months ago by Elvin.
-
AuthorPosts
-
September 13, 2021 at 1:35 pm #1929368Jeff
I have a custom post type (“chapter”, as in membership chapter); am using ACF for custom fields.
The issue I’m having is with the archive for the CPT.
I’m using a child theme. I cloned “archive.php” and named it “archive-chapter.php” in the child theme directory.
With one exception, the information is displaying just fine.
The exception is that the rendering of the custom field data is happening OUTSIDE of the “article” tag that displays the entry header, entry title, and permalink.
I kept the line: “generate_do_template_part( ‘archive’ );”
My problem occurs whether my custom fields rendering code is just above or just below that line.
I hope this makes sense. If so, what can I do to have the custom fields rendered WITHIN the article in which the entry title appears?
September 13, 2021 at 10:48 pm #1929678ElvinStaffCustomer SupportHi Jeff,
Can you share w/ us how you’re adding the ACF on the template? Is it coded manually within the child theme template?
Or perhaps hooked in using a Hook Element? Let us know.
September 14, 2021 at 6:41 am #1930096JeffIt is coded manually within the child theme template. I’m using WordPress
get_post_meta
, not the ACF functions.Full file is here in pastebin
September 14, 2021 at 7:56 pm #1930820ElvinStaffCustomer SupportThis is the one incharge for the
<article>
tags inside the loop. https://github.com/tomusborne/generatepress/blob/b60b853630da6d9015722da903e53c8064148b0a/content.phpAnd your code is outside of it so it will be rendered outside of the article tags.
If I may suggest:
There’s no need to create a child theme template for this if you simply want to add more post meta items on the post articles on an archive page.
I believe a Hook Element will suffice.
Try adding this code (one taken from your file) to a Hook Element’s code area.
/* Get the metas */ $location = get_post_meta($post->ID, 'location', true); $contact_person = get_post_meta($post->ID, 'contact_person', true); $contact_telephone = get_post_meta($post->ID, 'contact_telephone', true); $contact_email = get_post_meta($post->ID, 'contact_email', true); $href_contact_email = '<a href="mailto:' . $contact_email . '">'; $monthly_meeting = get_post_meta($post->ID, 'monthly_meeting', true); $notice = get_post_meta($post->ID, 'notice', true); <div class="acf"> <div class="location"><span class="label">Location:</span><br /> <span class="address"><?php echo $location;?></span></div> <div class="contact_person"><span class="label">Contact Person:</span><?php echo $contact_person;?></div> <div class="contact_telephone"><span class="label">Contact Telephone:</span><?php echo $contact_telephone;?></div> <div class="contact_email"><span class="label">Contact Email:</span><?php echo $href_contact_email . $contact_email . '</a>';?></div> <div class="monthly_meeting"><span class="label">Monthly Meeting:</span><?php echo $monthly_meeting;?></div> <?php if ( !empty($notice) ) : ?> <div class="notice"><?php echo $notice;?></div> <?php endif; ?> </div>
You then set the hook to “generate_before_content”. (or check the hooks with content.php).
Make sure to check “Execute PHP”.
On the Display Rule tab, set Location to the archive of your CPT. Save the hook and check the CPT archive page on the front end if it shows correctly.
September 14, 2021 at 10:17 pm #1930900JeffElvin –
That is so excellent. Thank you so much. It works perfectly. (The hook I used is
generate_after_entry_title
, by the way.)You helped me realize that this is essentially equivalent to writing a function in my functions.php file (e.g., “render_chapter_archive()”) with the above code and doing an
add_action
that hooks that function to thegenerate_after_entry_title
hook. I just disabled the GP element to test things, and it works nicely.I did have this line at the beginning of my function:
if ( ! is_post_type_archive( 'chapter' ) ) return;
(I do like the convenience of the GP hook element, though.)
And you’re right: to do stuff like this, there’s no need to create a custom child theme template.
Your and your team’s support is great. Great support for a great product. Appreciated.
Best,
Jeff CohanSeptember 14, 2021 at 10:24 pm #1930904ElvinStaffCustomer SupportNice one! π
As for the usage of Hook Element: In general, use with a bit of caution.
Some users get carried away and use it as a substitute for Code Snippets plugin or Child theme’s functions.php.
Hook Element is mainly for presentation purposes. It’s for hooking things to for the front end. (like in your case here)
Some users may sometimes use it for adding PHP filters for things. While it may work in some cases, that’s not recommended. π
Glad you got it sorted. No problem. π
-
AuthorPosts
- You must be logged in to reply to this topic.