You want the permalink for the attachment’s parent post, so, if I am guessing correctly about the contents of $attachment
$parent_url = get_permalink($attachment->post_parent);
Related Posts:
- Remove subcategory slug from url
- How to allow WordPress to recognize a custom URL segment after a product URL and use a custom template?
- Getting 404 when using rewrite rule for posts
- Custom permalink structure only for default posts
- How to Group Posts by the First Letter or Number?
- Pretty URL with add_query_var
- get_permalink returns default link
- Why does rewrite rule work for page not for custom post type post?
- Dynamically generate child-URLs for posts or custom posts
- URL Rewriting if it is a single post / redirecting post – post type
- add_rewrite_rules for type of post
- Changing Permalinks for Default Post type to reflect navigation
- Custom Post Type – Category Rewrite – Remove Rewrite from Sitemap
- Duplicate Custom Post Type and Taxonomy Slug
- Visting slug for ‘post’ post type shows 404 not archive?
- Custom Post Type Rewrite To Include Parent Page(s)
- How do I Redirect a WordPress Page?
- Disable WordPress Archive Conflict Check
- $_GET parametters not working in an specific template
- Post-Archive like a page in a specific subdirectory
- Custom Post Type, post-formats associated to custom post type > 404 archives
- add_rewrite_rule and template redirect not working
- Add static text to post URLs
- My post permalinks are redirecting to the post archive page for some reason?
- How to add a ‘News’ section to specific posts in WordPress
- Add a url parameter to the current post and read it in a shortcode
- Prevent Archive URLs
- Slug collision between page, taxonomy and custom post type
- How to add category base prefix to wordpress permalink
- Multipage Post URL correction
- Fixing the WP Post Object for Custom Route
- Posts in Page – How to customize the URL?
- Use a custom post URL that is outside the blog root directory
- Rewrite a specific Post Format to URL slug
- Overwrite URL on blog posts
- Blog posts are not appearing as subs of Blog Page
- How to change url of posts?
- Post pages are redirecting to the 404 page in wordpress
- Get WordPress post content by post id
- How to create custom URL routes?
- Get page ID of page that is set as the posts page
- How do you create a “virtual” page in WordPress
- Need help with add_rewrite_rule
- Get Posts Under Custom Taxonomy
- How to limit the number of posts that WP_Query gets?
- Stop WordPress Wrapping Images In A “P” Tag
- When is the ‘post_content_filtered’ column in database cleared by WordPress?
- Turn a URL into an Attachment / Post ID
- GET the excerpt by ID
- Stop WordPress automatically adding tags to post content
- Determine if page is the Posts Page
- How to get ‘post_content’ without stripping tags?
- remove “index.php” from permalinks
- How to add an admin notice upon post save/update
- Print Current Post Index number within Loop
- Should we trust the post globals?
- Optimize post insert and delete for bulk operations?
- Custom pages with plugin
- Get the Current Page Number
- How do you add thumbnail support for custom post types?
- How to mark every 3rd post
- Change Posts per page count
- Keep featured content post in homepage with original order
- Change the “page” slug in pagination
- How to display related posts from same category?
- Can posts have parents?
- Is there a action hook for the “Empty Trash” button?
- Understanding add_rewrite_rule
- How to end the excerpt with a sentence rather than a word?
- How do I check if I linked to a post before I delete it?
- How to change the case of all post titles to “Title Case”
- Appending content with broken in 4.4
- How to get posts published between a date and today?
- ZIP up all images displayed in a [gallery] and offer as download link
- How to detect single.php (but not single-portfolio.php)?
- How to check if post meta key exists or not in wordpress database
- How to remove a column from the Posts page
- removing inline styles from wp-caption div
- What is the use of to_ping and pinged column?
- How to use “Add link” pop up for a WordPress widget
- web.config conflict on IIS
- Remove Categories / Tags From Admin Menu
- How to retrieve text only from wp_content() not from wp_excerpt()?
- Stop WP from creating “Sample Page” and “Hello World!” post
- Where is the old post permalink slug stored?
- How to Create Editable Blocks of Text for the Homepage?
- Keyboard shortcut for updating a page or post?
- Add custom Attachment Display Setting for images
- Rewriting post slug before post save
- Apply the_title() filter in post & page title, but not in menu title
- How to Change Order of Posts in Admin?
- is it possible to get next_post_link
- Get post content by ID
- How to get post creation date?
- Update post counts (published, draft, unattached) in admin interface
- Function to execute when a post is moved to trash .
- How to get post ID of the current page/post inside a widget?
- add action only on post publish – not update
- How to display by default only published posts/pages in the admin area?
- How do you add Read more … link to posts?