I have a custom post with up to 13 images. Beside some other information, I write the image names and titles into a txt-file, whereby each post information is stored on a separate line.

So far everything works fine execept when the first post has eg. 8 images and the second post has 5 images, than in the txt-file I have the first 5 images from the second post but also the 3 last images from the first post, even though they should be “empty” in the second line in the txt-file.

To get the image information from the wordpress database I have following code:

            foreach($images as $image)
            {
                $path = parse_url(wp_get_attachment_url( $image ), PHP_URL_PATH);
                $files_all['images'][] = $files['images'][] = ABSPATH . $path;
                $files['title'][] = get_the_title($image);
                $files['imagename'][] = basename(ABSPATH . $path);
            }

            if(!empty($files['imagename'][0]))
            {
                $pic1_filename = $files['imagename'][0];
                $pic1_title = $files['title'][0];
            }

            if(!empty($files['imagename'][1]))
            {
                $pic2_filename = $files['imagename'][1];
                $pic2_title = $files['title'][1];
            }

            if(!empty($files['imagename'][2]))
            {
                $pic3_filename = $files['imagename'][2];
                $pic3_title = $files['title'][2];
            }

            if(!empty($files['imagename'][3]))
            {
                $pic4_filename = $files['imagename'][3];
                $pic4_title = $files['title'][3];
            }

            if(!empty($files['imagename'][4]))
            {
                $pic5_filename = $files['imagename'][4];
                $pic5_title = $files['title'][4];
            }

            if(!empty($files['imagename'][5]))
            {
                $pic6_filename = $files['imagename'][5];
                $pic6_title = $files['title'][5];
            }

            if(!empty($files['imagename'][6]))
            {
                $pic7_filename = $files['imagename'][6];
                $pic7_title = $files['title'][6];
            }

            if(!empty($files['imagename'][7]))
            {
                $pic8_filename = $files['imagename'][7];
                $pic8_title = $files['title'][7];
            }

            if(!empty($files['imagename'][8]))
            {
                $pic9_filename = $files['imagename'][8];
                $pic9_title = $files['title'][8];
            }

            if(!empty($files['imagename'][9]))
            {
                $pic10_filename = $files['imagename'][9];
                $pic10_title = $files['title'][9];
            }

            if(!empty($files['imagename'][10]))
            {
                $pic11_filename = $files['imagename'][10];
                $pic11_title = $files['title'][10];
            }

            if(!empty($files['imagename'][11]))
            {
                $pic12_filename = $files['imagename'][11];
                $pic12_title = $files['title'][11];
            }

            if(!empty($files['imagename'][12]))
            {
                $pic13_filename = $files['imagename'][12];
                $pic13_title = $files['title'][12];
            }

The txt-File code looks as follows:

            $txt .= "IDX3.01#Wordpress_4.6.2#".$proptype."#".$prop_label."#".$prop_status."#".$property_id."###".$address."#".$zip."#".$propcity."##".$country."###".$avail_from."#".$title.'#'.$post_description."#".$sale_price."#".$rent_price."##SELL#CHF###".$rooms."##".$property_size."#".$property_land."###".$property_year."#######".$garage."###".$distance_public_transport."#".$distance_shop."#".$distance_kindergarten."#".$distance_school1."#".$distance_school2."#".$pic1_filename."#".$pic2_filename."#".$pic3_filename."#".$pic4_filename."#".$pic5_filename."#".$pic1_title."#".$pic2_title."#".$pic3_title."#".$pic4_title."#".$pic5_title."######".$videos_name."#".$videos_title."##".$docname."#".$doctitle."##".$object_link."#agncyidxxxccc#Mischler Immobilien AG###Moosmattstrasse 23#6005#Luzern#CH#0413620101###info@mischler-immobilien.ch##".$agent_name."#0413620101#####".$pic6_filename."#".$pic7_filename."#".$pic8_filename."#".$pic9_filename."#".$pic6_title."#".$pic7_title."#".$pic8_title."#".$pic9_title."################################################".$pic10_filename."#".$pic11_filename."#".$pic12_filename."#".$pic13_filename."#".$pic10_title."#".$pic11_title."#".$pic12_title."#".$pic13_title."##################################".PHP_EOL;

The # are just separators within the file.

What did I miss here?

Read more here: Writing WordPress Data into a txt file


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: