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.
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.
Glad that worked out for you 😀