Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Boris Kamp
16,660 PointsTransforming html with jQuery
I was wondering if it is worth the while to transform this code:
<div id="archive-grid-view" class="row row-mobile">
<div class="list box text-shadow">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="col-xs-6 col-sm-4 col-md-3 list-item box archive-view-container grid">
<a href="<?php the_permalink(); ?>">
<div class="box-content">
<img class="box-img" src="<?php the_field('postGridImage'); ?>" alt="<?php the_title();?>" title="<?php the_title();?>">
<h5 class="title"><?php the_field('kitName'); ?></h5>
<div class="archive-kit-info grid">
<?php if ( has_term( 'none', 'countries' ) ) {} else { ?>
<img class="country-flag" src="<?php bloginfo('stylesheet_directory'); ?>/images/country_flags/<?php $terms = get_the_terms( $post->ID , 'countries' ); foreach ( $terms as $term ) { echo $term->name; } ?> Flag.png" alt="><?php $term = get_field('country_of_origin');?><?php echo $term->name; ?> Flag">
<?php } ?>
<div class="label-feature scale CB-<?php $terms = get_the_terms( $post->ID , 'scales' ); foreach ( $terms as $term ) { echo $term->slug; } ?>">
<?php $terms = get_the_terms( $post->ID , 'scales' ); foreach ( $terms as $term ) { echo '<span>' . $term->name . '</span>'; } ?>
</div>
<div class="label-feature manufacturer CB-<?php $terms = get_the_terms( $post->ID , 'manufacturers' ); foreach ( $terms as $term ) { echo $term->slug; } ?>">
<?php $terms = get_the_terms( $post->ID , 'manufacturers' ); foreach ( $terms as $term ) { echo '<span>' . $term->name . '</span>'; } ?>
</div>
<div class="label-feature country CB-<?php $terms = get_the_terms( $post->ID , 'countries' ); foreach ( $terms as $term ) { echo $term->slug; } ?>">
<?php $terms = get_the_terms( $post->ID , 'countries' ); foreach ( $terms as $term ) { echo '<span>' . $term->name . '</span>'; } ?>
</div>
<?php if ( has_term( '', 'product_categories' ) ) { ?>
<div class="label-feature product-categories <?php $terms = get_the_terms( $post->ID , 'product_categories' ); foreach ( $terms as $term ) { echo 'CB-' . $term->slug . ' '; } ?>">
<?php $terms = get_the_terms( $post->ID , 'product_categories' ); foreach ( $terms as $term ) { echo '<span>' . $term->name . '</span>'; } ?>
</div>
<?php } else {}?>
<?php if ( has_term( '', 'vehicle_categories' ) ) { ?>
<div class="label-feature vehicle-categories <?php $terms = get_the_terms( $post->ID , 'vehicle_categories' ); foreach ( $terms as $term ) { echo 'CB-' . $term->slug . ' '; } ?>">
<?php $terms = get_the_terms( $post->ID , 'vehicle_categories' ); foreach ( $terms as $term ) { echo '<span>' . $term->name . '</span>'; } ?>
</div>
<?php } else {}?>
<div class="label-feature date">
<?php echo the_time('m/d/Y h');?>
</div>
</div>
</div>
</a>
</div>
to this code when a user clicks a button (to change the layout from grid style to list style)
<div id="archive-list-view" class="row row-mobile">
<table>
<thead>
<tr>
<th>Name</th>
<th>Scale</th>
<th>Manufacturer</th>
<th>Country</th>
<th>Product Category</th>
<th>Vehicle Category</th>
<th>Date</th>
</tr>
</thead>
<tbody class="list box text-shadow">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<tr class="list-item box archive-view-container list">
<td class="title">
<?php the_field('kitName'); ?>
</td>
<td class="label-feature scale CB-<?php $terms = get_the_terms( $post->ID , 'scales' ); foreach ( $terms as $term ) { echo $term->slug; } ?>">
<?php $terms = get_the_terms( $post->ID , 'scales' ); foreach ( $terms as $term ) { echo '<span>' . $term->name . '</span>'; } ?>
</td>
<td class="label-feature manufacturer CB-<?php $terms = get_the_terms( $post->ID , 'manufacturers' ); foreach ( $terms as $term ) { echo $term->slug; } ?>">
<?php $terms = get_the_terms( $post->ID , 'manufacturers' ); foreach ( $terms as $term ) { echo '<span>' . $term->name . '</span>'; } ?>
</td>
<td class="label-feature country CB-<?php $terms = get_the_terms( $post->ID , 'countries' ); foreach ( $terms as $term ) { echo $term->slug; } ?>">
<?php $terms = get_the_terms( $post->ID , 'countries' ); foreach ( $terms as $term ) { echo '<span>' . $term->name . '</span>'; } ?>
</td>
<td class="label-feature product-categories <?php $terms = get_the_terms( $post->ID , 'product_categories' ); foreach ( $terms as $term ) { echo 'CB-' . $term->slug . ' '; } ?>">
<?php $terms = get_the_terms( $post->ID , 'product_categories' ); foreach ( $terms as $term ) { echo '<span>' . $term->name . '</span>'; } ?>
</td>
<td class="label-feature vehicle-categories <?php $terms = get_the_terms( $post->ID , 'vehicle_categories' ); foreach ( $terms as $term ) { echo 'CB-' . $term->slug . ' '; } ?>">
<?php $terms = get_the_terms( $post->ID , 'vehicle_categories' ); foreach ( $terms as $term ) { echo '<span>' . $term->name . '</span>'; } ?>
</td>
<td class="label-feature date">
<?php echo the_time('m/d/Y h');?>
</td>
</tr>
<?php endwhile; else: ?>
<div class="page-header">
<h1>Oh no!</h1>
</div>
<p>No content is appearing for this page!</p>
<?php endif; ?>
</tbody>
</table>
</div>
What do you guys think? hiding, adding with JS or something else?
Thanks!
1 Answer
Andre Romano
8,809 PointsNot sure if I understand what you mean, but you could remove the initial grid view and load the list view from a separate html file with ajax.
Or if you want the user to be able to switch back and forth, have the two layouts in separate html files and load them with ajax on button click.
Boris Kamp
16,660 PointsBoris Kamp
16,660 Pointslet me give you some background info: I use Wordpress and a jQuery plugin called jPlist http://jplist.com/ to create filters in my archive pages. Now I would like the user to switch between grid and list view as well. The code I gave above is part of a lot more code to create the total output for jPlist. here's the total code:
I found out that if I use two containers (one for list and one for grid) inside the <!-- Data Loop --> and simply hide/show either one of the two based on the user's click action (grid or list button), this breaks the jPlist and thus disables all the filters and sorting and stuff etc, I found jQuery to be the solution (this keeps jPlist working) but am wondering if im missing something or if this is really the way It should be achieved. I would like to be sure before I start transforming everything with JS and find out later that this is not the ideal solution.
if you need more info, shoot! Thanks!