Skip Navigation

[Resolved] Need to display a file's (pdf doc) media title as a link in a URL field

This support ticket is created 6 years, 3 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.

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)

This topic contains 8 replies, has 2 voices.

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

Assisted by: Luo Yang.

Author
Posts
#1116642

Tell us what you are trying to do?
I'm trying to create an easy to use method for a client to load a pdf document into the media library, add a title, and see the title as the link on the front end in the page.

Is there any documentation that you are following?
I had asked about using an image to achieve this, but creating an image for each pdf involves designer work for each upload, so I will need further assistance.

Is there a similar example that we can see?

What is the link to your site?
hidden link is the development site.

If you need admin login please send private message.

#1117040

Hello,

There isn't such a built-in feature within Types shortcode, it needs custom code, you can try the solution of another thread:
https://toolset.com/forums/topic/file-post-field-displaying-file-title-text-rather-than-file-path/#post-562737

For your reference.

#1117324

I've added the code (inserting my types field name and leaving everything else as it is) to my functions.php child theme file, but I have one question about adding the shortcode to the link in the content template.
This is what I have:
[my_file_name types_field="certifications"]

But it is still returning the file URL.

Can I ask you to login and check the functions file for me? Or is there a simple mistake in the link shortcode?

#1118098

OK, please provide a test site with the same problem, also point out the problem page URL and view URL, I need a live website to test and debug the custom codes. thanks

#1122807

ftp portal is 21

#1123146

Thanks for the details, I can login your website, will update here if there is anything found

#1123199

I have done below modification in your website:
1) Modify the PHP codes as below:

add_shortcode( 'my_file_name', 'my_file_name_func'); // Actually activate the shortcode
function my_file_name_func($atts) {
    global $post; // So we can get the post meta later on
    $res = ''; // Setting up a variable to hold the links so we can return it later
    $types = $atts['field'];
    if ($types) { // if the types_field argument was provided 
  
    	//define ShortCode attr prefix
    	$types = "wpcf-$types";
      
        $urls = get_post_meta($post->ID,$types); // let's get the (potentially multiple) values
      	$arr = array();
        foreach ($urls as $fileurl) { // Let's iterate for each of the multiple values
          
            $id = pippin_get_image_id($fileurl);
            $title = get_the_title($id);
            $arr[] = '<a href="' . $fileurl . '">' . $title . '</a>'; // Create whatever HTML and store it in the $content variable
        }
      	$res = implode(' | ', $arr);
    } 
  	return $res; // Return the content as the shortcode value
      
}

2) Edit the content template
hidden link
in block "certifications", add below shortcode:

{!{my_file_name field='certifications'}!}

Please test again, check if it is fixed, thanks

#1123897

Thank you. It's working fine if there is data in the "certifications" field. However, if the field is blank, it is returning the URL of the post on the front end. To see the problem look at: hidden link
for example.

I appreciate your help.
Let me know if you need credentials again.

#1124274

I have changed the PHP codes to:

function my_file_name_func($atts) {
    global $post; // So we can get the post meta later on
    $res = ''; // Setting up a variable to hold the links so we can return it later
    $types = $atts['field'];
    if ($types) { // if the types_field argument was provided 
  
    	//define ShortCode attr prefix
    	$types = "wpcf-$types";
      
        $urls = get_post_meta($post->ID,$types); // let's get the (potentially multiple) values
      	if(empty($urls[0])){
          return;
        }
      	$arr = array();
        foreach ($urls as $fileurl) { // Let's iterate for each of the multiple values
          
            $id = pippin_get_image_id($fileurl);
            $title = get_the_title($id);
            $arr[] = '<a href="' . $fileurl . '">' . $title . '</a>'; // Create whatever HTML and store it in the $content variable
        }
      	$res = implode(' | ', $arr);
    } 
  	return $res; // Return the content as the shortcode value
      
}

Please test again, check if it is fixed, thanks