You are here:  » WordPress shortcode in PHP file


WordPress shortcode in PHP file

Submitted by Pascal on Wed, 2016-06-08 13:40 in

Hi!

I want to use the price comparison table in my WooCommerce single product php-file. I imported my products just with the same names as in the PT database.

To get it working I just need to have the possibility to use this shortcode outside the content in a php file. The php code prints out the right name.

[pto prices="<?php the_title(); ?>"]

Is this anyhow possible?

Cheers,

Pascal

Submitted by support on Wed, 2016-06-08 14:14

Hello Pascal,

Have a go with:

<?php
 
print do_shortcode('[pto prices="'.the_title().'"]'); 
?>

Cheers,
David.
--
PriceTapestry.com

Submitted by Pascal on Wed, 2016-06-08 20:14

Thanks David!

I just found out that I need to use .get_the_title(). instead of .the_title(). to get it working.

It works now!

Cheers,

Pascal

Submitted by babyuniverse on Mon, 2018-12-31 03:32

Hi,

I am trying to gernate the following shortcode in PHP

[pto featured="brand:Nike:3"]

I am using the following code. In this example the category/taxonomy name is Nike

<?php
 
print do_shortcode('[pto featured="brand:'single_cat_title(). ':3"]');
?>

Any Ideas, Thanks Richard

Submitted by support on Mon, 2018-12-31 10:31

Hi Richard,

I assume you would be looking, in this example, for 3 random products from the brand "Nike", in which case if you were happy to have the results in search results format then you could use the modified search shortcode as per your question in this thread using:

<?php
 
print do_shortcode('[pto search="brand:'single_cat_title(). '" results="3" sort="rand"]');
?>

Of course, if you'd prefer the results in Featured Products layout, just let me know and I'll work out the special featured products sections mods to achieve the same...

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by babyuniverse on Tue, 2019-01-01 07:45

Hi David,

I have used the code above and it doesn't display any products has the following.
"Nike
Sorry, there are no results to display."

The reason I was looking for merchant is because of the way I have setup my site. Basically I need both.

post name = names of the merchant (Asos, M&S, Boohoo etc)
taxonomy = brand (Nike, Reebok, Levis)

So on the merchant pages I would like [pto featured="merchant:Asos:3"] and on the brand pages I would like [pto featured="brand:Nike:3"]

These work as short codes however I am trying to hard code them into my theme.

Thnaks
Richard

Submitted by support on Tue, 2019-01-01 12:39

Hello Richard,

Ah - single_cat_title() displays rather than returns the value by default so it would need to be passed FALSE as the $display parameter - have a go with for example;

<?php print do_shortcode('[pto featured="brand:'.single_cat_title('',FALSE).':3"]'); ?>

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by babyuniverse on Wed, 2019-01-02 09:01

Hi Mate,

That seems to work, how do I make it random products. Currently looks AtoZ

Thanks
Richard

Submitted by support on Wed, 2019-01-02 09:28

Hi Richard,

I know from other posts that you have made changed to pto_featured.php to support, for example, the format:

brand:Brand Name:3

...so if you edit that file and look for a specific handler for "brand", you may have:

  case "brand":
    ...

In the code to construct the SQL immediately below that point, look for something like:

  LIMIT ".$parts[2];

...and at this point you can insert the order by clause to make the results random:

  ORDER BY RAND() LIMIT ".$parts[2];

If you're not sure from the modifications where to add ORDER BY, if you could post that section of code from pto_featured.php from around line 37 beginning:

  if ($section)

...and I'll check it out further with you...

Cheers,
David.
--
PriceTapestry.com

Submitted by babyuniverse on Thu, 2019-01-03 07:28

thanks mate. that is awesome

Submitted by babyuniverse on Thu, 2019-01-03 08:19

[pto featured="merchant:Tony Bianco:10"] and [featured="brand:'.single_cat_title('',FALSE).':20"]

Are both working as required. One more question, how do I make these link straight to buyURL and not to product page?

Submitted by support on Thu, 2019-01-03 09:52

Hi,

In pto_featured.php look for the following code around line 149:

        $each = str_replace("%PRODUCT_URL%",pto_common_productHREF($row),$each);

...and REPLACE with:

        if ($row->numMerchants==1)
        {
          $each = str_replace("%PRODUCT_URL%",pto_common_buyURL($row),$each);
        }
        else
        {
          $each = str_replace("%PRODUCT_URL%",pto_common_productHREF($row),$each);
        }

The IF condition will ensure that the direct links are only made if the result is not compared linking to the product page if they are so that the preferred link can be selected. If you'd prefer to make a re-query in this case to link to [one of] the lowest priced merchants just let me know...

Cheers,
David.
--
PriceTapestry.com