Site logo

Archived topic

Custom fields template howto

7 replies · Started by David on October 21, 2020

Viewing posts 1–8 of 8

Hello!!
I have been studying the posts about the implementation of advanced custom types and custom fields in Generatepress templates. I don't know if I have it figured out. Before I start testing in code I would like you to confirm if the following is correct:

I have created two ACT: 'ong' and 'causa' and their custom fields.
As I have read here in the support forum the steps to show the page of each of these ACT are the following:
1º Copy page.php and rename it with the name of each ACT: ong.php and causa.php
2º Move this files to child folder.
3º Copy content-page.php in child folder and rename it with the name of content-ong.php and content-causa.php
4º In the code of ong.php and causa.php replace the line generate_do_template_part('page') with both
get_template_part('content','ong');
get_template_part('content','cause');

If everything is ok I need to know in which files do I have to put the HTML and the values of the custom fields of each ACT to design the custom page?
Thank you very much in advance.

Excuse me, it sounds very interesting about the elements but I have looked at it and I think it needs time to learn how to use it.
Can you solve my doubt based on what I have proposed? Later I will dedicate more time to Elements because it seems to be very practical.
Thank you very much.

Your proposed method is correct.

It is to these files that you would make the edits to include your custom fields:

3º Copy content-page.php in child folder and rename it with the name of content-ong.php and content-causa.php

Sorry again, but this is not working. I think that the theme is not getting the new templates of the ong and causa.
Would you take a look please and tell me what I am doing wrong?
Thank you very much!

I don't believe you've named your post type files correctly.

Post types have two parts - archives and single posts. There are different templates for both, as shown here: https://developer.wordpress.org/themes/template-files-section/custom-post-type-template-files/

So the archive would be named archive-{post-type}.php and the single posts would be single-{post-type}.php.

Then, when you do this:

get_template_part( 'content', 'cause' );

WordPress looks for a file like this: content-cause.php to insert into that area.

Hi Tom, thanks for your answer. So I dont have to copy page.php and rename it to causa.php? simply copy single.php and rename it to single-causa.php? Also with archive.php.

And into the single-causa.php do I have to change anything?
There is a line with: generate_do_template_part('single');
I have changed ('single') for ('causa') and ('single','causa' ) and returns nothing. How I said to Wordpress that the CPT causa has a different template with its custom fields? because I think that it is loading the main file single.php without the custom fields.
I am very lost with this issue...
Thanks in advance!!

Correct. You can replace this:

generate_do_template_part( 'single' );

With this:

get_template_part( 'content', 'causa' );

And that will load your content-causa.php file in that location.

This archived topic is closed to new replies.