Jump to content

Select Content From Pages Which Are Linked To Category


Liam88
 Share

Recommended Posts

Hi All,

New user over at Processwire and have been rebuilding my site based on this CMS.

I have been able to find so many answers through Google but I'm a little stuck on this one.

I have my services page -> services categories -> category children.

An example of those would be - domain -> services -> ppc -> management

I also have a set of tags which have different names - services-tag -> grow-your-traffic

Under these tags I would have multiple links to pages such as ppc, seo, social media and so on.

A second example would be - services-tag -> convert-your-traffic

Under here i would have multiple links to pages such as CRO

Now the set of tags are not visible on-site as they are only created to give overview content to the main services categories.

Using the categories and the tags I am looking to produce a layout such as (i have also attached an image as an example:

Tag_1 headline

Pull all services categories linking to Tag_1

Tag_1 snippet

Tag_2 headline

Pull all services categories linking to Tag_2

Tag_2 snippet

So far I have this snippet which is pulling in the tag content but unable to get the posts to show under each of the tags. 

If i change the if and statement to "tags" instead of "tag" then all posts show under all tags. Where as i want it to show only the posts which are linked to that tag.

<?php namespace ProcessWire;
	
	$tags = $pages->get("/categories-services/")->children(); // Gets the tags
	$posts = $pages->get("/services/")->children(); // Gets the services categories
	$link = $tags->ref_6; // Gets the tags and services categories link - under here you have pages_id (services cat id) and data (tags id)
	
	// Tag header and summary
	
	foreach($tags as $tag) {
	
		// This breaks down the tags into sections
		
		echo '<section id="services">
			<div class="container">
				<div class="row">
					<h2 class="heading-1"><span>'. $tag->headline.'</span></h2>
					<p class="mb-5">'. $tag->summary.'</p>
				</div>
			<div class="row justify-content-around services">';
			
		// Main services categories that link to the above tags
			
			if ($posts->id === $link->pages_id && $tag->id === $link->data){
			
				foreach($posts as $service){
				
					// This pulls in the services categories under the tag header. 
					
					echo '<div class="card flex-card" id="">
							<div class="card-img">
								<a href="/'. $service->name.'" title="'. $service->name .'">
								<img class="card-img-top" src="../assets/files/'. $service->id.'/'. $service->img_1.'" alt="'. $service->img_1.'" title="'. $service->img_1.'"></a>
							</div>
							<div class="card-body">
								<h3 class="card-title">'. $service->headline.'</h3>
								<p class="card-text">'. $service->summary .'</p>
								<div class="card-action">
									<a href="" title="'. $service->name .'" role="link" class="link">View service<span></span></a>
								</div>
							</div>
						</div>
					';
				}
			}
			
		// Grey snippet text
		
		echo '</div>
			</div>
				</section>
					<div class="snip-2 light-grey">
						<div class="container">
							<div class="row text-center">'. $tag->get('grey') .'</div>
						</div>
					</div>';
	}
?>

I appreciate this is a long post but i'm trying to be clear as I appreciate everyone's time.

Any insight into where I am going wrong is greatly appreciated.

Liam

processwire-question.JPG

pw-example.JPG

ref_6.JPG

Link to comment
Share on other sites

I might miss it but how do you add tags and categories to a page?

If you are using a page field (PageReference) you could just find() them.

foreach( $pages->find("tagFieldName=$tag") as $pageWithMyTag ) {
	// whatever you want
}

 

Link to comment
Share on other sites

13 hours ago, wbmnfktr said:

I might miss it but how do you add tags and categories to a page?

If you are using a page field (PageReference) you could just find() them.


foreach( $pages->find("tagFieldName=$tag") as $pageWithMyTag ) {
	// whatever you want
}

 

Thanks for the reply.

Within the page template I have a ref_6 which selects the tag for that page.

Then the aim is to have tag content which can then be pulled into sections and place the associated posts beneath. 

I did try find yesterday but I'll give that route another go today.

Thanks

ref example.JPG

Link to comment
Share on other sites

13 hours ago, wbmnfktr said:

I might miss it but how do you add tags and categories to a page?

If you are using a page field (PageReference) you could just find() them.


foreach( $pages->find("tagFieldName=$tag") as $pageWithMyTag ) {
	// whatever you want
}

 

Just to say huge thanks and your version worked. Really enjoying learning this CMS!

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

×
×
  • Create New...