• Resolved yydevelopment

    (@yydevelopment)


    Hey i am trying to get the product url with the wordpress function of get_the_permalink() with the product page id.

    It seems like instead of getting clean url like

    website.com/my-page-name/

    I get something like this

    website.com/?p=229

    So i get the url by ID and it make redirect to the normal url. Is there a way to pass that and get the correct URL with product id?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi @yydevelopment,

    Without seeing the code it’s difficult to know exactly what is going on. How are the permalinks set in Settings > Permalinks?

    When I use a snippet like this on my site, I’ll get the page slug for the shop page when I use “post name.” With the “default” option, I’ll get something like ?p=ID.

    
    function ijab_shop_permalink() {
    	echo get_the_permalink( wc_get_page_id( 'shop' ) );
    }
    add_action( 'wp_head', 'ijab_permalink' );
    

    Could you include the code you’re using?

    Thanks!

    Thread Starter yydevelopment

    (@yydevelopment)

    Hey thanks for your reply,

    The premalinks on the site for products are set to the defualt so in there it show this:
    https://www.website.co.il/product/sample-product/

    And when i chooce the defualt and save it set it to custom structure and in there it show this:
    product/

    The way i use to the code is simple get the get_the_permalink with the id so something like this:

    echo get_the_permalink(4386);

    Now when the id is that of a page i get the regular page url
    https://www.website.co.il/sample-page/

    When it set to product id i get this
    https://www.website.co.il/?p=8282

    And when i enter the page it redirect me to the correct product url:
    https://www.website.co.il/product/product-name/

    On the shop the url is the correct when but when i try and get the product url with the get_the_permalink and the product id i get the short url that redirect the user to the right url.

    I don’t the the problem is the premalinks in this case. Is there other way to get the page url with an id and without the get_the_permalink?

    Thanks

    Hey @yydevelopment,

    Another way to approach that might be to get the “slug” of the item and construct the URL from it.

    
    $post_id = 45; //specify post id here
    $post = get_post($post_id); 
    $slug = $post->post_name;
    

    Then once you have the slug, you could make your own permalink with the slug as the ending.

    Just a thought ??

    Thread Starter yydevelopment

    (@yydevelopment)

    Hey thanks, I think it might cause problem if the permalink will change in the future so i guess the safe way is to keep it as it is.

    Thanks for the thought

    Thread Starter yydevelopment

    (@yydevelopment)

    Seems like this is the correct way to get the url if someone got the same problem

    $product = wc_get_product( $product_id );
    $product_permalink = $product->get_permalink();

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘get_the_permalink() not returning clean url’ is closed to new replies.