GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   simple way to count number of photos in wordpress? (https://gfy.com/showthread.php?t=1163792)

druid66 03-27-2015 06:29 AM

simple way to count number of photos in wordpress?
 
i've spend 3 hours looking for smth, plugin or other crap, ended with some hard to understand script which i cannot utilize.

all i want is to have this same feature as TGPX had: "xxx number of photos on our site"

is there any simple to use plugin that would count all photos from all posts excluding featured image and allow me to paste code so i could display it somehere close to header or anywhere i want?

HowlingWulf 03-27-2015 08:39 AM

Now sure how TGPX works with images, but if they're attached to the posts you can use this function in your theme:

Code:

function total_images() {
        global $wpdb;
        $images=  $wpdb->get_var("
                SELECT COUNT(ID) FROM $wpdb->posts
                WHERE (post_mime_type LIKE 'image/%')
                AND post_type = 'attachment'
                AND (post_status = 'inherit')
                AND post_parent > 0
        ");
        echo 'total attached images= '. $images;
}


druid66 03-27-2015 08:42 AM

so i'm copying this code and paste into functions.php in wordpress, now how can i display it on the site?

and thanks ;)

Barry-xlovecam 03-27-2015 08:43 AM

SSH/terminal

Code:

[CD to the $PWD(working directory to start recursion)]

$ find . -name "*.jpg" -or  -name "*.gif" -or -name "*.png" | wc -l >ct
$ cat ct
110
^image count


druid66 03-27-2015 08:45 AM

guys, ffs thank you very much but i'm familiar with html only, fraction of knowledge of php that's all i know, some css.

in other words i need more guidance how to use it and where.

Barry-xlovecam 03-27-2015 09:03 AM

Sorry - You said simple. Mine is one short [long :0D] line but I see what you mean.

You either need to install a script (PHP or ?) and execute the script to get the count or do it at the command line.

Maybe, someone could make a plug in ... but it would be a very limited market.

druid66 03-27-2015 09:49 AM

i understand a bit Howlingwulfs script a bit but need to know how to call it, how to display it?

your Barry i have no idea what to do with.

but thanks a lot for trying anyway.

Barry-xlovecam 03-27-2015 01:08 PM

It would just give you the count (printed to a file) when you run it and would not output 'print or echo' the count on your site.

PHP could open the counter file and echo that count.

What I wrote is the basis of a .sh (bash script) that could be set into a cron job -- the server would do it every 4 hours or something. This is the fast way to do things with the least server resources used.

If you were to add a PHP script to do this each time the wordpress page loads it is a waste of resources ... polling the same count over and over each page load slows the page load time ...

The count should be cached every so often 1 sec - 1 day this is done in a user crontab.

druid66 03-27-2015 01:20 PM

so i should make a .sh file with this content:

[CD to the $PWD(working directory to start recursion)]

$ find . -name "*.jpg" -or -name "*.gif" -or -name "*.png" | wc -l >ct
$ cat ct
110
^image count

and setup cron to run it one or more times daily and it will count all images?
but how to show this file where are those images located (normally it's wp-content/uploads and there are subfolders) ?

also how would look command to echo of results?

thanks a lot for helping, your way is indeed much better than any plugin or php which is running all the time.

420 03-27-2015 01:29 PM

I just use my fingers. For more complex counting I use an abacus like this:

http://38.media.tumblr.com/e731673af...fo1_r1_500.gif

Barry-xlovecam 03-27-2015 05:16 PM

Code:

#!/bin/bash
#imgct.sh
#FTP and CHMOD 755

find . -name "*.jpg" -or -name "*.gif" -or -name "*.png" | wc -l >ct.txt

exit

The bash script should start in the parent folder of the images -- it will recurse the sub directories from there.

You may have to path the PHP script to the ct.txt file.
crontab
there shold be access in the admin interface (cPanel?)
Quote:

*/10 * * * * /home/user/path/../../imgct.sh
#every 10 min
#30 18 * * *
#each day at 6:30 PM^
Crontab ? Quick Reference | Admin's Choice - Choice of Unix and Linux administrators

PHP Code:

<?php
//display count
//displayct.php
$imgct fopen("ct.txt""r") or die("Unable to open file!");
echo 
fread($imgctfilesize("ct.txt"));
fclose($imgct);
?>

You could make this PHP an include in the file you want it in. Path shown is relative to the .sh You will need to change the path from the template page or PHP file it is put on.

Freeware: MIT license as-is without warranty or guarantee.:1orglaugh

druid66 03-28-2015 02:30 AM

thanks a lot, just woke up, i'll play with this and let you know results :thumbsup

druid66 03-28-2015 03:31 AM

here's what i did:

i've made imgct.sh file with this content:

#!/bin/bash
#imgct.sh
#FTP and CHMOD 755

find . -name "*.jpg" -or -name "*.gif" -or -name "*.png" | wc -l >ct.txt

exit

this file is in my /uploads/ folder inside wp-content

now in same folder i've made blank ct.txt file with 755 permissions

and as 3rd i've made displayct.php file with content:

<?php
//display count
//displayct.php
$imgct = fopen("ct.txt", "r") or die("Unable to open file!");
echo fread($imgct, filesize("ct.txt"));
fclose($imgct);
?>

where is fopen("ct.txt","r") i've put full path to /wp-content/uploads/

and as 4th step i've setup cron.

when i've opened displayct.php in my browser it remains blank, cron is working every 2minutes (testing) and cron sends me an emails with this content:

#imgct.sh
#FTP and CHMOD 755

find . -name "*.jpg" -or -name "*.gif" -or -name "*.png" | wc -l >ct.txt

exit

now what i did wrong?

Kafka 03-28-2015 06:33 AM

Quote:

Originally Posted by HowlingWulf (Post 20430889)
Now sure how TGPX works with images, but if they're attached to the posts you can use this function in your theme:

Code:

function total_images() {
        global $wpdb;
        $images=  $wpdb->get_var("
                SELECT COUNT(ID) FROM $wpdb->posts
                WHERE (post_mime_type LIKE 'image/%')
                AND post_type = 'attachment'
                AND (post_status = 'inherit')
                AND post_parent > 0
        ");
        echo 'total attached images= '. $images;
}


In order to display total images just do this:
copy the function into the page where to display.
then "total_images();" this will invoke the function and echo the total images, if the query is correct ofcourse.

Dont use bash scripts for this.

Barry-xlovecam 03-28-2015 06:45 AM

1. Download the ct.txt and see if it has a number.
If it does, then the .sh and its cron is working
Check the ownership of the file it should be username:group (root?)

I tested it here on a site directory at the command line, the .sh(bash) command and the php <file> they worked.

$cat ct =>110
cat is open and read out a file in LINUX terminal. $tac is reverse the file read from the end Trivia :)

If there are contents in the file then it is a PHP issue.
PHP may need the full path ?
Or, there is a fopen, or fread issue -- maybe the chown (ownership of the file -- the PHP user www-data usually needs to be the file's group, i.e.; $chown username:www-data but PHP shouldn't need that permission to read just to write to a file.
Full path looks sort of like this:
/home/username/docroot/ ..../.../wp-content/uploads/

*** I just tried it in a browser and it worked =>712
That was the count from that directory but the path was relative all in the same directory. Put the image count readout *displayct.php* in the same directory as the ct.txt if that file has contents to read. Wordpess file location problem?

I sort of like the idea because it could wc -l (count lines in a file or a find (like shown | (pipe the output))) of a link file or a videos found count.

druid66 03-28-2015 08:53 AM

thank Kafka but i would love to make Barry's scripts to work, looks like low server resource and should do the job.

can't make it. i think i've fucked up smth at the start since there is no numbers generated in ct.txt so maybe chmods are setup badly or smth in .sh file

gonna work on it more, rly like this idea.

Barry-xlovecam 03-28-2015 09:15 AM

Did you make the bash .sh script in a widows editor? In Windows it need to be done in notepad and saved as 'all files' .sh -- DOS and UNIX line endings?

If you could SSH to the directory and run the command manually you could test for output ...

$ cd ./path/to/file
$ find . -name "*.jpg" -or -name "*.gif" -or -name "*.png" | wc -l >ct.txt

also
$ stat imgct.sh
make sure the permissions are 755 if the command works manually.

ct.txt and display.php should stat with 644 permissions.

druid66 03-28-2015 09:19 AM

made it in notepad with .sh tylde
already wrote a ticket to my admins asking what could be wrong, will post here once we will figure out.


All times are GMT -7. The time now is 09:12 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123