{"id":1877,"date":"2017-07-29T08:31:01","date_gmt":"2017-07-29T08:31:01","guid":{"rendered":"http:\/\/docs.contentviewspro.com\/?p=1877"},"modified":"2023-12-28T08:22:32","modified_gmt":"2023-12-28T08:22:32","password":"","slug":"media-link-post-attached","status":"publish","type":"docs","link":"https:\/\/contentviewspro.com\/documentation\/article\/media-link-post-attached\/","title":{"rendered":"Link the Media file to the post which it is attached to"},"content":{"rendered":"<p>Content Views Pro helps you to show Media easily.<br \/>\nBy default, media title, thumbnail and Read More button will link to itself. To link to the post which it is attached to, please add this code to file <strong>functions.php<\/strong> of your active theme:<\/p>\n<pre style=\"font-size:13px\">\n\/\/ Content Views Pro - Media link to post which it is attached to\nadd_filter( 'pt_cv_field_href', 'cvp_theme_media_link_post', 100, 2 );\nfunction cvp_theme_media_link_post( $href, $post ) {\n\tglobal $pt_cv_id;\n\tif ( $pt_cv_id === 'VIEW_ID' ) {\n\t\tif ( !empty( $post->post_parent ) ) {\n\t\t\t$href = get_permalink( $post->post_parent );\n\t\t}\n\t}\n\n\treturn $href;\n}\n<\/pre>\n<p><em>(Please replace <strong>VIEW_ID<\/strong> with ID of your View)<\/em><\/p>\n<p>If you want to keep the media title, thumbnail links to itself, but links the Read More button to the post, please add this code to file <strong>functions.php<\/strong> of your active theme:<\/p>\n<pre style=\"font-size:13px\">\n\/\/ Content Views Pro - Media \"Read More\" link to post which it is attached to\nadd_filter( 'pt_cv_field_href', 'cvp_theme_media_readmore_link_post', 100, 2 );\nfunction cvp_theme_media_readmore_link_post( $href, $post ) {\n\tglobal $pt_cv_id;\n\tif ( $pt_cv_id === 'VIEW_ID' ) {\n\t\t$trace = debug_backtrace();\n\n\t\tif ( !empty( $trace[ 5 ][ 'function' ] ) && $trace[ 5 ][ 'function' ] === '_field_content' && !empty( $post->post_parent ) ) {\n\t\t\t$href = get_permalink( $post->post_parent );\n\t\t}\n\t}\n\n\treturn $href;\n}\n<\/pre>\n<p><em>(Please replace <strong>VIEW_ID<\/strong> with ID of your View)<\/em><\/p>\n<p>Best regards,<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Content Views Pro helps you to show Media easily. By default, media title, thumbnail and Read More button will link to itself. To link to the post which it is attached to, please add this code to file functions.php of your active theme: \/\/ Content Views Pro &#8211; Media link to post which it is &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/contentviewspro.com\/documentation\/article\/media-link-post-attached\/\"> <span class=\"screen-reader-text\">Link the Media file to the post which it is attached to<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"doc_category":[25],"doc_tag":[21],"class_list":["post-1877","docs","type-docs","status-publish","hentry","doc_category-adjustment","doc_tag-custom-code"],"year_month":"2026-09","word_count":222,"total_views":"0","reactions":{"happy":"1","normal":"0","sad":"0"},"author_info":{"name":"CVP","author_nicename":"cvp","author_url":"https:\/\/contentviewspro.com\/documentation\/author\/cvp\/"},"doc_category_info":[{"term_name":"Adjustment","term_url":"https:\/\/contentviewspro.com\/documentation\/cats\/adjustment\/"}],"doc_tag_info":[{"term_name":"custom PHP code","term_url":"https:\/\/contentviewspro.com\/documentation\/tags\/custom-code\/"}],"_links":{"self":[{"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/docs\/1877","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/comments?post=1877"}],"version-history":[{"count":2,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/docs\/1877\/revisions"}],"predecessor-version":[{"id":5489,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/docs\/1877\/revisions\/5489"}],"wp:attachment":[{"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/media?parent=1877"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/doc_category?post=1877"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/doc_tag?post=1877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}