Support

Account

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

Solved

How to show PDF's by title?

    • Alcadis

    • November 12, 2013 at 3:00 am

    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.

    • jarvis

    • November 12, 2013 at 3:27 am

    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

    • Elliot

    • November 12, 2013 at 4:46 pm

    Hi @stefanluk

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

    Thanks
    E

    • jarvis

    • November 12, 2013 at 7:20 pm

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

    Hope that helps!?

    • Elliot

    • November 13, 2013 at 1:38 pm

    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

    • Alcadis

    • November 13, 2013 at 6:50 pm

    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

    • Elliot

    • November 14, 2013 at 3:49 pm

    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

    • Alcadis

    • November 18, 2013 at 10:31 pm

    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)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.