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!
Hey thanks for your reply,
The premalinks on the site for products are set to the defualt so in there it show this:
http://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
http://www.website.co.il/sample-page/
When it set to product id i get this
http://www.website.co.il/?p=8282
And when i enter the page it redirect me to the correct product url:
http://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 🙂
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
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();