Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Samuel Johnson
Samuel Johnson
9,152 Points

Wordpress Advance Custom Fields

Im creating a very simple marketing website for a start-up in Wordpress that has a home page, a blog and contact page.

I want to create it so that on the front page, all the text is fully customisable by the admin page.

I tried using loops as described with the Wordpress create a theme but some reason i was unable to get on with this and seeing as the fields are only being used on the home page i found an easy way round this.

Ive create around 20 custom fields using the ACF Plugin then each field is stored as a variable at the top of the page like so:

''' <?php get_header();

$title = get_field( "title" ); $description = get_field( "description" ); .etc '''

then when i want the information to be displayed to the page i just echo out the correct variable:

''' <div class="slidecontent eight columns"><h2><?php echo $title?></h2></div> '''

It works very easily but can anyone tell me disadvantages to doing it this way or another more efficient way to this?

Also can you load images this way what complications will i come into?

4 Answers

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

You would echo out fields using

<?php the_field('field_name'); ?>

To echo out the title of a post or page you would still use

<?php the_title(); ?>
Hot Bricks Employee
Hot Bricks Employee
10,991 Points

Hi Zac, to echo the title of a post or page what would you use?

Samuel Johnson
Samuel Johnson
9,152 Points

Ok thanks im guessing when i first did this i was doing something wrong which is why i stored it as a variable at the top of the page.

Hey Zac, I'm still running into an issue trying to integrate the Advanced Custom Fields capability into my theme files.

This is the code i'm using to try to pull in a Custom Field named slide_title_1

<?php the_field("slide_title_1", $category_id);?>

Do I need to add code or a variable somewhere else to get this to work? I'm applying it to a page called archive-product.php and pulling from a custom field on the current category.

Any help is SO GREATLY appreciated. Thank you in advance,

Samuel Johnson
Samuel Johnson
9,152 Points

Hi Lindsey,

I ended up doing it a bit both ways depending on what i wanted it to do..

So using Zac's way id assume you this: <?php the_field('slide_title_1'); ?>

The way i originally did it would be this:

<?php get_header(); $slide_title_1 = get_field( "slide_title_1" ); ?>

This stores the field data as a variable, then use:

<?php echo $slide_title_1?> to use the variable anyway on the page.

Are you using pictures or text?