Support

Account

Home Forums Add-ons Repeater Field Make (Sub_Field) Link to Post?

Solved

Make (Sub_Field) Link to Post?

  • Hello..I am fairly new to WordPress, so please bare with me. πŸ™‚

    I purchased ACF Repeater – and im trying to figure out how do I get a (sub_field) to link to a post I created for it?

    I was able to display & list everything on the Page with:

    <?php if(get_field(‘list_of_games’)): ?>

      <?php while(has_sub_field(‘list_of_games’)): ?>

    • <?php the_sub_field(‘game_name’); ?>
    • <?php endwhile; ?>

    <?php endif; ?>

    However, I need the “game_name” to hyperlink to a specific post…Can you please help to show me how to make it work & link properly?

    I appreciate your help!

    Thank you!

  • Hi @wpdragon

    Does your data contain a link that you can use?
    Is game_name a post_object field type? Or is it a text field?
    Where in the backend would you like to enter this link? Would you link to enter the link for each repeater row?

    Once you have the link data available, then you can simply write a link like so:

    
    <a href="<?php echo $link; ?>"><?php the_sub_field(β€˜game_name’); ?></a>
    

    Thanks
    E

  • Hii..okay, i switched it from Text to Page Link.

    After I put the code in…I do get the URL to display a Link on the page…however, I notice it is listing the Full URL – instead of the actual name of the games….can we have it display the game name, as the link?

    Also, the links are not actually working — when I click on them or hover over them I notice that they are pointing to the current page they are on…and not the actual URL address that they are suppose to go to.

    Hopefully, you know what i mean…im fairly new, so im trying to explain it easily. πŸ™‚

    Thanks.

  • *UPDATE – I just realized you mentioned “Post Object Field”…so, I tried that instead of “Page Link”…but setting it to “Post Object” gave me some odd error:

    ) SCREAM: Error suppression ignored for
    ( ! ) Catchable fatal error: Object of class WP_Post could not be converted to string in C:\wamp\www\site\wp-content\plugins\advanced-custom-fields\core\api.php on line 681

    Should I be using “Page Link” instead?

  • Hi @wpdragon

    The post object field will return an object containing all the $post data.
    Please read over the documentation to better understand how to output the $post data in a link / title.

    Thanks
    E

  • Hey there, I’m having the same issue (or very similar). I setup the structure to have a repeater field with “Artists”, and then each artist is fetched from the post_object field (each artist is a Page in my case). I read the documentation for the post_object, but honestly can’t get any output (ok, I’m not a developer, I rely a lot on copy/paste, don’t judge me) πŸ˜€

    I tried what the documentation says:

    <?php if(get_field('album_artist')): ?>
    	<?php while(has_sub_field('album_artist')):?>
    
    		<?php
    
    		$post_object = get_field('post_object');
    		 
    		if( $post_object ): 
    		 
    			// override $post
    			$post = $post_object;
    			setup_postdata( $post ); 
    		 
    			?>
    		    <div>
    		    	<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    		    	<span>Post Object Custom Field: <?php the_field('artist_name'); ?></span>
    		    </div>
    		    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    		<?php endif; ?>
    
    	<?php endwhile; ?>				 
    <?php endif; ?>

    but I get no output πŸ™

    Help!!

  • Hi @hugobaeta

    Your issue is most likely with the line:
    $post_object = get_field('post_object');

    1. When loading a sub field value, your CAN NOT use get_field, you must use get_sub_field
    2. Is your sub field really called ‘post_object’? Please change this to the correct sub field name.

    Hope that helps.

    Thanks
    E

  • IT WORKED!!! YAY! Thank you so much @elliot πŸ™‚

    so, yeah, the post_object was not the name of the sub-field, I just hadn’t changed it when I copy/pasted the code from the documentation.

    Cheers!

  • Hello..It worked based off the documentation & example above…and I am now able to list the Titles & they are linking properly….However, when its set to Field Type: Post_Object — I lose my entire sidebar & Footer from the template page that I am using the this on…

    When the Field Type is set on anything else – such as: Page_Link — then my page goes back to normal with the sidebar & footer on the page….but, obviously using Page_Link wont work the way I need it….

    Also, I am working Locally – so I even notice that the WordPress Admin bar at the top disappears….this is only occurring on this specific page, when using Post_Object as the Field type.

    Is there any explanation as to why I would lose my sidebar & Footer from the page, when using Post_Object?…I would appreciate any help possible to make this work.

    Please keep in mind, I am not a developer – so i appreciate the patience & assistance. πŸ™‚ thanks.

  • Hi @wpdragon

    Can you please turn on DEBUG_MODE in your wp-config.php file. This will spit out a PHP error which you can use to track down the issue.

    It’s most likely that you are attempting to use an object variable which doesn’t exist.

    Thanks
    E

  • okay, i set DEBUG_MODE to ‘TRUE’….and I do not get any Error or Debugging information on the page.

    Just so you know the Data results that I created in ACF are displaying & linking properly…but my page template loses its Sidebar, Footer, and the WordPress Admin Bar at the Top disappears (only on this specific page) — when I select Post_Object as the Field Type…it seems to only happen when I select Post_Object.

    Below is the code I used — I dont think this code is the issue, because even when I remove the entire code, it still doesnt show the sidebar, widget, and WP Admin bar….it only seems to happen when I select Post_Object in the sub field ACF settings: (in my case, “game_name” is set as Post_Object)

    <?php if(get_field('list_of_games')): ?>
      <?php while(has_sub_field('list_of_games')):?>
    
        <?php 
          $post_object = get_sub_field('game_name');
             
          if( $post_object ): 
          // override $post
          $post = $post_object;
          setup_postdata( $post ); 
        ?>
         <div>
           <ul>
           <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a</li>
           </ul>
              <span><?php the_sub_field('list_of_games'); ?></span>
          </div>
            <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
        <?php endif; ?>
      <?php endwhile; ?>         
    <?php endif; ?>

    Any ideas?… :-/

    I appreciate your help. Thanks.

  • Hi @wpdragon

    For a page to stop rendering, the issue is always a PHP error and will be output as an error message in the source code. Perhaps you need to look in the source code to find the error? It should be the last thing on the template aside from closing tags.

    Have you done any debugging of your code?
    Can you debug the $post_object variable? What value does it hold?

    Thanks
    E

  • Hi @elliot,

    Okay, I did a test by switching the page to another Template & inserted the Code above, and it worked!…so, with that – I went ahead & looked deeper into the original template page that I was having issues and finally noticed an extra ‘ <?php endif; ?>’ hidden in a line of code.

    So, things seem to be working properly now..thanks for your assistance..if i have any other questions, im sure ill ask. πŸ™‚

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

The topic ‘Make (Sub_Field) Link to Post?’ is closed to new replies.