我想做一个自定义的Wordpress模板/页面,我从几个子类别的帖子中列出附加的pdf。我找到了几个片段,但我不能让它们工作。我的PHP技能有些有限。
这得到了我所有附加的pdf,但我想要4个不同的列表,每个类别一个。
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_mime_type' => 'application/pdf',
'post_parent' => null,
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $post) {
setup_postdata($post);
?>
<ul class="">
<li class=""><h2>Sub-Category-Name</h2>
<ul>
<li>
<?php
the_attachment_link($post->ID, false);
echo '</li>';
}
}
?>
我已经在一定程度上解决了这个问题。现在我在页面上运行了四次,每次循环使用不同的类别名称。
<ul>
<?php query_posts('category_name=your-category-name &posts_per_page=-1'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php
$args = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'application/pdf',
'post_status' => null,
'numberposts' => 1,
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
echo "<li><a href='";
echo the_permalink();
echo "' title='";
echo the_title();
echo "'>";
echo the_attachment_link($attachment->ID);
echo "</a></li>";
}
}
?>
<?php endwhile;?>
</ul>
<?php wp_reset_query(); ?>