【问题标题】:Wordpress get pdf attached link to postWordpress 获取 pdf 附加链接以发布
【发布时间】:2014-01-20 07:25:50
【问题描述】:

我明天会在这方面工作到很晚,所以可以利用社区来提供帮助。我在一个输出自定义帖子类型的插件中有一个循环,我想获取与帖子关联的附加 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


    【解决方案1】:

    在您的循环中,您需要指定父 ID 以获取 pdf。您在其中传递 $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/

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,我的自定义帖子包含一个 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>";
                          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-09
        • 2018-07-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多