• I have a category and it has sub-category under these category i have post as attachments.i want to retive only files from this category,for this i use the following code but it was not working?

    <ul>
        <?php
        global $wpdb;
        $max_posts = 5;
        $sql = "SELECT posts.ID, attach.ID attachID, attach.post_title, MIN(attach.post_date)
        FROM $wpdb->posts posts, $wpdb->posts attach
        WHERE posts.ID = attach.post_parent
        AND attach.post_type='attachment'
        AND attach.post_status = 'inherit'
        AND posts.post_type = 'post'
        AND posts.post_status = 'publish'
        AND posts.post_date <= NOW()
        GROUP BY posts.ID
        ORDER BY posts.post_date DESC
        LIMIT $max_posts";
    
        $postIDs = $wpdb->get_results($sql);
        foreach ($postIDs as $postID) {
        the_attachment_link($postID->attachID,false);
    
        }
        ?>
        </ul>
Viewing 15 replies - 1 through 15 (of 17 total)
  • How is it not working? The code seems to get the images but bunches them together because you are an an unordered list (ul), but do not have li tags around each entry.

    If that is what is wrong, just add the li tags like this:

    foreach ($postIDs as $postID) {
       echo '<li>';
       the_attachment_link($postID->attachID,false);
       echo '</li>';
    }

    You may need to add some classes to the tags to style them the way you want.

    Thread Starter Shashikanta

    (@sashikanta)

    Hi,

    I’m using WordPress WP Publications Archive plugins, its saving the Docs in its category created by me. And all post type is ‘publication’, I have a CSS Divs and there I want to display files in each category. I’ve tried with the code but Publication under those category is not listing.

    I have a Category > PROGRESS and Under this 9 sub category and 13 is the ID so below is my Code …

    https://pastebin.com/4cjHqQYF

    Thread Starter Shashikanta

    (@sashikanta)

    This one is also not working

    <?php
    
    // The Query
    $the_query = new WP_Query( 'cat=13' );
    
    // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
    	echo '<li>';
    	the_title();
    	echo '</li>';
    endwhile;
    
    // Reset Post Data
    wp_reset_postdata();
    
    ?>

    all post type is ‘publication’,

    If this is true, you need to specify the post_type in your queries:

    $sql = "SELECT posts.ID, attach.ID attachID, attach.post_title, MIN(attach.post_date)
        FROM $wpdb->posts posts, $wpdb->posts attach
        WHERE posts.ID = attach.post_parent
        AND attach.post_type='attachment'
        AND attach.post_status = 'inherit'
        AND posts.post_type = 'publication'
        AND posts.post_status = 'publish'
        AND posts.post_date <= NOW()
        GROUP BY posts.ID
        ORDER BY posts.post_date DESC
        LIMIT $max_posts";

    And:

    $the_query = new WP_Query( 'cat=13&post_type=publication' );
    Thread Starter Shashikanta

    (@sashikanta)

    Wow Thanks!!! vtxyzzy Its coming.

    Plz do lwt me know how to link this directly to attachment ?

    Currently below this code is working and showing title

    <?php
    
    // The Query
    $the_query = new WP_Query( 'cat=13&post_type=publication' );
    
    // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
    	echo '<li>';
    	the_title();
    	echo '</li>';
    endwhile;
    
    // Reset Post Data
    wp_reset_postdata();
    
    ?>

    Here is the Codex article on getting attachments:

    https://codex.www.ads-software.com/Template_Tags/get_posts#Show_attachments_for_the_current_post

    Your Loop should look something like this:

    while ( $the_query->have_posts() ) : $the_query->the_post();
       $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
       $attachments = get_posts($args);
       if ($attachments) {
          foreach ( $attachments as $attachment ) {
             echo apply_filters( 'the_title' , $attachment->post_title );
             the_attachment_link( $attachment->ID , false );
          }
       }
    endwhile;

    You may need to add in the li tags around the call to the_attachment_link().

    Thread Starter Shashikanta

    (@sashikanta)

    I’ve tried but not working, Just see my above code, its not attachment it just publication. So I need publication link to the title.

    So here attachment for the post is not required.

    also Permalink is not linking to the post.

    <?php
    
    // The Query
    $the_query = new WP_Query( 'cat=13&post_type=publication&numberposts=5' );
    
    // The Loop
    	$attachments = get_posts( $args );
    	 while ( $the_query->have_posts() ) : $the_query->the_post();
    
    		echo '<li>';
    		echo '<a href="<?php the_permalink(); ?>"<?php the_title(); ?></a>';
    		echo '</li>';
     	 endwhile;
    
    // Reset Post Data
    wp_reset_postdata();
    
    ?>

    Look at the code in index.php for the ‘default’ theme for WordPress. There you will see how to display different parts of posts.

    Thread Starter Shashikanta

    (@sashikanta)

    I’ve tried but not happening

    Are you saying about <?php get_template_part( ‘content’, get_post_format() ); ?>

    I see you switched queries on me. Disregard the part about the li tags – it was for the first query.

    What exactly did you try?

    Thread Starter Shashikanta

    (@sashikanta)

    Sorry, its just a discussion.

    What I exactly trying is to link the post

    the echo ‘“<?php the_title(); ?>‘; is not linking.

    From index.php in the ‘default’ theme:

    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

    Thread Starter Shashikanta

    (@sashikanta)

    What you said is ryt. I’ve tried with the same just see the screenshot its rendering the css but not the permalink.

    [IMG]https://i40.tinypic.com/34rsolk.jpg[/IMG]

    Sorry don’t want to bother you ??

    An image is usually of very limited use in debugging code/CSS problems.

    Please post a few lines of the code you are actually using and a link to the page where the output can be seen.

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘How to get lists of files under a category or slug of WordPress’ is closed to new replies.