WordPress.org

Ready to get started?Download WordPress

Forums

Page template listing attachements and posts (7 posts)

  1. langton82
    Member
    Posted 1 year ago #

    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.

  2. Andrew Nevins
    Barrel Rider, Spam Zapper & Volunteer Moderator
    Posted 1 year ago #

    Which theme are you using?

  3. langton82
    Member
    Posted 1 year ago #

    That was swift.

    Custom theme based on TwentyTen.

  4. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    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' )'); ?>

  5. langton82
    Member
    Posted 1 year ago #

    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.

  6. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

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

  7. langton82
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic