• Resolved rgb_life

    (@rgb_life)


    I really want to use this plugin but I can’t do the simplest thing.

    What I want to do is just call out the URL to a single attachment to drop it into the href of a link for the user to download.

    Actually, I’m not sure if that’s the right way to allow the user to download an attachment, but basically, right now I have no idea how to access the contents of the attachments array.

    When I use the “exists” function, it returns the count of attachments. This seems fine.
    When I use the “get” function, I get an array that houses the info about the attachment I loaded. Things seem to be working fine, I just can’t pull the contents from within the loop.

    Code:

    // Check for attachments… or count them??
    $attachments_yes_no = wpba_attachments_exist(array(
        'post_id'             => $post->ID,
        'show_post_thumbnail' => true
    ));
    
    // Get attachments
    $about_attachments = wpba_get_attachments(array(
        'post_id'             => $post->ID,
        'show_post_thumbnail' => true
    ));

    Results of array prints:

    Any attachments?

    1

    Attachments Array print:

    Array ( [0] => WP_Post Object ( [ID] => 113 [post_author] => 1 [post_date] => 2014-01-11 06:24:33 [post_date_gmt] => 2014-01-11 06:24:33 [post_content] => This is a sample contract acting as a filler file. [post_title] => resume [post_excerpt] => sample contract [post_status] => inherit [comment_status] => open [ping_status] => open [post_password] => [post_name] => ams_pltarc_contract [to_ping] => [pinged] => [post_modified] => 2014-01-11 06:24:33 [post_modified_gmt] => 2014-01-11 06:24:33 [post_content_filtered] => [post_parent] => 109 [guid] => http://localhost/sachiko-beck/wp-content/uploads/2014/01/AMS_PltArc_contract.pdf [menu_order] => 1 [post_type] => attachment [post_mime_type] => application/pdf [comment_count] => 0 [filter] => raw ) [1] => WP_Post Object ( [ID] => 112 [post_author] => 1 [post_date] => 2014-01-11 03:04:52 [post_date_gmt] => 2014-01-11 03:04:52 [post_content] => [post_title] => Sachiko Beck Picture [post_excerpt] => [post_status] => inherit [comment_status] => open [ping_status] => open [post_password] => [post_name] => my-picture [to_ping] => [pinged] => [post_modified] => 2014-01-11 03:04:52 [post_modified_gmt] => 2014-01-11 03:04:52 [post_content_filtered] => [post_parent] => 109 [guid] => http://localhost/sachiko-beck/wp-content/uploads/2014/01/My-picture.jpg [menu_order] => 2 [post_type] => attachment [post_mime_type] => image/jpeg [comment_count] => 0 [filter] => raw ) )

    http://wordpress.org/plugins/wp-better-attachments/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author dholloran

    (@dholloran)

    Your almost there the only thing your really missing is a loop and wp_get_attachment_url(). Here is one way I would do it there may be other ways possibly use a custom WP_Query instead of wpba_get_attachments() if you want more flexibility. Since they are actual WordPress attachments you can query them like any other post type.

    <?php
    // Get attachments
    $about_attachments = wpba_get_attachments(array(
    	'post_id'             => $post->ID,
    	'show_post_thumbnail' => true
    ));
    
    foreach ( $about_attachments as $attachment ) {
    	$att_url   = wp_get_attachment_url( $attachment->ID );
    	$att_title = $attachment->post_title;
    	// For a title you could also use something like this if you want the filename
    	// I have not tested this so it may need to be tweaked.
    	// $upload_dir = wp_upload_dir( 'yyyy/mm' );
    	// $att_title  = str_replace( $upload_dir, '', $att_url );
    	?>
    	<a href="<?php echo esc_url( $att_url );  ?>"><?php echo esc_html( $att_title ); ?></a>
    <?php } // foreach

    Please let me know if this does not answer your question.

    Thread Starter rgb_life

    (@rgb_life)

    Hey! Thanks for getting back so quickly. This looks like a good solution. I’ll post again after I try it.

    Thread Starter rgb_life

    (@rgb_life)

    Thank you Thank you Thank you!!

    I guess I’m still getting familiar with theming with WordPress overall. I wasn’t aware of the “wp_get_attachment_url()” function.

    I could do exactly what I want by targeting the first item in the array and dropping the url into an href.

    // Get attachments
    $about_attachments = wpba_get_attachments(array(
        'post_id'             => $post->ID,
        'show_post_thumbnail' => true
    ));
    // get URL
    $resume_url = wp_get_attachment_url($about_attachments[0]->ID);
    
    <p><a href="<?php echo $resume_url; ?>">See my resume</a></p>

    Thanks again. This plugin is a lot more approachable than the more robust alternative “Attachments.” A couple more examples in the documentation or concepts to study (such as this wp_get_attachment_url() function and its brethren), and I think it will be easier to pick up for novices like myself.

    Thread Starter rgb_life

    (@rgb_life)

    Oops, I forgot to mark this as resolved.

    Thanks again. You rock, dholloran!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘drawing URL from within LOOP’ is closed to new replies.