Archived topic
How to run php for advance custom fields in generatepress elements block
4 replies · Started by parantapa sarkar on September 4, 2021
I have installed advance custom field now I want to create hooks for the different categories that's why I can't modify single.php file. I want to use generatepress display rules.
Goal: I want to run PHP for fetching dynamic content from the fields in a table if the custom field is empty then the table should not show anything.
Problem : i am only be able to fetch content though shortcode of acf "[acf field="real_name"]"
But the PHP function is not working
for example, i want to run This code
<div class="col-sm-12" id="vee-details">
<table class="table table-bordered" width="100%">
<tbody>
<th colspan="2" class="thead"><span style="font-size: 18pt;color: #ff7a00"><strong>Bio/Wiki</strong></span></th>
<tr>
<td><b>Real Name</b></td>
<td><?php the_field('real_name'); ?></td>
</tr>
<tr>
<td><b>Nick Name</b></td>
<td><?php the_field('nick_name'); ?></td>
</tr>
</table>
</div>
Here <?php the_field('real_name'); ?> the function is not working
And for displaying if the fields is empty or not i have written this code
<?php
$real_name = get_post_meta($post->ID, 'real_name', true);
if($real_name){ ?>
<p> Real name : <?php echo $real_name; ?></p>
};
<?php
}else{
// do nothing
}
?>
I hope the codes are correct but in element i can't run the code.
I will combine both codes if <?php echo get_post_meta(get_the_ID(), 'real_name', true); ?> , <?php the_field('real_name'); ?> any function works.
Please help me.
Hi there,
you can't run PHP Codes in the Block Element. You would have to use a shortcode in them.
The Hook Element accepts HTML and PHP.
David, I have tried with the hook but that is still can't fetch the value from the acf
can you explain to me how can I fetch the value from the ACF without the ACF shortcodes.
I cannot see an issue with the code.
First question is, are is the meta name eg. real_name you're using correct? And does the post you're hooking the code into have a value in that field?
As a note the Block Element with the GenerateBlocks plugins supports Post Meta including ACF Custom Fields.
https://docs.generatepress.com/article/dynamic-data/
In the Dynamic Data text type you can use the Post Meta option and add the ACF Custom Field name in the Post Meta name field.