WordPress.org

Ready to get started?Download WordPress

Forums

WP Better Attachments
[resolved] If / Else possibilities, Attachment Count ? (4 posts)

  1. chomachomachoma
    Member
    Posted 1 year ago #

    Is it possible to use an if/else statement to check for the existence attached documents?

    I would like to check for attached "files" and if there there are files, show the list. But if there are none, I would like to show a simple message like "sorry, but no files exist for this item."

    Something like...
    (where attached_files_exist() checks if files are attached)

    if ( attached_files_exist($post->ID) ) {
      echo wpba_attachment_list();
    } else {
      echo 'sorry, but no files exist for this item.';
    }

    Or perhaps a count...
    (where attached_files_count() gets the number of attached files)

    if ( attached_files_count() > 0 ) {
      echo wpba_attachment_list();
    } else {
      echo 'sorry, but no files exist for this item.';
    }

    http://wordpress.org/extend/plugins/wp-better-attachments/

  2. chomachomachoma
    Member
    Posted 1 year ago #

    I have found a solution to this problem and have posted it below for others that may be looking to do the same thing:

    function get_docs() {
    
    	$query_pdf_args = array(
    	'post_parent'=>$post->ID,
    	                'post_type' => 'attachment',
    	                'post_mime_type' =>'application/pdf',
    	                'post_status' => 'inherit',
    	                'posts_per_page' => -1,
    	                );
    	$attachments = get_children($query_pdf_args);
    	$nbPdf = count($attachments);
    
    	if ($nbPdf==0) {
    		echo '<h3>Documents</h3>';
    		echo '<em>No documents have been posted for this product.</em>';
    
    	}
    	else {
    		echo '<h3>Documents</h3>';
    		echo wpba_attachment_list();
    	}
    
    }
    
    get_docs();
  3. dholloran
    Member
    Plugin Author

    Posted 1 year ago #

    Good idea, I have added wpba_attachments_exist() that will return true/false. It basically does what your doing except it will respect show thumbnail settings as well as any setting added going forward. This will be available in the next update, probably will be out on Sunday, and I will try to roll this into the shortcodes and functions before the update as well so you will not have to worry about it going forward.

  4. chomachomachoma
    Member
    Posted 1 year ago #

    Perfect! Works like a charm. Couldn't be happier! Thanks for the great work and speedy update.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic