-2

I need to insert following dynamic URL:

echo $myfy->get_retrieve_cart_url( $post->ID, $landing_page )

into:

echo "<a href='"LINK"'> . get_title() . </a>";

Basically, I need to replace the "LINK" with above URL, I have tried all combinations that I've thought about and I'm getting a string error, Can anybody give me a helpline?

1

There are two workarounds:

If the first line you're mentioning is inside a void function:

You have to create a wrapper function and use ob_ functions to get the real link:

<?php
function echo_link($post, $landing_page) {
    global $myfy;
    echo $myfy->get_retrieve_cart_url( $post->ID, $landing_page );
}
function get_link($post, $landing_page){
    ob_start();
    echo_link($post, $landing_page);
    return ob_get_clean();
}

// And finally in your result:
echo "a href='" . get_link($post, $landing_page) . "'>.get_title()";

If the first line you're mentioning is not inside a void function or you can simply duplicate it without concerning about the future changes:

<?php
$link = $myfy->get_retrieve_cart_url( $post->ID, $landing_page );
echo "a href='" . $link . "'>.get_title()";

By the way, You might forget about those dots (.) while trying to concatenate your strings.

  • I was able insert the link but url is not resolved correct, this goes as a part of a custom post query in woocommerce plugin. – Roshan Deshapriya Sep 3 '18 at 13:18
  • Double check your call and give more information about your plugin or at least the source code of get_retrieve_cart_url function. – Farsad Sep 3 '18 at 15:21
  • I already have page template which load the cart successfully with this above url under custom post types, so though to do a plugin for same purpose. This url load inside a query. I'll do test your code and share the updated code for more insight. – Roshan Deshapriya Sep 3 '18 at 20:32
  • I was able to get the correct path by adding $post to your 1st function (global $myfy, $post;) Thanks mate for support. – Roshan Deshapriya Sep 3 '18 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.