TransWikia.com

display post pdf attachments in side bar with link text

WordPress Development Asked by TDouglassNewby on December 17, 2021

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(
            array(
                'post_type'         => 'attachment',
                'numberposts'       => null,
                'posts_per_page'      =>$number,
                'post_status'         => 'any',
                'author'            =>$authorID,
                'post_parent'       =>$post->ID
        
            ),
            $instance
    );
        
    if ( ! $attachments->have_posts()) {
        return;
        } ?>
    
    <?php echo $args['before_widget']; ?>
    <?php
    if ( $title ) {
        echo $args['before_title'] . $title . $args['after_title'];
    }
    ?>
    
    <ul id="attachments">
        <?php foreach ( $attachments->posts as $attachment ) { 
        setup_postdata($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 } } ?>
    </ul>

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP