Wordpress获得pdf附加链接发布

问题描述 投票:1回答:3

我明天就迟到了,所以可以利用社区帮助。我在插件中有一个循环输出自定义帖子类型,我想获得与帖子相关联的附加pdf文件。我已经设法获得帖子和大部分pdf附件工作,除了它只是拉入第一个pdf文件并在所有链接上显示它。我需要它来为每个帖子提取pdf的链接。我快到了,但我似乎无法得到它。

代码是:

global $post;

    $custom  = get_post_custom($post->ID);


    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 10;

    $args = array( 'post_type' => 'trends', 'orderby' => 'title', 'order' => 'asc', 'posts_per_page' => $paged );

    $success = new WP_Query( $args );

    if( $success->have_posts() ) :

    $output  = '';
    $output .= '<table class="custom-table-trend">';
    $output .= '<tr><th>File Name</th><th>Date added</th><th>Download</th></tr>';


        while( $success->have_posts() ) : $success->the_post();

        $query_pdf_args = array(
            'post_type' => 'attachment',
            'post_mime_type' =>'application/pdf',
            'post_status' => 'inherit',
            'numberposts' => 1,
            'posts_per_page' => -1,
            'post_parent'   => $custom
            );

            $query_pdf = new WP_Query( $query_pdf_args );

        foreach ( $query_pdf->posts as $file) {

                    $string = '<td><a href='. $file->guid .'>Download</a></td>';
            }


                $output .= '<tr>';
                $output .= '<td>'. get_the_title() .'</td>';
                $output .= '<td>' . get_the_date() . '</td>';
                $output .= sprintf( $string );
                $output .= '<tr>';



        endwhile;

        $output .= '</tr></table>';

    endif;

        return  $output;
php wordpress pdf while-loop custom-post-type
3个回答
1
投票

在你的循环中你需要指定父id来获取pdfs.your正在传递$custom,你已经在循环之外初始化了尝试下面

$query_pdf_args = array(
    'post_type' => 'attachment',
    'post_mime_type' =>'application/pdf',
    'post_status' => 'inherit',
    'numberposts' => 1,
    'posts_per_page' => -1,
    'post_parent'   => get_the_ID()
);

get_the_ID检索当前帖子的数字ID。此标记必须在The Loop


2
投票

对我来说,get_attached_media()帮助我在循环上获取pdf文件。保存一些代码

https://developer.wordpress.org/reference/functions/get_attached_media/


0
投票

我一直在经历同样的问题,我的自定义帖子包含pdf或下载链接,该链接未作为链接插入,只是粘贴在编辑器上。我在PHP中使用substring来提取我需要的内容,如果帖子中包含其他mime,它也可以扩展。我的代码是:

while($query->have_posts()) 
                 {
                    $query->the_post();
                    echo "<li>";
                    echo "<a href='";


                    $id=get_the_ID();
                        $queryx = get_post(get_the_ID()); 
                        $content = apply_filters('the_content', $queryx->post_content);

                            $sublen=strpos($content,strpos($content,'.pdf">'));
                        if($sublen!=0)
                        {
                            $strt=strpos($content,'<a href="')+9;
                            $end=strlen($content)-$strt-$sublen+5;
                        echo substr($content,$strt,$sublen+$strt+6+9);
                       }
                       else
                         echo wp_strip_all_tags( get_the_content());
                        echo "'>";
                        echo the_title()."</a></li>";
                    }
© www.soinside.com 2019 - 2024. All rights reserved.