Jump to content

Showing images by tag on another page


Jon E
 Share

Recommended Posts

Hi,

So I am creating a portfolio site and am using my homepage as a way of displaying images (so images added to that entry).

Is there a way I can create a tag page, so that I can add a page elsewhere on the site, and the user can enter into a field the tag of images they want to show there that are uploaded to the homepage entry.

So, I'm trying something like the below:

<?php 
include("./head.inc"); ?>

<div id="client_body">
	<h1>Tag view title</h1>
	<h2><?php echo $page->get("title"); ?></h2>
		
	<?php if($page->tag_name):?>
	<h2><?php echo $page->get("tag_name"); ?></h2>
	<?php endif; ?>
	
	<?php
	
	foreach($page->images as $image) {
		// retrieve first image with tag 'mytag'
$image = $page->images->getTag('mytag'); 
  echo "<img src='$image->url'>"; 
}
?>

	

<?php 
include("./foot.inc");  ?>

Thanks, does this make sense?

Jon

Link to comment
Share on other sites

Thanks, that works.

But say if I want the tag its pulling from to be dynamic, so I use the 'tag_name' field within this code to show whatever I propose in the admin panel, how may I go about doing that? Also for some reason I only get one image appearing at the moment...

Sorry thanks so much.

<?php 
include("./head.inc"); ?>

<div id="client_body">
	<h1>Client Area</h1>
	<h2><?php echo $page->get("title"); ?></h2>
		
	<?php if($page->tag_name):?>
	<h2><?php echo $page->get("tag_name"); ?></h2>
	<?php endif; ?>
	
	<?php
	
	$homepage = $pages->get("/"); //get the homepage
foreach($homepage->images as $image)
		// retrieve first image with tag 'mytag'
$images = $homepage->images->getTag('tag'); 
  echo "<img src='$image->url'>"; 

?>

	

<?php 
include("./foot.inc");  ?>
Link to comment
Share on other sites

  • 3 weeks later...

Have I got this right?

You have a portfolio section in PW. It consists of several Projects and each project is a Page.

Projects

- project a

- project b

- project c

On each Project field, you have the followign fields

Title

Image

Tag

Body

etc

You then want to pull 1 or more projects onto the homepage and display just the contents of these fields

Title

Image

Tag

Link to comment
Share on other sites

Almost. I have all of the images in the images field on the homepage. I then have a page called 'clients', which is blank. For each child of this page I want to enter the tag into a field, so on a client child page I enter 'portrait' and it shows all images from the homepage that are tagged with 'portrait'.

Does this make sense?

Thanks so much

0sS48yh.png

Link to comment
Share on other sites

Writing quickly but these should point you in the right direction...to implement in your code above

// retrieve all images with tag 'potrait'
$images = $page->images->findTag('potrait');  
you can then iterate $images.
 
If in a selector, then search using the subfield tags:
// Find pages containing an image with the single (1) tag: 'potrait'
// This will only match images with 1 single tag of 'potrait'.
$mypages = $pages->find("images.tags=potrait");

http://processwire.com/api/fieldtypes/images/

Btw, just wondering why your tags have to be manually entered in some field? Why not use the child pages themselves as tags? Then you can use pagefields to pull in those tags? E.g. a child paged titled 'potrait'

Edited by kongondo
Link to comment
Share on other sites

Almost. I have all of the images in the images field on the homepage. I then have a page called 'clients', which is blank. For each child of this page I want to enter the tag into a field, so on a client child page I enter 'portrait' and it shows all images from the homepage that are tagged with 'portrait'.

That's interesting. I do it the other way around as to me, the homeage consists of contents from deeper within the site. 

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...