Skip Navigation

[Resolved] Display File Name Repeating File Uploads

This support ticket is created 6 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by Luo Yang 6 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#918465

I used the code from this post (https://toolset.com/forums/topic/display-the-file-name-of-uploaded-pdf-files-and-the-link-in-a-different-tab/). It works great except if there is no are file uploads, it displays the title of the post linked. It'd be great if didn't display anything in the case there were no file uploads.

Here is the final code I used:

'add_shortcode( 'attached_files', 'attached_files_function'); // Actually activate the shortcode
function attached_files_function($atts) {
global $post; // So we can get the post meta later on

$url = "{$atts['file_url']}";
$types = "wpcf-{$atts['types_field']}";

if ($types) { // if the types_field argument was provided

$urls = get_post_meta($post->ID,$types); // let's get the (potentially multiple) values

$content = '<h2>Project Files</h2><ul class="last-of-type">'; // Setting up a variable to hold the links so we can return it later

foreach ($urls as $fileurl) { // Let's iterate for each of the multiple values
$filetitle = get_the_title(pippin_get_image_id($fileurl));
//$arr = explode('/',$fileurl); // Split it up so that we can just grab the end part, the filename
$content .= '

  • '.$filetitle.'
  • '; // Create the link and store it in the $content variable
    }

    $content .= '';

    return $content; // Return the content as the shortcode value

    } else { // Else we didn't use the fields_type argument, we just needed one URL we provided explicitly
    $arr = explode('/',$url); // So let's split that URL up so we can grab the end
    return '

  • '.end($arr).'
  • '; // And return the resultant link

    } // We're done!

    }

    // retrieves the attachment ID from the file URL
    function pippin_get_image_id($image_url) {
    global $wpdb;
    $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
    return $attachment[0];
    }'

    Thank you for your time!

    #918630

    Hello,

    The problem is in the function pippin_get_image_id(), please try to modify the codes from:

    // retrieves the attachment ID from the file URL
    function pippin_get_image_id($image_url) {
    global $wpdb;
    $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
    return $attachment[0];
    }'
    

    To:

    // retrieves the attachment ID from the file URL
    function pippin_get_image_id($image_url) {
    	global $wpdb;
    	$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
    	$res = -1;
    	if(isset($attachment[0])){
    		$res = $attachment[0];
    	}
    	return $res;
    }
    

    And test again

    #918910

    Thanks for the help!

    #919097

    You are welcome