• Resolved Patrick

    (@zaanmedia)


    Hallo guys,

    anyone know how i’m able to count the attached images in the file_list and echo this number based on the post ID.

    Searched everywere but doesn’t seem to be able to find an answer/solution to my question. Can’t believe i’m the only one trying to archive this.

    Thanks!

    • This topic was modified 5 years, 9 months ago by Patrick.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Not quite following ultimate goal, sorry. Possibly explaining your intent will help me work things out mentally.

    First part says you want to get a total count of files saved. Say you have 3 saved for a post.

    Second, you want to conditionally echo that count, dependent on something in regards to the post ID?

    Thread Starter Patrick

    (@zaanmedia)

    what i try at the moment is to show the amount of images/attachments per post in an admin column

    i use the file list field from cmd2 in my post type.

    post 1 has 10 images
    post 2 has 15 images
    etc. etc.

    • This reply was modified 5 years, 9 months ago by Patrick.
    • This reply was modified 5 years, 9 months ago by Patrick.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Ah. So, take the following for example.

    I borrowed the code over at https://github.com/CMB2/CMB2/wiki/Field-Types#file_list as is, no edits.

    I attached some images to a sample post, and then ran this snippet in the single.php file just for ease of example.

    var_dump( get_post_meta( get_the_ID(), 'wiki_test_file_list', true ) );
    

    This made the following output:

    /app/public/wp-content/themes/twentyseventeen/single.php:23:
    array (size=3)
      25 => string 'http://www.example.com/wp-content/uploads/2018/08/1303483397485060.png' (length=63)
      26 => string 'http://www.example.com/wp-content/uploads/2018/08/1305369424349688.jpg' (length=63)
      27 => string 'http://www.example.com/wp-content/uploads/2018/08/1311363149881839.jpg' (length=63)
    

    Based on this, you can get a count of the images by simply assigning the get_post_meta() call to a variable and using the following example code:

    echo count( $images );
    

    For the example post, this would echo out “3” for my 3 images above. Should get you going on your needs.

    Thread Starter Patrick

    (@zaanmedia)

    This is simply amazing i think i have it working thanks to you!

    So what i did is this:

    $images = get_post_meta( get_the_ID(), 'wiki_test_file_list', true );

    and then

    echo count( $images );

    Thanks again and much appreciated for all the help!

    • This reply was modified 5 years, 9 months ago by Patrick.
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Glad that worked out for you 😀

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘(file_list) count items(images)’ is closed to new replies.