Code returns also empty fields
- This topic has 8 replies, 2 voices, and was last updated 3 years, 9 months ago by
Long Nguyen.
-
AuthorPosts
-
June 30, 2021 at 1:04 AM #29208
[email protected]
ParticipantCode question: I have a field Post with related posts. The code I use for display works. When there is no related post then it displays a empty value. I think the problem is in the IF statement.
Can anybody help me?<?php global $post; $field_id = "hoogtepunten"; $post_id = rwmb_meta( $field_id ); if ( $post_id ) { echo '<h2 class="related-content-title">Bezienswaardigheden langs de route</h2>'; echo '<div class="related-content">'; foreach( $post_id as $post) { setup_postdata( $post ); ?> .... some data ....here it goes wrong <?php } echo '</div>'; wp_reset_postdata(); } ?>
June 30, 2021 at 8:49 AM #29211Long Nguyen
ModeratorHi Cees,
There is no problem with the
if
statement in your code. Nothing displays if the condition is not true, it's normal behavior. You can addelse
statement to display something if the condition wrong.if ( $post_id ) { echo "True"; } else { echo "False"; }
Get more details here https://www.w3schools.com/php/php_if_else.asp
June 30, 2021 at 12:45 PM #29214[email protected]
ParticipantHi,
It doesn't change anything.
if ( $post_id ) {
echo "True";
} else {
echo "False";
}gives a true on every page (with or without related posts). I have migrate from ACF to METABOX with the tool. I don't understand what is happen.
June 30, 2021 at 12:58 PM #29215[email protected]
ParticipantIt is a bug. I have do the new migration tool.
This code $arrayLength = count($post_id) show 1 when there is no related post.
I have edit the and save and got a error-message that I have to fill al the fields (required) but that isn't required.I go to metabox and save the Custom Fields without changing anything. Now the results on the front-end are oke and I can change the post without fill all the fields.
I see this message on all posts where I use the Custom Fields that are migrate from ACF.
June 30, 2021 at 9:10 PM #29220Long Nguyen
ModeratorHi Cees,
Can you please share the debug log and some screenshots of the error message?
June 30, 2021 at 9:16 PM #29221[email protected]
ParticipantHi Long,
Which debug log do I have to send?
You can see here an example:
https://oxygen.groetenuitleusden.nl/doen/hardlopen-in-asschat/
(removed)
You see that at below the heading Horeca langs de route an empty picture and link. This is what happens (also at heading Bezienswaardigheden langs de route) where are no related post at the current post. When I save this post then the problem is gone.
July 2, 2021 at 2:19 PM #29250Long Nguyen
ModeratorHi Cees,
Here is the code that you shared
global $post; $field_id = "hoogtepunten"; $post_id = rwmb_meta( $field_id ); if ( $post_id ) { echo '<h2 class="related-content-title">Bezienswaardigheden langs de route</h2>'; echo '<div class="related-content">'; foreach( $post_id as $post) { setup_postdata( $post ); ?> .... some data ....here it goes wrong <?php } echo '</div>'; wp_reset_postdata(); }
and I see the heading, post related in one code block display as well https://share.getcloudapp.com/lluN7Ogx
What is the code block that you show the heading "Horeca langs de route"?
You also said:
This code $arrayLength = count($post_id) show 1 when there is no related post.
I have edit the and save and got a error-message that I have to fill al the fields (required) but that isn't required.Can you please share some screenshots of the whole scenario?
July 5, 2021 at 10:50 PM #29313[email protected]
ParticipantHi Long,
The website is live now. You can see there problem here: https://www.groetenuitleusden.nl/doen/hardlooproute-amersfoort/
I share a video what is happening :
https://api.vadoo.tv/landing_page?vid=63wYAKBAmkbwY0ELr29PducxHNpowzOW
July 6, 2021 at 12:52 PM #29326Long Nguyen
ModeratorHi,
Thanks for your additional information.
Can you please export the field group of the ACF or share the generated PHP code when exporting? I will try to import that field group to my site and migrate data to reproduce the issue.
And please share some screenshots/screen record in your database, table wp_postmeta, meta key: field ID before updating the post. It looks like the meta value (field value) is not really empty.
Or change the condition to
if ( !empty ( $post_id ) ) { ... }
-
AuthorPosts
- You must be logged in to reply to this topic.