Jump to content

add class to post


pppws
 Share

Recommended Posts

hey guys,

i'm currently working on my first processwire page and really enjoy the way it works.

the basic set up is done and everything works.

now i want to add some more features:

i have a spreadsheet which i want to import as pages. besides two fields (name and date) all of the fields should be additional classes for the posts/pages in my loop.

so the spreadsheet looks like this:

name: Aibo ERS 110 (Sony)

year: 1999

fictional: 

real: yes

interactive: yes

cogntive: yes

and my loop looks like this:


<div class="grid">
<?php $items = $pages->find("template=einzelansicht, sort=title");
foreach($items as $item) {
$firstimage = $item->einzelbild->first()->size(200,200)->url;
if ()
echo "
<a href='{$item->url}'>
<div class='item'>
<div class='overlay'></div>
<img src='{$firstimage}' />
<h2 class='headline'>{$item->title}</h2>
</div>
</a>
";
} ?> 
</div>

so basically i need this (line 8 in code above):

<div class="item REAL INTERACTIVE COGNITIVE">

any help is appreciated!

thanks a lot

philipp

Link to comment
Share on other sites

In what type of field are you saving the table fields you want to use as classes?

My guess is your example already considers that the spreadsheet data is already imported, so you can use IF statements to check each fields value. For example in case they are text fields:

$content = "<a href='{$item->url} class='";
if($page->real == "yes"){
  $content .= $page-> $page->classValue;  
} 

if($page->interactive == "yes"){
   //etc, etc
}

$content .= "'>";

// add more stuff

echo $content;
Link to comment
Share on other sites

So if I understand you correctly you have added the fields that exist in your spreadsheet to your template, if not your first task will be to create some inputs for these fields. Once you have done that and you have added all of your data you will loop over your pages like before and test the value in the field. I used an array but if you wanted to concatenate a string you could do that as well. This is just one quick option :).

<div class="grid">

<?php 

	$items = $pages->find("template=einzelansicht, sort=title");

	foreach($items as $item):
		
		$firstimage = $item->einzelbild->first()->size(200,200)->url;

		//Test your fields for the values that you are wanting
		if($item->year == 'yes') 		$addClasses[] = 'year';
		if($item->fictional == 'yes')	 	$addClasses[] = 'fictional';
		if($item->interactive == 'yes')		$addClasses[] = 'interactive';
		if($item->cogntive == 'yes')		$addClasses[] = 'cogntive';

		//create a string out of your array of options
		$extraClasses = implode(' ',$addClasses);
		
		//foreach loop is still running
?>

	<a href="<?=$item->url;?>">
		<div class="item <?=$extraClasses;?>">
			<div class="overlay'></div>
			<img src="<?=$firstimage;?>" />
			<h2 class="headline"><?=$item->title;?></h2>
		</div>
	</a>
	
<?php 
	//unset classes for the next item
	unset($extraClasses);
        unset($addClasses);

	endforeach;
	//foreach over
?> 

</div>
  • Like 1
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...