WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Getting attachment URL for custom post type (3 posts)

  1. jtm12
    Member
    Posted 1 year ago #

    I'm trying to create custom pages for clients. Specific documents would be uploaded to protected folders, and the titles and URLs would be listed on these custom pages. There is no crossover of material. Clients should not see each other's material, and I have the pages and upload folders protected.

    I have a bad system set up right now for how the document names are added to these client pages. It requires too many steps.

    In trying to streamline it, I thought about using custom post types for each client and then attaching one document per post and getting the title of the custom post and the URL of the attachment in my loop. Whoever was inputting the document would not have to get the URL manually and move it to the client page, which is what we're doing now. My plan worked perfectly in my mind. It's not going so well on the code side. Here is what I have:

    `global $post;
    $args = array( 'numberposts' => 3, 'post_type' => Clientone);
    $myposts = get_posts( $args );foreach( $myposts as $post ) : setup_postdata($post); ?>
    <h2><a href="document URL would go here" ><?php the_title(); ?></a></h2>`

    I've tried many variations of echo wp_get_attachment_url for the attachment URL, and I'm not coming up with the right code to generate the URL. Has anyone else run into this?

    Thank you.

  2. jtm12
    Member
    Posted 1 year ago #

    I'm getting close. I haven't figured out how to limit this href to one attachment link yet (so, if there's more than one attachment, the link is screwed up), but here's what I have:

    <?php
    	global $post;
    	$args = array( 'numberposts' => 3, 'post_type' => Client);
    	$myposts = get_posts( $args ); foreach( $myposts as $post ) :	setup_postdata($post); ?>
    
    <h2><a href="
    <?php
     $args2 = array(
       'post_type' => 'attachment',
       'numberposts' => -1,
       'post_status' => null,
       'post_parent' => $post->ID
      );
    
      $attachments = get_posts( $args2 );
           if ( $attachments ) {
            foreach ( $attachments as $attachment ) {
    
              echo wp_get_attachment_url( $attachment->ID, true );
    
              }
         } ?>
    "> 
    
    <?php the_title(); ?></a></h2> 
    
    <?php endforeach; ?>
    
    <?php wp_reset_query(); ?>
  3. jtm12
    Member
    Posted 1 year ago #

    This is working. Many thanks to others who post on the WordPress forum with their code. I pieced a few things together for this.

    <?php query_posts('post_type=Client'); ?>
    	<?php while (have_posts()) : the_post(); ?>
    		<?php
    			$args = array(
    			'order'          => 'ASC',
    			'post_type'      => 'attachment',
    			'post_parent'    => $post->ID,
    			'post_status'    => null,
    			'numberposts'    => 1,
    
    		);
    
    		$attachments = get_posts($args);
    			if ($attachments) {
    			foreach ($attachments as $attachment) { ?>
    			<h2><a href="<?php echo wp_get_attachment_url( $attachment->ID, true ); ?>">
    			<?php the_title(); ?></a></h2>
    			<?php }
    		}
    	?>
    	<?php endwhile;?>

Topic Closed

This topic has been closed to new replies.

About this Topic