WordPress.org

Ready to get started?Download WordPress

Forums

get_posts and foreach (2 posts)

  1. Andrew
    Member
    Posted 1 year ago #

    I'm using get_posts to display attachments of my posts:

    <?php
    $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
    $attachments = get_posts($args);
    if ($attachments) {
    foreach ( $attachments as $attachment ) {
    echo apply_filters( 'the_title' , $attachment->post_title );
    the_attachment_link( $attachment->ID , false );
    }
    }
    ?>

    How can I change the code to exclude the echo so I can simply write out the HTML for the attachments?

  2. Andrew
    Member
    Posted 1 year ago #

    Okay, to elaborate a little bit more on my question, I'd like to get the attachment uri to use in a shortcode in my template.

    So how can I just get the attachment uri for use in a formatted link like so:

    [Shortcode src="wp_get_attachment_url"]

    based on the code in the previous post? Preferably without having to use PHP but HTML instead.

    The code is pulling the post's attachments but how can I format the display of the attachments?

    Please help.

    Thank you,

    Andrew

Topic Closed

This topic has been closed to new replies.

About this Topic