PeterDK Posted February 5, 2014 Share Posted February 5, 2014 Hi, Suppose I have a page /pagename/ Could I link to an included image /pagename/imagename? I know, if it gets deleted, the link will not work anymore... Is it possible to get the 'imagename' within my 'pagename' template? Is there a best practice I want to follow (using id instead of imagename) for creating this? Thanks!! Link to comment Share on other sites More sharing options...
kongondo Posted February 5, 2014 Share Posted February 5, 2014 (edited) Writing this quickly, have to run...there could be errors... I don't know what you mean by an included image but here goes: If talking about RTE (e.g. TinyMCE), yes you can include images from other pages... If you are talking about images inserted in pages via image fields, yes, you can grab this and output them in other template files. Just remember: $page will give you access to everything about the current page. If you want to get this page's images, oh well, see link below...;-) $pages will give you access to everything about other pages.. So, if you want images inserted in other pages, you will use $pages to get to them. How you get to other pages using $pages vary - you can get there by path, ID, name, etc...choose the selector that suits you. In some cases, people prefer getting there by ID in case they need to be very sure (since ID does not change). In other cases, people prefer getting other pages by their title since it makes for better human-readable code. http://processwire.com/api/variables/pages/ Also to remember. Sometimes people grab images from a multiple image field and try to echo them out and nothing happens. Point is? multiple image fields will return an array so you would need to loop through them using foreach or similar. Otherwise, for a single image field, you can just echo the image out. Now this may be misleading, you can specifically grab only one image from a multiple image field. In that case, there is no array, so just echo out what you want. I am not sure about best practice. I can't recall anyone here grabbing an image by its id. Pages, yes, but images can be tricky remembering ids. It is more common to grab images (all), or by their index in the array, eq(0), etc. A good practice though is to check if there are images before trying to output them (i.e. using "if"). This saves outputting blank HTML tags, getting PHP errors and sometimes is a good cure for headaches - why isn't my image showing?!? OK, enough theory. For some code, have a read in the docs about images; no need for me to reinvent the wheel: http://processwire.com/api/fieldtypes/images/. It also include image properties e.g. $image->filename. One more thing, at times, something some people forget; as with other fields, grab the images on a page using the name of the image field! People have been known to copy paste code and wonder why $pages->get("selector")->images doesn't work. For the "images", you need to use the image field's name . See also the cheatsheet: http://cheatsheet.processwire.com/ For more complicated scenarios, Google this forum. Some wonderful examples come up Edited February 5, 2014 by kongondo 5 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now