Support » Themes and Templates » Page template listing attachements and posts

  • langton82

    (@langton82)


    In my template the code to list the page’s attachments or to list posts from a particular category both work fine, but not together. When running them together the attachments disappear. I’ve tried different ways to list attachments and posts, but to no avail.

    Here’s the code:

    <ul class="post_list">
    <?php query_posts('category_name=resource-article&showposts=5'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <li><span class="title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></span> <span class="date"><?php echo get_the_date(); ?></span></a></li>
    <?php endwhile; ?>
    </ul>
    
    <div class="pdf_list">
    <ul>
    <?php
    $args = array(
    'post_type' => 'attachment',
    'numberposts' => null,
    'post_status' => null,
    'post_parent' => $post->ID,
    'post_mime_type' => 'application/pdf'
    );
    $attachments = get_posts($args);
    if ($attachments) {
    foreach ($attachments as $attachment) {
    echo "<li>";
    the_attachment_link($attachment->ID, false);
    echo "</li>";
    }
    }
    ?>

    Any help or suggestions would be gratefully received. Thanks.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator Andrew Nevins

    (@anevins)

    Which theme are you using?

    langton82

    (@langton82)

    That was swift.

    Custom theme based on TwentyTen.

    esmi

    (@esmi)

    Forum Moderator

    Try replacing:

    <?php query_posts('category_name=resource-article&showposts=5'); ?>

    with:

    <?php query_posts('category_name=resource-article&posts_per_page=5&post_type=array( 'post', 'attachment' )'); ?>

    langton82

    (@langton82)

    Nope – doesn’t like that.

    Just to clarify: the attachements I want to list are attachments to the page not the listed posts. Does that make sense?

    In other words, I have a page I want list file resources (e.g. PDFs) and post resources (i.e. WordPress posts in a particular category). The attachments do not directly relate to the posts.

    esmi

    (@esmi)

    Forum Moderator

    In that case, you’ll need to create a secondary query & loop that display that page’s attachments. Try reviewing multiple Loops.

    langton82

    (@langton82)

    No great joy there – examples all relate to the calling of sets of posts. I can’t see how to alter the attachment list query away from $wp_query.

    My workaround is not ideal but solves the immediate issue: Change the ‘post_parent’ from $post to the specific page id needed. Means the template will only work with one page, but at least it works.

    Open to alternative suggestions.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Page template listing attachements and posts’ is closed to new replies.