使用下面的代碼:
<ul id="attachments">
<?php foreach ($attachments as $attachment) {
if ($attachment ->post_mime_type != 'image/jpeg') {
$filetipe = $attachment->post_mime_type;
$signs = array(".", ",", "-", "application", "/");
$filetype = str_replace($signs, "", $filetipe);
?>
<li class="<?php echo $filetype; ?>"><a href="<?php echo wp_get_attachment_url($attachment->ID); ?>" target="blank"><?php echo apply_filters('the_title', $attachment->post_title); ?> </a></li>
<?php } } ?>
</ul>
你CA獲得一個職位的所有附件,除了圖像。 現在您可以編輯您的sidebar.php並查詢附有這些pdf文件的帖子。這將是你的sidebar.php文件的完整代碼,其中5是你的帖子的ID
<?php
query_posts('p=5');
while (have_posts()) : the_post(); ?>
<?php $args = array(
'post_type' => 'attachment',
'numberposts' => null,
'posts_per_page' => 666,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) {?>
<ul id="attachments">
<?php foreach ($attachments as $attachment) {
if ($attachment ->post_mime_type != 'image/jpeg') {
$filetipe = $attachment->post_mime_type;
$signs = array(".", ",", "-", "application", "/");
$filetype = str_replace($signs, "", $filetipe);
?>
<li class="<?php echo $filetype; ?>"><a href="<?php echo wp_get_attachment_url($attachment->ID); ?>" target="blank"><?php echo apply_filters('the_title', $attachment->post_title); ?> </a></li>
<?php } } ?>
</ul>
<?php } ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
它給我這個錯誤:[[[警告:在第21行爲/home/srvestle/public_html/svin/wp-content/themes/twentyten/sidebar.php中的foreach()提供的無效參數 wp_reset_query(); ?>]]]。這裏是代碼中的第21行:[[[foreach($ attachments as $ attachment){]]]。 我不明白代碼100%,所以我努力調試它。任何人? – Zeth 2013-02-22 14:32:57
我修改了一下代碼。 (在wp重置查詢之前,必須有一個php開始標記) – 2013-03-05 08:27:27