phpwordpress

Count Total Images attach to post and print result in title - wordpress


I want to show total number of images in my post title. for ex: "my post is This car for sale ( 4 images)" if this is the title i want to print 4 in my title before images. i try this code yet but it show 0. i dont know why. so can you please help me in this. Please note: im new to php and wordpress.

$count = count( $attachments );
$specific = array();
$i = 1;

foreach ( $attachments as $attachment ) {
    $specific[$attachment->ID] = $i;
    ++$i;
}

Solution

  • Please try below solution

    for this, you need to pass your post id

    <?php
    $getAttachments = get_children( array( 'post_parent' => $post->ID ) );
    $count = count( $getAttachments );
    echo $count;
    ?>