• For the past few days, I have been looking at a better way of displaying our gallery. I’ve managed to setup a content type called ‘Gallery Images’. The only data in the custom fields is a featured image.

    In my custom theme, I would like to display these in a template. Not sure how to do this.

    Duncan

Viewing 3 replies - 1 through 3 (of 3 total)
  • You have to call that custom field into your theme/template file.

    You can pull any custom field into your theme/template by doing this:
    <?php the_field(''); ?>

    You’ll need to wrap that in whatever HTML tag you want.

    You can find more here: https://www.advancedcustomfields.com/resources/hiding-empty-fields/

    Thank you Alston,

    Lets say I want to show the value in this line:

    $output = sprintf( '%s <a class="link" %s href="%s" #CODEHERE#>%s %s %s %s %s %s %s</a> %s', $site_icon, $target, $href, $title, $sticky_icon, $additional_icon, $mobile, $new_icon, $extra, $hover, $review );

    Since that code is already between php tags, how can I replace the #CODEHERE# with the_field?

    Can you (or someone) help me? ??

    Thanks!

    Hey Divvy,
    I’m not familiar with the above snippet you included, but if you’re already inside the
    <?php ?> tags then you just would right it in the template code. I typically will save the custom field into a php variable on that specific template file that I’m working on changing in a manner like:
    $custom-field-var = get_field('custom_field_name');

    Then when I need the exact input from that field in my page template I’ll echo that variable within php tags:
    <img src="<?php echo $custom-field-var ?>" />
    I would use the above if I needed that src url from the backend into the front end. I would do this to create a <a href> link instead of having some other site admin manually wrap the <a href> around an element. From my experience, the less work a site admin has to do with the WYSIWIG editor’s buttons/inline styles the better.

    So having said all that, since you want to put a custom field into an html string, I would imagine within the ” marks that delineate the string you’ll need to use an opening and closing php tag since within the string and concatenate it with the string by opening and closing the string quote marks, inserting the variable/custom field, and then finishing the string by concatenating it with the rest of whatever you need in that file above. Look up Php string concatenation and you’ll see how to do it.

    • This reply was modified 7 years, 9 months ago by traveler.
    • This reply was modified 7 years, 9 months ago by traveler.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Displaying my Custom Fields’ is closed to new replies.