Support

Account

Home Forums Add-ons Repeater Field How to show PDF's by title?

Solved

How to show PDF's by title?

  • I’m having some problems showing PDF files. I want to be able to upload multiple PDF files to a page and show them by title with a download link.

    I already found an older post, but the given code doesn’t seem to work
    http://support.advancedcustomfields.com/forums/topic/display-title-of-file-in-href-using-repeater/

    Here is my code`if(get_field('pdf_download_repeater'))
    {
    echo '<div class=”widget”><h3 class=”widgettitle”>Downloads</h3> <ul>';
    while(has_sub_field('pdf_download_repeater'))
    {
    $attachment_id = get_sub_field('pdf_download');
    $url = wp_get_attachment_url( $attachment_id );
    $title = get_the_title( $attachment_id );

    echo '<li><a target=”_blank” class=”pdf_download” href=”' . $url . '”>' . $title . '</a></li>';
    }

    echo '</ul></div>';
    }`

    This code outputs 2 links (I uploaded 2 pdf’s) with both the same title (the title of the first post on my website, called “hello world”). Both links open the current page in a new tab.

    Anyone knows what the problem is here?

    EDIT: This code shows a link on every other page using the page title as anchor text linking to the same page.

  • I use the repeater and add 2 parts:
    – File
    – Text Field

    Then I do this:

    
    			<?php if(get_field('downloads')): ?>
    			<ul class="downloads">
    			<?php while(has_sub_field('downloads')): ?>
    				<li><img src="<?php echo $image_path; ?>adobe_pdf_icon.png" width="31" height="33" /><a href="<?php the_sub_field('file'); ?>" target="_blank"><?php the_sub_field('file_name'); ?></a></li>
    			<?php endwhile; ?>
    			</ul>		
    			<?php endif; ?>
    

    Not sure if that helps you or not

  • Hi @stefanluk

    Thanks for the code. What is the return setting for the file field?

    Thanks
    E

  • Sorry I forgot to say that bit. I use the File URL in the settings

    Hope that helps!?

  • Hi @stefanluk

    Can you please do some debugging like so:

    <?php 
    
    echo '<pre>';
    	print_r( get_field('downloads') );
    echo '</pre>';
    die; ?>
    

    And paste the results in a comment? Remember to wrap the results in the code button!

    Thanks
    E

  • Sorry, thought I used the code button. I use the file object setting as the return field.

    if(get_field('pdf_download_repeater'))
    {
    	echo '<div class="widget"><h3 class="widgettitle">Downloads</h3><ul>';
    						
    while(has_sub_field('pdf_download_repeater'))
     {
     $attachment_id = get_sub_field('pdf_download_repeater');
     $url = wp_get_attachment_url( $attachment_id );
     $title = get_the_title( $attachment_id );
    							
     echo '<li><a target="_blank" class="download_pdf" href="' . $url . '">' . $title . '</a></li>';
     }
    					 
     echo '</ul></div>';
    }

    I’ll try the debugging right now

  • Hi @stefanluk

    If you have selected the return format of ‘file object’, the your code won’t work. Your code expects the value to be an ID.

    For example, your code $url = wp_get_attachment_url( $attachment_id ); requires $attachment_id to be an integer. However, if you have selected ‘file object’ as the return setting, this will NOT be an integer!

    Please read the docs and debug your code to see what data you have available on each line.

    Thanks
    E

  • Hey elliot,

    Thanks for your input.

    I changed the return format to ‘file ID’ and it works perfect.

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘How to show PDF's by title?’ is closed to new replies.