On f.e. http://www.advancedcustomfields.com/resources/displaying-custom-field-values-in-your-theme I read about code you can use. But WHERE can I place this code? (Sorry, I don’t have experience with this.)
I ‘m using WordPress 4.1.1 theme: Twenty Fifteen.
Who can help me?
Ok, so you create the fields in the “Custom Fields” tab in the WordPress dashboard. If you have done this and created a field, it will yield a “value”. So if you create a field called “Home Text”, the value field will be home_text.
When you create a rule for the “Home Text” to be shown on your “Home” page, then the fillable field will show on the Home Page dashboard.
Once that is done, you need to call the field value within the template that is being used for the Home Page. Are you using a Home Page template or the default template?
So lets say you have a pretty generic Homepage Template
<?php /** *Template Name: Home */ <?php get_header(); ?> <?php the_field('home_text'); ?> <?php get_footer(); ?> ?>
If you place “the_field” php execution like above, your custom field will show within this template.
No, are you familiar with WordPress templates? So, on the WordPress dashboard, go to your homepage editor. Pages > Home.
On the right panel, where you can publish your page, you should see ‘Page Attributes’. That will tell you which template you are using for your homepage. Then go to Appearance > Editor > look for the template file that your page attribute for your homepage is using.
This will show the code markup for your homepage template and you can place the
<?php the_field('home_text'); ?> wherever in the code you want your field to render on the homepage.
Well the whole point of ACF is to be able to add fields to the Page/Post Editor dashboard, so you don’t have to add to the content editor and your field will populate onto the page. So to answer your question, no…the snippets need to be added to the template.
Doesn’t that go against the easy way of creating the custom fields? I love how that is managed. Wish there was a simpler way for non-developers to add the fields to a page, post, etc. instead of modifying the PHP of the template. Unfortunately, I can’t get that to work either.
@toolboxwebdesign, thank you very much for your answers. You really helped me with this.
One more question: which file/template do I have to change when I want to display the added fields under the title of a post? I am using the standard template in WordPress 4.1.1 theme ‘Twenty Fifteen’.
You have to add it to content.php. The reason you add it here to add it to all your blog posts, is that it follows a hierarchy of templates to call the content.php into every blog post. It goes Main Index Template(index.php) > content.php. Meaning content.php is part of the Main Index Template.
So if you want to add your field right under the title, go to content.php, and add your php snippet under a div tag classed “entry-content”. Just add it under that opening tag. 🙂
Hi Lt_Core, ok so you can place the custom fields on both pages and posts. So lets say you create a page “Portfolio”. Go into Custom Fields and create a new field. You can set a rule for your field that says it will show up on “Page” and your pages will show up in a drop down menu. Then choose Portfolio and your custom field will be on your “Portfolio” content editor page. As for the code. If you create a text field, then it depends on what you call it….but lets just say you call it Top Content. then the value will be
top_content. Go to your Portfolio page and fill out your
top_content field with your content. Then you need to go into the page template that you want the content to show up on and add the following code:
<?php the_field('top_content'); ?>
The topic ‘Where can I place the code?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!