Relationship CPT Title not Displaying using MB View Shortcode
Support › MB Relationships › Relationship CPT Title not Displaying using MB View ShortcodeResolved
- This topic has 6 replies, 2 voices, and was last updated 3 years, 11 months ago by
Long Nguyen.
-
AuthorPosts
-
May 19, 2021 at 1:10 AM #28281
Shrini
ParticipantBasically want to display list of candidate & his party
Relationships:
1. Candidate belongs to one party
2. Party have many members (candidates)so created a relationship called "candidate-party" between candidate CPT & party CPT (non-reciprocal)
Relationship Establishment Reference:
<?php add_action( 'mb_relationships_init', 'candidate-party' ); function candidate-party() { MB_Relationships_API::register( [ 'id' => 'candidate-party', 'from' => [ 'object_type' => 'post', 'post_type' => 'candidate', 'empty_message' => 'No Party Added', 'admin_column' => [ 'position' => 'after title', 'link' => 'view', ], 'meta_box' => [ 'title' => 'Party', ], 'field' => [ 'add_button' => 'Add Member', ], ], 'to' => [ 'object_type' => 'post', 'post_type' => 'party', 'empty_message' => 'No Candidates Added', 'meta_box' => [ 'title' => 'Party Members', 'context' => 'normal', ], 'field' => [ 'max_clone' => '1', ], ], ] ); }
as a first step trying to display list of candidates through MB View using shortcode, but unfortunately display none.
MB View Reference:
Candidate List:
{% set relationship = attribute( relationships, 'candidate-party' ) %} {% for post in relationship.to %} {{ post.title }} {% endfor %} <br/> <br/> {% set relationship = attribute( relationships, 'candidate-party' ) %} {% for post in relationship.from %} {{ post.title }} {% endfor %} <br/> <br/>
May 19, 2021 at 11:31 AM #28285Long Nguyen
ModeratorHi,
Your code looks good except for one point, the function name should use the underscore (_) between words, no hyphen (-). See more here https://www.codementor.io/@veenitchauhan/basics-of-naming-conventions-for-php-developers-eliexmew6
add_action( 'mb_relationships_init', 'candidate_party' ); function candidate_party() {
And if you want to use the View shortcode on other pages, please wrap the relationship PHP code in a function then call it via proxy
mb.
in View.May 19, 2021 at 8:52 PM #28294Shrini
ParticipantI pasted the generated code from relationship in theme main function file through theme editor in wordpress but it shows
Your PHP code changes were rolled back due to an error on line 69 of file wp-content/themes/Extra/functions.php. Please fix and try saving again.
syntax error, unexpected '<', expecting end of file
/*** Meta Box Functions by Shrini ***/ <?php add_action( 'mb_relationships_init', 'candidate_party' ); function candidate_party() { MB_Relationships_API::register( [ 'id' => 'candidate_party', 'from' => [ 'object_type' => 'post', 'post_type' => 'candidate', 'empty_message' => 'No Party Added', 'admin_column' => [ 'position' => 'after title', 'link' => 'view', ], 'meta_box' => [ 'title' => 'Party', ], 'field' => [ 'add_button' => 'Add Member', ], ], 'to' => [ 'object_type' => 'post', 'post_type' => 'party', 'empty_message' => 'No Candidates Added', 'meta_box' => [ 'title' => 'Party Members', 'context' => 'normal', ], 'field' => [ 'max_clone' => '1', ], ], ] ); }
so I removed the <?php and pasted but it shows
Your PHP code changes were rolled back due to an error on line 101 of file wp-content/themes/Extra/functions.php. Please fix and try saving again. syntax error, unexpected end of file
May 19, 2021 at 8:57 PM #28296Shrini
ParticipantOk fixed the syntax error & saved successfully
May 19, 2021 at 9:02 PM #28298Shrini
Participantstill not able to display the values
here is the MB View
codeCandidate List: {% set relationship = attribute( relationships, 'candidate_party' ) %} {% for post in relationship.to %} {{ post.title }} {% endfor %} <br/> <br/> {% set relationship = attribute( relationships, 'candidate_party' ) %} {% for post in relationship.from %} {{ post.title }} {% endfor %} {% for post in relationships.candidate_party.to %} {{ post.title }} {% endfor %} {% for post in relationships.candidate_party.from %} {{ post.title }} {% endfor %} <br/> <br/>
May 19, 2021 at 11:43 PM #28307Shrini
ParticipantHere is the video for your reference ....just simple task, I dont know where I am making mistake
May 20, 2021 at 9:09 AM #28316Long Nguyen
ModeratorHi Shrini,
Let me explain this case.
If you use Insert Field and get the code
{% set relationship = attribute( relationships, 'candidate_party' ) %} {% for post in relationship.from %} {{ post.title }} {% endfor %}
it will take the current post ID of the post that you insert the shortcode. The PHP code in the documentation has the same meaning
$pages = MB_Relationships_API::get_connected( [ 'id' => 'posts_to_pages', 'from' => get_the_ID(), //here ] ); foreach ( $pages as $p ) { echo $p->post_title; }
So if you add the shortcode to a post/page that does not have the relationship, it will not show the connection title. You should change the Type to Singular and Post to post type Candidate to use the field in the Insert Fields list. Screenshot https://share.getcloudapp.com/Jru4K55B
-
AuthorPosts
- You must be logged in to reply to this topic.