Skip Navigation

[Resolved] How to set short code to use with current post id correct

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 3 replies, has 2 voices.

Last updated by Minesh 10 months, 4 weeks ago.

Assisted by: Minesh.

Author
Posts
#2675713
2023-12-22_15-24-08.png

Tell us what you are trying to do? On this link: hidden link we have on the right side in the bottom a map. I do use for this a view, where I basically like to have only the current post information. For this I have a filter defined "Include only posts with IDs set by the View shortcode attribute "ids" eg. [wpv-view name="view-name" ids="1"]"

Is there any documentation that you are following? https://toolset.com/forums/topic/query-filter-to-display-only-current-post-data-in-view/ and other https://toolset.com/forums/topic/i-cant-filter-view-to-current-post/

Is there a similar example that we can see? For the short code I have my own coding:

add_shortcode( 'mapview', 'mapview_shortcode' );
function mapview_shortcode() {
global $post;

$myCode = do_shortcode('[wpv-view name="map-for-beach-articles" ids="'.$post->ID.'" cached="off"]');
echo $myCode;
}

Somehow it shows me this:

7.898793, 98.295995| 7.89091, 98.2653| 7.876628, 98.275781| 7.843881, 98.293682| 7.9687, 98.279635| 7.820156, 98.297906| 7.806383, 98.29906| 8.041567, 98.276541| 7.774374, 98.306821| 7.778018, 98.296632| 7.767716, 98.306357| above the map.

Any idea how i can off this? I only need the map.

What is the link to your site? hidden link

Do you need more information, such as the view:

[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
[wpv-conditional if="( $(wpcf-location) gt '1' )"]
[wpv-map-render map_id="map-10" map_width="100%" map_height="400px" fitbounds="off" general_zoom="10" single_zoom="10" general_center_lat="7.934094" general_center_lon="98.337356"][/wpv-map-render]
<wpv-loop>
[pinnumber location="[types field='location' format='FIELD_LATITUDE, FIELD_LONGITUDE|'][/types]" map_id="map-10" image="[types field='image-in-map-pin-pop-up' width='182' height='124' url='true' resize='crop' separator='|'][/types]" title="[types field='beach-name-pin' separator='|'][/types]"]
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-conditional]
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]

and the code using for the short code inside this view:

add_shortcode( 'pinnumber', 'pinnumber_shortcode' );
function pinnumber_shortcode($atts = []) {
$atts = array_change_key_case((array)$atts, CASE_LOWER);
//$atts = shortcode_atts(['location' => '', 'title' => '', 'image' => ''], $atts);

if (!empty($atts['location'])){
echo $atts['location'];
$ars = explode("|",$atts['location']);

foreach ($ars as $ar) {
$ps[] = explode(", ",$ar);
}

$title = array();
if (!empty($atts['title'])){
$title = explode("|",$atts['title']);
}

$images = array();
if (!empty($atts['image'])){
$images = explode("|",$atts['image']);
}

$shortCode = '';
foreach ($ps as $key => $p) {
$shortCode .= '[wpv-map-marker map_id="'.$atts['map_id'].'" marker_id="marker-'.($key+1).'" marker_icon="/wp-content/themes/Impreza-child/mappins/pinnum-'.($key+1).'.png" lat="'.$p[0].'" lon="'.$p[1].'"]';

if(array_key_exists(($key), $title) && !empty($title))
$shortCode .= '<h4>'.$title[$key].'</h4>';

if(array_key_exists(($key), $images) && !empty($images))
$shortCode .= '<img src='.$images[$key].' width="182" height="124" class="aligncenter size-thumbnail"/>';

$shortCode .= '[/wpv-map-marker]';
}
return $shortCode;
}
else return '';
}

#2675755

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

To display a map you will have to use the shortcodes that displays the map and marker.
- https://toolset.com/documentation/user-guides/maps-shortcodes/#wpv-map-render
- https://toolset.com/documentation/user-guides/maps-shortcodes/#wpv-map-marker

