Hyperlink url field outputs strange code
Support › MB Builder › Hyperlink url field outputs strange codeResolved
- This topic has 2 replies, 2 voices, and was last updated 6 years, 2 months ago by
Hein.
-
AuthorPosts
-
February 26, 2019 at 2:09 AM #13471
Hein
ParticipantHey Guys,
I am working on a thing which seemed simple but I am struggling with something and I need a few fresh eyes. Is it because of me or what?
The thing I'm making is a few source link/text inside posts. So the authors can have enter the sources of some stuff which has been written inside the article.
No I've added this code inside functions.php (don't mind the default values, it's just for testing purposes)
add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' ); function your_prefix_register_meta_boxes( $meta_boxes ) { $meta_boxes[] = array ( 'title' => 'Micro Trends Sources', 'id' => 'micro-trends-sources', 'post_types' => array( 0 => 'post', 1 => 'page', ), 'context' => 'advanced', 'priority' => 'high', 'fields' => array( array ( 'id' => 'text_1', 'type' => 'text', 'name' => 'Source Title', 'size' => 50, 'clone' => 1, 'sort_clone' => 1, ), array ( 'id' => 'url_1', 'type' => 'url', 'name' => 'Source URL', 'size' => 50, 'clone' => 1, 'sort_clone' => 1, ), ), ); return $meta_boxes; }
Looks like this in the post area
I need to set this up with a shortcode so I've used this.
<a href="[rwmb_meta meta_key='url_1']">[rwmb_meta meta_key='text_1']</a>
And it looks like this inside the post frontend area
Now this is all looking good and works fine. Only when I click on a link it returns a 404 error. After some head smashing to the wall, I've found that there's some extra code behind the url.
It trows in a closing li and ul at the end of the url. Now I think it's because of the brackets inside the shortcode or because the shortcode is wrapped inside a href tag.
Hope this all makes some sense. I would appreciate any ideas on how to solve this. Probably something simple.
Regards,
HeinFebruary 26, 2019 at 9:00 AM #13472Anh Tran
KeymasterHi Hein,
You have 2 cloneable fields, so when you use the shortcode to output, each of them will output in a unordered list.
To fix this, please use this PHP code instead of shortcode:
$texts = rwmb_meta( 'text_1' ); $urls = rwmb_meta( 'url_1' ); if ( ! empty( $texts ) ) { echo '<ul>'; foreach ( $texts as $k => $text ) { echo "<li><a href='{$urls[$k]}'>{$text}</a></li>"; } echo '</ul>'; }
February 26, 2019 at 3:31 PM #13480Hein
ParticipantThanks Anh,
That solved it.
You can close this topic now.
-
AuthorPosts
- You must be logged in to reply to this topic.