【发布时间】: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