display post pdf attachments in side bar with link text


I have a widget that places links to pdf attachments in the sidebar. I want to use the pdf attachment text heading (that is used to display it in the parent post) as the title. I cannot get this to work. see below. $img_title is what I DON’T want, as this shows the title that is assigned through the media library, and $post_title is showing the name of the main page (on which I’m viewing the sidebar), which seems very wrong, and makes me wonder if there’s some larger issue here. Any help would be appreciated. Here is what is in the actual post to show you what title I want to show up in sidebar.

a href=”http://localhost/mytestsite/wp-content/uploads/2020/07/test-Copy-6.pdf”>I want this to show as title</a

    $attachments = new WP_Query(
                'post_type'         => 'attachment',
                'numberposts'       => null,
                'posts_per_page'      =>$number,
                'post_status'         => 'any',
                'author'            =>$authorID,
                'post_parent'       =>$post->ID
    if ( ! $attachments->have_posts()) {
        } ?>
    <?php echo $args['before_widget']; ?>
    if ( $title ) {
        echo $args['before_title'] . $title . $args['after_title'];
    <ul id="attachments">
        <?php foreach ( $attachments->posts as $attachment ) { 
        $post_title = get_the_title($post_parent->ID); // retrieves title of main page
        $img_title = apply_filters( 'the_title', $attachment->post_title ); //title from media librar       
    if ($attachment ->post_mime_type != 'image/jpeg') {
    $filetipe = $attachment->post_mime_type;
    $signs = array(".", ",", "-", "application", "/");
    $filetype = str_replace($signs, "", $filetipe);
    <li class="<?php echo $filetype; ?>"><a href="<?php echo wp_get_attachment_url($attachment->ID); 
    ?>" target="blank"><?php echo $img_title . $post_title; ?></a></li>
    <?php } } ?>
, , TDouglassNewby 1 year 2020-07-23T15:11:10-05:00 0 Answers 52 views 0

Leave an answer