Skip Navigation

[Resolved] Passing values to functions in shortcode

This thread is resolved. Here is a description of the problem and solution.

Problem: I have Stores and Products custom post types. I have access to a Store ID as a custom field value in a Product post. I would like to be able to use that Store ID to display information about the Store post, but I can't figure out how to use it in a shortcode.

Solution: If you know the post ID, it's easier to use the built-in "item" attribute to display information about another post.

Offer Store: [wpv-post-field name='offer-store']
Title: [wpv-post-title item="[wpv-post-field name='offer-store']"]
URL:  [wpv-post-url item="[wpv-post-field name='offer-store']"]
This support ticket is created 5 years, 3 months 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by nicolaS-3 5 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1371313

Hi,
first of all I want to say I am not a PHP programmer, even though I broadly understand how a PHP function works.
I have two custom post types: stores and products. in Stores I have Store ID and Store Name. In Products, for each product I have the Store ID but not the name.
I have created a view that loops on products and works perfectly, but of course it returns the store code while I'd like to show the store name (with link). In my theme there is a function that returns the store name passing the ID and I tried to use it. I have copied the function and created a shortcode (as suggested in many of your solutions to other users) and it partially works. I manage to get the right store ID for each product, but it doesn't return the shop name (get_the_title). Can you help here ?
PS - I know I can create a one-to-many relationship, but that's more complex and for now I just need to show the store name ...

In the view
[store_of_product store=[wpv-post-field name="offer_store"]][/store_of_product]

Function in functions.php

function store_of_product( $atts ) {
extract( shortcode_atts( array(
'store' => ''), $atts) );
return '' . get_the_title( $store ) . '';
}
add_shortcode('store_of_product', 'store_of_product');

#1371453

Hi, if you know the store ID it might be simpler to just use the built-in features to display the store post title and store post link. For example:

Offer Store: [wpv-post-field name='offer-store']
Title: [wpv-post-title item="[wpv-post-field name='offer-store']"]
URL:  [wpv-post-url item="[wpv-post-field name='offer-store']"]

If you're having trouble implementing a custom shortcode, you can use the "die" function to output debug information to the screen.

function store_of_product( $atts ) {
extract( shortcode_atts( array(
'store' => ''), $atts) );
die( 'Store: ' . $store );
return '<a href="' . get_permalink( $store ) . '">' . get_the_title( $store ) . '</a>';
}
add_shortcode('store_of_product', 'store_of_product');

That should give you an idea of what's being passed into the shortcode attribute "store"

#1373879

My issue is resolved now. Thank you!