Skip Navigation

[Closed] Display Media Library title and description of a (PDF) file field in PHP code

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 9 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 -
- - - - - - -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 1 reply, has 2 voices.

Last updated by Waqas 9 years, 5 months ago.

Assisted by: Waqas.

Author
Posts
#261173

I am trying to display the title as given in the Media Library directly in the theme code. My field currently looks like this in the code (but it only outputs the url):

<?php echo types_render_field("technical_drawing") ?>

. This only outputs the path to the upload.

The file is an uploaded PDF-file but I guess there must be a one-fits-all method to achieve this. I've been looking at code with arrays mostly for alt text, but it doesn't really makes sense to me (I'm not a programmer).

Basicly I need to achieve an output such as this:

<a href="/wp-content/uploads/2014/01/mydocument.pdf">(here comes the title from the Media Library)</a>
#261596

Waqas
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

The 'types_render_field()' can return only the value of a custom field, and optionally allows you to format the output (details at https://toolset.com/documentation/user-guides/displaying-wordpress-custom-fields/).

However, the media uploaded in the media library, is treated as the 'attachment' post type. You can retrieve attachments with a post, using the following code:

<ul>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();    

 $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 '<li>';
           echo wp_get_attachment_image( $attachment->ID, 'full' );
           echo '<p>';
           echo apply_filters( 'the_title', $attachment->post_title );
           echo '</p></li>';
          }
     }

 endwhile; endif; ?>
</ul>

This will list all the attached media with a post. Please notice the '$attachment->post_title' in the foreach loop, to fetch the title of the media.

The 'File' type of custom field in Types, only offers to store the URL of uploaded file.

Please let me know if I can help you with anything related.

The topic ‘[Closed] Display Media Library title and description of a (PDF) file field in PHP code’ is closed to new replies.