Web Hosting
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to add Files to a page automatically

Discussion in 'Troubleshooting & How-To's' started by rokealy, Feb 1, 2017.

  1. rokealy

    New Member

    Joined:
    Feb 1, 2017
    Messages:
    1
    Likes Received:
    0
    I have had a function in the functions..pho that dynamically pulls documents that are attached to a page and then inserts them onto that page using the relative code. My issue is that it pulls in the file name of each document and not the TITLE, which is what I want to show. Below is the code I am using:

    The function in the functions.php file:
    ////////////////////////////////////////////////////////////////////////
    /// Grabs PDF attachments
    ////////////////////////////////////////////////////////////////////////
    function get_attachment_icons($echo = false){
    if ( $files = get_children(array( //do only if there are attachments of these qualifications
    'post_parent' => get_the_ID(),
    'post_type' => 'attachment',
    'numberposts' => -1,
    'post_mime_type' => 'application/pdf', //MIME Type condition
    ))
    ){
    $sAttachmentString = "<div class='mdownloads' ><h2>Downloads</h2>";
    $sAttachmentString .= "<ul>";
    foreach( $files as $file ){ //setup array for more than one file attachment
    $file_link = wp_get_attachment_url($file->ID); //get the url for linkage
    $file_name_array=explode("/",$file_link);
    $file_name=array_reverse($file_name_array); //creates an array out of the url and grabs the filename
    $file_title = wp_get_attachment_link($file->ID);
    $sAttachmentString .= "<li>$file_title</li>";
    }
    $sAttachmentString .= "</ul><br/><div class='clear'></div>";
    $sAttachmentString .= "</div>";
    }
    //Audio Files
    $mp3s = get_children(array( //do only if there are attachments of these qualifications
    'post_parent' => get_the_ID(),
    'post_type' => 'attachment',
    'numberposts' => -1,
    'post_mime_type' => 'audio', //MIME Type condition
    ) );

    if (!empty($mp3s)) :
    $sAttachmentString .= "<ul class='audiofiles'>";
    foreach($mp3s as $mp3) :
    $sAttachmentString .= "<li>";
    if(!empty($mp3->post_title)) : //checking to make sure the post title isn't empty
    $sAttachmentString .= "<h4 class='title'>".$mp3->post_title."</h4>";
    endif;
    if(!empty($mp3->post_content)) : //checking to make sure something exists in post_content (description)
    $sAttachmentString .= "<p class='description'>".$mp3->post_content."</p>";
    endif;
    $sAttachmentString .= "<object width='470' height='24' id='single".$mp3->ID."' name='single".$mp3->ID."'>";
    $sAttachmentString .= "<param name='movie' value='player.swf'>";
    $sAttachmentString .= "<param name='allowfullscreen' value='true'>";
    $sAttachmentString .= "<param name='allowscriptaccess' value='always'>";
    $sAttachmentString .= "<param name='wmode' value='transparent'>";
    $sAttachmentString .= "<param name='flashvars' value='file=".$mp3->guid."'>";
    $sAttachmentString .= "<embed ";
    $sAttachmentString .= "id='single".$mp3->ID."' ";
    $sAttachmentString .= "name='single".$mp3->ID."' ";
    $sAttachmentString .= "src='".get_bloginfo('template_directory')."/jw/player.swf' ";
    $sAttachmentString .= "width='470' ";
    $sAttachmentString .= "height='24' ";
    $sAttachmentString .= "bgcolor='#ffffff' ";
    $sAttachmentString .= "allowscriptaccess='always' ";
    $sAttachmentString .= "allowfullscreen='true' ";
    $sAttachmentString .= "flashvars='file=".$mp3->guid."' ";

    $sAttachmentString .= "/>";
    $sAttachmentString .= "</object>";
    $sAttachmentString .= "<a href='".$mp3->guid."'>Download</a>";
    $sAttachmentString .= "</li>";
    endforeach;
    $sAttachmentString .= "</ul>";
    endif;

    if($echo){
    echo $sAttachmentString;
    }
    return $sAttachmentString;
    }
    add_shortcode('attachment icons', 'get_attachment_icons');

    This is on the page.php:
    <?php if(!is_page('5')) get_attachment_icons($echo=true); ?>
     
    #1

Share This Page

Web Hosting