Site logo

Archived topic

Custom field post ID needs to be dynamic

5 replies · Started by Vicky on May 13, 2021

Viewing posts 1–6 of 6

Hi there,
I've got some code in a hook element which adds a fancybox modal to the custom posts which are displayed on the homepage. I'm using WP Showposts to display the posts and it's using the the wpsp_before_title hook.

 
    <a data-fancybox data-src="#modal" href="javascript:;" class="btn">Open demo</a>
  

  <div style="display: none;" id="modal">
    <h2>Hello!</h2>	
    [acf field="menu_item_title" post_id="30"]
</div>

This code works fine, but I really need to make the post ID dynamic so it shows the custom field from each post. Is there a way to do this?
Thanks in advance!

Hi there,

I believe there's no need to add the id if the ACF shortcode is used within a loop.

Can you try not adding the post_id atts?

Keep it like this: [acf field="menu_item_title"]

Let us know.

Hi,

I've taken out the post_id as you've suggested but it still doesn't work. It still just shows the custom field from the first post for all of the posts.

There's a link below to the site if that helps.

Thanks

Hi there,

what Elvin said is correct and the code is actually working, if you inspect the site and check the HTML being output for each posts Modal Div it is display the correct custom field value.

The issue is the Modal HTML - all of them have identical IDS:

data-src="#modal"
<div style="display: none;" id="modal">

those IDs need to be unique for each modal.

- the script and the modal IDs are identical - so it opens the first modal it finds.
You would need the modal script to be dynamic

Hi David, thanks for the explanation, I can see what is going on now.

I've replaced modal with the post ID and now it's working.

Thanks

Glad to be of help.

This archived topic is closed to new replies.