我明天就迟到了,所以可以利用社区帮助 . 我在插件中有一个循环输出自定义帖子类型,我想获得与帖子相关联的附加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;
2 回答
对我来说get_attached_media()帮助我在循环上获取pdf文件 . 保存一些代码
https://developer.wordpress.org/reference/functions/get_attached_media/
在你的循环中你需要指定父id以获取pdfs.your正在传递
$custom
,你已经在循环之外初始化了尝试下面get_the_ID检索当前帖子的数字ID . 此标记必须在The Loop内