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 From Bootstrap to WordPress Create Bootstrap Styled Theme Templates Creating a Portfolio Single Page

sandrine labat
sandrine labat
6,861 Points

how can we call/echo out the custom fields title with php

in the lesson:Creating a Portfolio Single Page we wrote <h1>Portfolio</h1> but how could we call/echo out the custom fields title with php instead

Are you using a custom field plugin like Advanced Custom Fields?

6 Answers

Have you looked at their documentation? http://www.advancedcustomfields.com/resources

It's pretty straightforward. For example, if you were using a Text field as the ACF field type, it would be as simple as:

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

Where 'text' would be replaced with the name of your field. If you wanted to use a conditional to check for the existence of a value for that custom field before displaying it, you could do:

<?php
if(get_field('field_name'))
{
    echo get_field('field_name');
}
?>

Where 'field_name' would be replaced with the name of your field.

sandrine labat
sandrine labat
6,861 Points

Indeed I am I am filling the exercise in this tutorial If I write <?php the_title(); ?> will obviously echo out the title of the single page but I would like to call out the custom field title which is kind of a parent

sandrine labat
sandrine labat
6,861 Points

Thank you, I did try before the_field but unfortunately it does not work

In the tutorial given the label and the name of the field is Link... Now I try to change to the relevant name still no luck In this particular example I have a custom field call portfolio In this field I have several pieces All those pieces got a single page. In that single page I need the <h1> To echo the custom field name 'portfolio'.. But at the moment it return blank;(

Can you post your code and possibly a screen shot of your custom field configuration?

sandrine labat
sandrine labat
6,861 Points

ok this is the screenshot

[img]http://i.imgur.com/wJvZZkg.png[/img] [img]http://i.imgur.com/7NOWRY6.png[/img]

and for the code

'''

<?php get_header(); ?>

<div class="container">
  <div class="page-header">
    <div class="row">

      <div class="col-xs-9">
        <h1><?php the_field('name') ;?></h1>
      </div>

      <div class="col-xs-3 prev-next">
        <?php next_post_link( '%link', '<span class="glyphicon glyphicon-circle-arrow-left"></span>'); ?>
        <a href="<?php bloginfo('url') ;?>/?p=65"><span class="glyphicon glyphicon-th"></span></a>
        <?php previous_post_link( '%link', '<span class="glyphicon glyphicon-circle-arrow-right"></span>'); ?>
      </div>

    </div>

  </div>

Thank you

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Based on the screenshots you posted of the link field, you will want to change your code to

<h1><?php the_field('link') ;?></h1>

in order for it to show up, unless you have a custom field called name as well?

sandrine labat
sandrine labat
6,861 Points

Thanks zac, I tried that before and that give me nothing;( Blank I as well tried <h1><?php the_field('custom_title') ; ?>

as shown on the pluging site and still nothing

however the link field is used in the exercise for website address to link to another page

Just to test something out, try:

<?php
$value = get_field( "link" );
echo $value;
?>

See if that returns anything.

sandrine labat
sandrine labat
6,861 Points

Thank you for your help. Unfortunately this will only give me the content of that field which is a link what I wanted to try it was to call out the title of the group field in this case portfolio

It seems the easiest way it s to add another field to this group which will allow me to call out what ever title i want there for each piece