MB Views doesn't show data in front
- This topic has 4 replies, 2 voices, and was last updated 2 years, 2 months ago by
Milos.
-
AuthorPosts
-
February 28, 2023 at 9:25 PM #40758
Milos
ParticipantHello, I have problem to show data from Field Groups in MB View.
Field Group name is Tickets fields, it has just one field ticket ( type is group) and it's Cloneable field.
Inside of this field group I have subfileds with id's ( team_1, team_2, prediction, betting_odd, datetime_game)In Views I created Ticket view and I used Insert Field button to get all data. this is the code:
<h3>TEST</h3> {% for clone in post.ticket %} {{ clone.team_1 }} <br> {{ clone.team_2 }} <br> {{ clone.prediction }} <br> {{ clone.betting_odd }} <br> {{ clone.datetime_game | date( 'F j, Y' ) }} {% endfor %}
For some reason I can see anything in front, I tried with shorcode, and with single page view, but all what I can see is h3 test...
Anybody knows what could be the problem ?
ThanksFebruary 28, 2023 at 10:04 PM #40760Peter
ModeratorHello,
The code looks good and works correctly on my local site. Can you please share some screenshots of the View template, post editing and value output?
February 28, 2023 at 11:04 PM #40764Milos
ParticipantHi Peter thank you for your replay, yes everthing looks correct but still is not working.
I will share with you here few screenshots, I hope it can help:
https://postimg.cc/gallery/0sghV0B
If I try to output anything from {% for clone in post.ticket %} I always get Null
March 1, 2023 at 6:02 PM #40771Peter
ModeratorHello,
Thanks, I see that issue. The custom fields are associated with the post type
ticket
so if you use the shortcode or assign the view template to a single page, the codepost.ticket
will return nothing because there is no field valueticket
associated with the page.To display a custom field associated with a post on a page, you can use the helper function
rwmb_meta()
and pass the post ID to the third parameter to get the field value.For example:
{% set tickets = mb.rwmb_meta( 'ticket', '', 123 ) %} {% for clone in tickets %} ...
where 123 is the ticket post ID.
Please read more on the documentation
https://docs.metabox.io/functions/rwmb-meta/
https://docs.metabox.io/extensions/mb-views/#running-php-functionsMarch 1, 2023 at 10:57 PM #40782Milos
ParticipantHello Peter thanks for help,
After reading documentation that you shared I manage to get data that I wanted.
I will share here code if somebody have similar problem.<h3>TEST Tiketi</h3> {% set args = { post_type: 'ticket', posts_per_page: -1 } %} {% set posts = mb.get_posts( args ) %} {% for post in posts %} {# Tickets: #} {% set tickets = mb.rwmb_meta( 'ticket', '', post.ID ) %} {% for ticket in tickets %} {# {{ ticket.team_1 | json_encode(constant('JSON_PRETTY_PRINT')) }} #} <br> {{ ticket.team_1 }} <br> {{ ticket.team_2 }} <br> {{ ticket.prediction }} {% endfor %} <div><hr></div> {% endfor %}
-
AuthorPosts
- You must be logged in to reply to this topic.