Skip Navigation

[Resolved] How to php code to get a specific field

This support ticket is created 5 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

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 6 replies, has 3 voices.

Last updated by fred-r.M 5 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#1383783

Tell us what you are trying to do? We are having some custom code:

$str .= '<span class="locality" itemprop="addressLocality">{here my variable used to be}</span>'."\n";

Where you see {here my variable used to be} - we like to have some php code, replaced for [wpv-post-taxonomy type='location' format='name']

Is there any documentation that you are following? No found anything

Is there a similar example that we can see? We like to have something similar like this:

<span class="locality" itemprop="addressLocality">[wpv-post-taxonomy type='location' format='name']</span>

What is the link to your site?

hidden link

#1383835

Hello,

You can try WordPress function do_shortcode(), for example:

echo do_shortcode( "[wpv-post-taxonomy type='location' format='name']" );

More help:
https://developer.wordpress.org/reference/functions/do_shortcode/

#1383961

Dear Luo Yang

We might need to mention, that it will be inside an existing php function:

function display_schema_org() {
  
  global $post;
  $str = '';
   
  $schema_org = get_post_meta($post->ID,'wpcf-schema-org',true);
  $featured_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium', true);
  $thumb_url = $featured_image[0];
  $alt = get_post_meta($featured_image, '_wp_attachment_image_alt', true);
    
  if(!empty($schema_org)){
       $str .=  '<div class="rat" itemscope="" itemtype="<em><u>hidden link</u></em>'.$schema_org.'">'."\n";
       $str .=  '<span itemprop="name">'.$post->post_title.'</span>'."\n";
       $str .=  '<div itemprop="description">'.substr($post->post_content, 0, 600).'</div>'."\n";
   }
  if ($thumb_url != '') {
	   $str .=  '<img itemprop="image" class="embed-responsive-item" src="'.$thumb_url.'" alt="'.$wpv_post_title.'">'."\n";
   }   
       $str .= '<div itemprop="address" itemscope="" itemtype="<em><u>hidden link</u></em>">'."\n";
       $str .= '<span class="region" itemprop="addressRegion">PKT</span>'."\n";
       $str .= '<span class="locality" itemprop="addressLocality">'."\n";
       $str .= '<div class="country-name" itemprop="addressCountry">Thailand</div>'."\n";
       $str .= '</div>';
       $str .= '</div>';
	   
  return $str;
}
add_shortcode( 'show_schemaorg', 'display_schema_org' );

So the string we get will be in a line like [php]$str .= '<[php]

#1385615

Minesh
Supporter

Languages: English (English )

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

Luo is on vacation. This is Minesh here and I'll take care of this ticket in Luo's absence. Hope this is OK.

I see you are using the custom code that generates string as per your requirement.

What if you try to use the following code - as you can see I've added the variable $shortcode_value that contains the value of your location taxonomy and I've modified the line where you concat the string as given under:

  $str .= '<span class="locality" itemprop="addressLocality">'.$shortcode_value.'</span>\n';

Here is the full code you should try to use:

function display_schema_org() {
   
  global $post;
  $str = '';
    
  $schema_org = get_post_meta($post->ID,'wpcf-schema-org',true);
  $featured_image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium', true);
  $thumb_url = $featured_image[0];
  $alt = get_post_meta($featured_image, '_wp_attachment_image_alt', true);
  $shortcode_value = do_shortcode( "[wpv-post-taxonomy type='location' format='name']" );
     
  if(!empty($schema_org)){
       $str .=  '<div class="rat" itemscope="" itemtype="<em><u>hidden link</u></em>'.$schema_org.'">'."\n";
       $str .=  '<span itemprop="name">'.$post->post_title.'</span>'."\n";
       $str .=  '<div itemprop="description">'.substr($post->post_content, 0, 600).'</div>'."\n";
   }
  if ($thumb_url != '') {
       $str .=  '<img itemprop="image" class="embed-responsive-item" src="'.$thumb_url.'" alt="'.$wpv_post_title.'">'."\n";
   }   
       $str .= '<div itemprop="address" itemscope="" itemtype="<em><u>hidden link</u></em>">'."\n";
       $str .= '<span class="region" itemprop="addressRegion">PKT</span>'."\n";
      $str .= '<span class="locality" itemprop="addressLocality">'.$shortcode_value.'</span>\n';
      $str .= '<div class="country-name" itemprop="addressCountry">Thailand</div>'."\n";
       $str .= '</div>';
       $str .= '</div>';
        
  return $str;
}
add_shortcode( 'show_schemaorg', 'display_schema_org' );
#1385817

Dear Minesh

For wpv-post-taxonomy type='location' format='name' - do I need to use the slug, as we have the taxonomy Locations, but the slug is tourist-information.

An for every tourist-spot, we choose the Location where it is in.

Regards

#1385855

Minesh
Supporter

Languages: English (English )

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

For wpv-post-taxonomy type='location' format='name' - do I need to use the slug, as we have the taxonomy Locations, but the slug is tourist-information.
==>
Sorry, using [wpv-post-taxonomy] shortcode, you can list the terms attached to your current post.

More info:
=> https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-taxonomy

If your custom taxonomy slug is tourist-information, then you should use the shortcode:

[wpv-post-taxonomy type='tourist-information' format='name']
#1386533

My issue is resolved now. Thank you!