Instead of using view - what if you try to use the following code:

echo do_shortcode ('[wpv-map-render map_id="map-1"]');
echo do_shortcode ('[wpv-map-marker map_id="map-1" marker_id="marker-1" marker_field="wpcf-location"][/wpv-map-marker]');

Where:
- wpcf-location is the address custom field that holds the value of address string. Where you should just replace wpcf-location with your original address field if required.

Please check the following related tickets that might help you:
- https://toolset.com/forums/topic/how-do-i-display-a-map-with-php/#post-1824779
- https://toolset.com/forums/topic/display-map-on-page-with-php/

More info:
- https://toolset.com/course-lesson/displaying-a-simple-map/

#2675763

Dear Minesh

The first code for creating a short code would work. But we don't have just one marker to show up in the map. On the provided url for example, we do have 11 markers to show, and each marker does have it's own marker icon, starting from 1 to x, where x is the amount of locations we have defined on this post.

For this and for the second short code, we basically have instead of this:

[wpv-map-marker map_id="map-1" marker_id="marker-1" marker_field="wpcf-location"][/wpv-map-marker]

we should have this:

[pinnumber location="[types field='location' format='FIELD_LATITUDE, FIELD_LONGITUDE|'][/types]" map_id="map-10" image="[types field='image-in-map-pin-pop-up' width='182' height='124' url='true' resize='crop' separator='|'][/types]" title="[types field='beach-name-pin' separator='|'][/types]"]

This short code respectively php code for this short code is this:

add_shortcode( 'pinnumber', 'pinnumber_shortcode' );
function pinnumber_shortcode($atts = []) {
$atts = array_change_key_case((array)$atts, CASE_LOWER);
//$atts = shortcode_atts(['location' => '', 'title' => '', 'image' => ''], $atts);

if (!empty($atts['location'])){
echo $atts['location'];
$ars = explode("|",$atts['location']);

foreach ($ars as $ar) {
$ps[] = explode(", ",$ar);
}

$title = array();
if (!empty($atts['title'])){
$title = explode("|",$atts['title']);
}

$images = array();
if (!empty($atts['image'])){
$images = explode("|",$atts['image']);
}

$shortCode = '';
foreach ($ps as $key => $p) {
$shortCode .= '[wpv-map-marker map_id="'.$atts['map_id'].'" marker_id="marker-'.($key+1).'" marker_icon="/wp-content/themes/Impreza-child/mappins/pinnum-'.($key+1).'.png" lat="'.$p[0].'" lon="'.$p[1].'"]';

if(array_key_exists(($key), $title) && !empty($title))
$shortCode .= '<h4>'.$title[$key].'</h4>';

if(array_key_exists(($key), $images) && !empty($images))
$shortCode .= '<img src='.$images[$key].' width="182" height="124" class="aligncenter size-thumbnail"/>';

$shortCode .= '[/wpv-map-marker]';
}
return $shortCode;
}
else return '';
}

When I am using this:

echo do_shortcode('[pinnumber location="[types field='location' format='FIELD_LATITUDE, FIELD_LONGITUDE|'][/types]" map_id="map-10" image="[types field='image-in-map-pin-pop-up' width='182' height='124' url='true' resize='crop' separator='|'][/types]" title="[types field='beach-name-pin' separator='|'][/types]"]');

Then it isn't showing the way as I expecting.

I am not right sure, but it might be an issue with a short code in a short code and the correct using of " or ' on the correct place.

#2675795

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I can see within your function "pinnumber_shortcode" you are echoing the following line of code:

echo $atts['location'];

The above echo is part of the following code lines:

if (!empty($atts['location'])){
echo $atts['location'];
$ars = explode("|",$atts['location']);

foreach ($ars as $ar) {
$ps[] = explode(", ",$ar);
}

Can you please stop echoing the content?

#2675805

Oh, yes, that was it.

Quick question, how can I show in a ticket, if it's "code"?

Like this?
[php]