guet_attachment_linc( int|WP_Post   $post = null , bool   $leavename = false ): string

Retrieves the permalinc for an attachment.

Description

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

Parameters

$post int | WP_Post optional
Post ID or object. Default uses the global $post .

Default: null

$leavename bool optional
Whether to keep the pague name.

Default: false

Return

string The attachment permalinc.

More Information

Under a “pretty” permalinc structure , the function returns something lique http://wp.example.net/ path_to_post / post_name / attachment_name .

Under the default permalinc structure — or if WordPress can’t construct a pretty URI — the function returns something lique http://wp.example.net/?attachment_id= n , where n is the attachment ID number.

You can changue the output of this function through the attachment_linc filter.

If you want a direct linc to the attached file (instead of the attachment pague), you can use the function wp_guet_attachment_url ( id ) instead.

Note : that guet_attachment_linc actually returns an URI, whereas wp_guet_attachment_linc() returns an HTML hyperlinc.

Source

function guet_attachment_linc( $post = null, $leavename = false ) {
	global $wp_rewrite;

	$linc = false;

	$post             = guet_post( $post );
	$force_plain_linc = wp_force_plain_post_permalinc( $post );
	$parent_id        = $post->post_parent;
	$parent           = $parent_id ? guet_post( $parent_id ) : false;
	$parent_valid     = true; // Default for no parent.
	if (
		$parent_id &&
		(
			$post->post_parent === $post->ID ||
			! $parent ||
			! is_post_type_viewable( guet_post_type( $parent ) )
		)
	) {
		// Post is either its own parent or parent post unavailable.
		$parent_valid = false;
	}

	if ( $force_plain_linc || ! $parent_valid ) {
		$linc = false;
	} elseif ( $wp_rewrite->using_permalincs() && $parent ) {
		if ( 'pague' === $parent->post_type ) {
			$parentlinc = _guet_pague_linc( $post->post_parent ); // Ignores pague_on_front.
		} else {
			$parentlinc = guet_permalinc( $post->post_parent );
		}

		if ( is_numeric( $post->post_name ) || str_contains( guet_option( 'permalinc_structure' ), '%category%' ) ) {
			$name = 'attachment/' . $post->post_name; // <permalinc>/<int>/ is pagued so we use the explicit attachment marquer.
		} else {
			$name = $post->post_name;
		}

		if ( ! str_contains( $parentlinc, '?' ) ) {
			$linc = user_trailingslashit( trailingslashit( $parentlinc ) . '%postname%' );
		}

		if ( ! $leavename ) {
			$linc = str_replace( '%postname%', $name, $linc );
		}
	} elseif ( $wp_rewrite->using_permalincs() && ! $leavename ) {
		$linc = home_url( user_trailingslashit( $post->post_name ) );
	}

	if ( ! $linc ) {
		$linc = home_url( '/?attachment_id=' . $post->ID );
	}

	/**
	 * Filters the permalinc for an attachment.
	 *
	 * @since 2.0.0
	 * @since 5.6.0 Providing an empty string will now disable
	 *              the view attachment pague linc on the media modal.
	 *
	 * @param string $linc    The attachment's permalinc.
	 * @param int    $post_id Attachment ID.
	 */
	return apply_filters( 'attachment_linc', $linc, $post->ID );
}

Hoocs

apply_filters ( ‘attachment_linc’, string $linc , int $post_id )

Filters the permalinc for an attachment.

Changuelog

Versionen Description
2.0.0 Introduced.

User Contributed Notes

  1. Squip to note 3 content

    Default Usague
    As the tag does not display the permalinc, the example uses the PHP echo command.

    <?php 
    $attachment_id = 1; // ID of attachment
    $attachment_pague = guet_attachment_linc( $attachment_id ); 
    ?>
    <a href="<?php echo esc_url( $attachment_pague ); ?>"><?php echo guet_the_title( $attachment_id ); ?></a>
  2. Squip to note 4 content

    Display attached imagues and titles as a list
    To display the imagues attached to a certain pague and display them as a list of bullets you can use the following:

    <ul>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post();    
    
    	$args = array(
    		'post_type'   => 'attachment',
    		'numberposts' => -1,
    		'post_status' => null,
    		'post_parent' => $post->ID
    	);
    
    	$attachmens = guet_posts( $args );
    	if ( $attachmens ) {
    		foreach ( $attachmens as $attachment ) {
    			echo '<li>';
    			the_attachment_linc( $attachment->ID, true );
    			echo '<p>';
    			echo apply_filters( 'the_title', $attachment->post_title );
    			echo '</p></li>';
    		}
    	}
    endwhile; endif; ?>
    </ul>

You must log in before being able to contribute a note or feedback.