Home Forums Front-end Issues Custom fields only displaying for one post


Custom fields only displaying for one post

  • Hi there ,

    I mostly come from a Front End/Design background and have built a few sites in Genesis but I am now delving into the world of making Custom Fields, Custom Posts and pages through using a variety of tutorials and experimenting and through my surprise I have managed to get everything working apart from one thing (my PHP and WordPress code knowledge is still not the greatest so any help would be appreciated!)

    On my wedding archives page template I want each item to be able to display each Custom Field in a clickable lightbox (site link and password below)

    This function only seems to display the most recent entries lightbox content!? Which means when you click on a lightbox it just shows the same content no matter which one you click on ?!

    Here is the function :

    function display_custom_fields_archive() {
       	$music = get_field( 'music' );
    	$video = get_field( 'video' );
    	$set_list = get_field( 'set_list' );
    	$photos = get_field( 'photos' );
    	if ( $music || $video || $set_list || $photos ) {
               echo '<div class="archive_container">' ,'<div class="archive_box">' ,
            '<a href="#fancyboxID-video" class="fancybox-inline"><i class="fa fa-video-camera"></i></a>', '','<a href="#fancyboxID-set_list" class="fancybox-inline"><i class="fa fa-sticky-note"></i></a>','', '<a href="#fancyboxID-photos" class="fancybox-inline"><i class="fa fa-camera"></i></a>','','<a href="#fancyboxID-music" class="fancybox-inline"><i class="fa fa-headphones"></i></a>','
            		if ( $music ) {
    				echo '<div style="display:none" class="fancybox-hidden"><div id="fancyboxID-music" class="hentry" style="width:460px;max-width:100%"> ','' . $music . '
    			if ( $video ) {
    				echo '<div style="display:none" class="fancybox-hidden"><div id="fancyboxID-video" class="hentry" style="width:460px;max-width:100%"> ','' . $video . '
    			if ( $set_list ) {
    				echo '<div style="display:none" class="fancybox-hidden"><div id="fancyboxID-set_list" class="hentry" style="width:460px;max-width:100%"> ','' . $set_list . '
    			if ( $photos ) {
    				echo '<div style="display:none" class="fancybox-hidden"><div id="fancyboxID-photos" class="hentry" style="width:460px;max-width:100%"> ','' . $photos . '
    		echo '</div>','</div>';

    The Custom CPT is known as “wedding”

    If anyone can help this would be greatly appreciated as I am struggling with the syntax I know it will have something to do with getting the ID of each post but I am struggling to get this one over the line !


    Andrew Rainey

  • Hi @raindahl

    I believe you added it to the functions.php file. Because the get_field() function was called outside The Loop, you need to get the post object using the global method. Could you please try the following code?

    global $post;
    $music = get_field( 'music', $post->ID );
    $video = get_field( 'video', $post->ID );
    $set_list = get_field( 'set_list', $post->ID );
    $photos = get_field( 'photos', $post->ID );


  • Hi James ,

    Thanks very much for getting in touch to try help me with this issue!

    I did have it in my functions.php initially as I wanted it to work across a couple of post names such as Wedding , Ceilidh , DJ etc but I did move it into the archive page and singular pages to see if it made a difference which unfortunately had no joy

    I put the code that you suggested and tried moving it around the templates and functions and it still has not worked



  • Hi @raindahl

    Could you please share the PHP file for that page? If you could share the JSON or XML export of the field group, that would be great too!

    Also, could you please test it by providing the ID manually for the get_field() function like the following?

    var_dump(get_field( 'music', 105 ));

    Where “105” is the ID of your post that has the custom fields. That code will test if ACF can get the fields correctly. If it can, it means there’s something wrong with your code.


Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Custom fields only displaying for one post’ is closed to new replies.