Switch language

wpseek.com
A WordPress-centric search engine for devs and theme authors




get_attachment_link [ WordPress Function ]

get_attachment_link ( $id = false )
Parameters:
  • (int) $id Optional. Post ID.
Returns:
  • (string)
Defined at:



Retrieve permalink for attachment.

This can be used in the WordPress Loop or outside of it.

Source


<?php
function get_attachment_link($id false) {
    global 
$post$wp_rewrite;

    
$link false;

    if ( ! 
$id)
        
$id = (int) $post->ID;

    
$object get_post($id);
    if ( 
$wp_rewrite->using_permalinks() && ($object->post_parent 0) && ($object->post_parent != $id) ) {
        
$parent get_post($object->post_parent);
        if ( 
'page' == $parent->post_type )
            
$parentlink _get_page_link$object->post_parent ); // Ignores page_on_front
        
else
            
$parentlink get_permalink$object->post_parent );

        if ( 
is_numeric($object->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
            
$name 'attachment/' $object->post_name// <permalink>/<int>/ is paged so we use the explicit attachment marker
        
else
            
$name $object->post_name;

        if ( 
strpos($parentlink'?') === false )
            
$link user_trailingslashittrailingslashit($parentlink) . $name );
    }

    if ( ! 
$link )
        
$link home_url"/?attachment_id=$id);

    return 
apply_filters('attachment_link'$link$id);
}
?>

Examples [ wp-snippets.com ]

Top Google zoekresultaten

Meer ...

Gebruikersdiscussies [ wordpress.org ]

0 User Note(s)

Nog geen één. Wees de eerste!

Nieuw toevoegen ...



HTML5 Powered with CSS3 / Styling, Performance & Integration, and Semantics