最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

loop - display post pdf attachments in side bar with link text

programmeradmin9浏览0评论

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>
发布评论

评论列表(0)

  1. 暂无评论