Thread:Project:Forum/Image acoording to category of the page/reply

One way would be to do all that beforehand, rather than afterwards when the page is loaded. Use four different templates. Each template displays the image and categorises the page in a specific category. For example, Template:A would display image A.png and categorise the page in category A, as follows:

[[Category:A]]

You then place the template in all pages to be categorised in category A, rather than categorising them explicitly.