Support » Fixing WordPress » Getting attachment URL for custom post type

  • Resolved jtm12


    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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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:

    	global $post;
    	$args = array( 'numberposts' => 3, 'post_type' => Client);
    	$myposts = get_posts( $args ); foreach( $myposts as $post ) :	setup_postdata($post); ?>
    <h2><a href="
     $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(); ?>

    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(); ?>
    			$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;?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Getting attachment URL for custom post type’ is closed to new replies.