Integrating isotope.js with Wordpress loop - php

I've been trying to get isotope.js working on a Wordpress site. I've been following this tutorial https://www.aliciaramirez.com/2014/03/integrating-isotope-with-wordpress/ and have been able to get it all functioning. For my design, I'm trying to add <div class="grid-sizer"></div> every four posts that are called. I've been referring to this question: Wrap every 4 posts in a custom wordpress loop with a div but cannot seem to figure out the proper placement for the count and i statements. Can anyone help me figure this out? Here's my loop right now:
<?php
$terms = get_terms( array(
'taxonomy' => 'solutions',
'parent' => 0
)
); // get all categories, but you can use any taxonomy
$count = count($terms); //How many are they?
if ( $count > 0 ){ //If there are more than 0 terms
foreach ( $terms as $term ) { //for each term:
echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
//create a list item with the current term slug for sorting, and name for label
}
}
?>
</ul>
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => asc,
'tax_query' => array(
array(
'taxonomy' => 'solutions',
'field' => 'term_id',
'terms' => get_queried_object()->term_id,
),
) );
$the_query = new WP_Query( $args ); //Check the WP_Query docs to see how you can limit which posts to display ?>
<?php if ( $the_query->have_posts() ) : ?>
<div id="isotope-list">
<?php
while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "solutions" ); //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
$i = 0;
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
if($i%4 == 0) {
echo "<div class='grid-sizer'> </div>";
}
?>
<div class="<?php echo $termsString; ?>item">
<p class="product-image"><a href="<?php the_permalink(); ?>" ><img src="<?php the_field("product_image") ?>" alt="<?php the_title(); ?>" class="solution-image" /></a></p>
<h4 class="product-name">
<?php the_title(); ?>
</h4>
</div>
<?php $i++; ?>
<!-- end item -->
<?php endwhile; ?>
</div>
<!-- end isotope-list -->
<?php endif; ?>

Here's the resulting code - thanks for misorude's help!
<?php
$terms = get_terms( array(
'taxonomy' => 'solutions',
'parent' => 0
)
); // get all categories, but you can use any taxonomy
$count = count($terms); //How many are they?
if ( $count > 0 ){ //If there are more than 0 terms
foreach ( $terms as $term ) { //for each term:
echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
//create a list item with the current term slug for sorting, and name for label
}
}
?>
</ul>
<?php
$i = 0;
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => asc,
'tax_query' => array(
array(
'taxonomy' => 'solutions',
'field' => 'term_id',
'terms' => get_queried_object()->term_id,
),
) );
$the_query = new WP_Query( $args ); //Check the WP_Query docs to see how you can limit which posts to display ?>
<?php if ( $the_query->have_posts() ) : ?>
<div id="isotope-list">
<?php
while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "solutions" ); //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
if($i%4 == 0) {
echo "<div class='grid-sizer'> </div>";
}
?>
<div class="<?php echo $termsString; ?>item">
<p class="product-image"><a href="<?php the_permalink(); ?>" ><img src="<?php the_field("product_image") ?>" alt="<?php the_title(); ?>" class="solution-image" /></a></p>
<h4 class="product-name">
<?php the_title(); ?>
</h4>
</div>
<?php $i++; ?>
<!-- end item -->
<?php endwhile; ?>
</div>
<!-- end isotope-list -->
<?php endif; ?>

Related

displaying the filtered custom taxonomy

Still trying to figure it out how could I filter the custom taxonomy and display it. the JS is for firing the URL generated is working fine. the problem is the results page items are not showing. I have this code so far:
<?php
$args = array(
'orderby' => 'slug',
'order' => 'ASC',
'parent' => 0,
'hide_empty' => false
);
$terms = get_terms([
'taxonomy' => 'pdf_cat',
'hide_empty' => false,
]);
foreach( $terms as $term ){
echo '<option class="ctg" value="'. get_term_link($term->term_id) .' ">' . $term->name . '</option>';
}
//NEXT CODE IS GET THE CATEGORY ID AFTER CLICKED TO DISPLAY
<?php
$terms = get_terms([
'taxonomy' => 'pdf_cat',
'hide_empty' => false,
]); // get the category from query
$terms = $term[0];
$term_name = get_term_name($term->name); // get the category name
$term_id = get_term_ID($term->term_id); // get the catehory ID
?>
<?php
$paged = get_query_var('paged', 1);
$args = array( // asigned the functions inside the array
'paged' => $paged,
'post_type' => 'pdf',
'taxonomy' => $term_id,
);
$query = new WP_Query($args); // get the functions inside the assigned name
global $query_string; //Post the specify numbers per page
query_posts( $query_string . "&posts_per_page=12&paged=".$paged );
while ( have_posts() ) : the_post()
?>
and here is the js to fire the dropdown menu link when clicked:
<script type="text/javascript">
$("#form").change(function(){
var url = $(this).val();
location.assign(url);
});
</script>
//NEXT CODE IS TO THE DISPLAY
<div class="pdf-box">
<?php
$file = get_field('file');
if( $file ): ?>
<a href="<?php echo $file['url']; ?>" target="_blank"><?php echo $file['file']; ?>
<div class="service-list-col1">
<i class="fa-file-o"></i>
</div>
<div class="service-list-col2">
<p><?php the_time('Y.m.d'); ?></p>
<h3><?php the_title(); ?></h3>
</div>
</a>
<?php endif; ?>
</div>

ACF row to show thumbnails of recent posts

I'm trying to create a ACF flexible content row to display the most recent post thumbnails for a given category. However it keeps throwing a critical error and I'm not sure why.
<?php
$section_id = get_sub_field('section_id')
$categories = get_sub_field('categories');
$tags = get_sub_field('tags');
$postnum = get_sub_field('number_of_posts');
if (!is_array($categories)) {
$categories = array($categories);
}
$tags = get_field('my_tags_field');
if (!is_array($tags)) {
$tags = array($tags);
}
$args = array(
'post_type' => 'post',
'numberposts' => $postnum,
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'terms' => $categories
),
array(
'taxonomy' => 'post_tag',
'terms' => $tags
)
)
);
$query = new WP_Query($args);
?>
<style>
</style>
<section class="post_row_with_thumbnails" id="<?php echo $section_id; ?>">
<div class="container-fluid">
<div class="row">
<?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>
<div class="col">
<a href="<?php the_permalink(); ?>">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ); ?>" class="project_pics">
<h5 class="posttitle"><?php the_title(); ?></h5>
<h6 class="postdate"><?php the_date(); ?></h6>
</a>
</div>
<?php endwhile; endif; wp_reset_postdata(); ?>
</div>
</div>
</section>
I have tried substituting WP_Query() with get_posts() but it gives me the same critical error.
In line 2 the ; at the end of the line, in this part:
$section_id = get_sub_field('section_id') ; THE DOT AND COMMA IS MISSING
This is the reason for the critical error.

How do I list sub pages to the current page in Wordpress, genesis theme

I want to list all closes children to the current page, not grand children or siblings, but when I try this, I get all pages.
function list_my_child_pages( $post ) {
$list_p = array(
'sort_column' => 'menu_order, post_title',
'child_of' => $post->ID,
'sort_order' => 'ASC'
);
$children = get_pages($list_p);
$result = "<ul>";
foreach ( $children as $child )
{
$child_id = $child->ID;
$url = get_permalink( $child_id );
$thumb = get_the_post_thumbnail($child_id, array(240, 240));
$title= $child->post_title;
$link = "<a href='$url'><div class='child_page_thumb'>$thumb</div><div class='child_page_title'>$title</div></a>";
$result .= "<li>$link</li>";
}
$result .= "</ul>";
echo $result;
}
To me it looks like it should not list siblings with this query, but it does. And how do I remove the grandchildren?
I usually do that directly in the templates, here is my code :
<?php
// Showing the list of child pages
// Check if current page has children pages
$children = get_pages('child_of='.$post->ID.'&parent='.$post->ID);
// if current page got children pages :
if (sizeof($children) > 0):
$args = array(
'post_status' => ' publish',
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'date',
);
$parent = new WP_Query( $args );
// Showing a list in the template
if ( $parent->have_posts() ) : ?>
<aside class="item-list">
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<a class="item-from-list" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php if (has_post_thumbnail()) {the_post_thumbnail('thumbnail', array('class' => 'alignleft'));} ?>
<h2><?php the_title(); ?></h2>
<?php atm_content(20, true); ?>
</a>
<?php endwhile; ?>
</aside>
<?php endif; wp_reset_query(); ?>
<?php endif;// end if sizeof($children) ?>
And if you don't want to get a list, you can store IDs for exemple in an array as following :
if ( $parent->have_posts() ) :
$array = new Array();
while ( $parent->have_posts() ) : $parent->the_post();
array_push($array, get_the_ID());
endwhile;
endif;

How to get last post thumbnail for each category as category thumbnail

I was looking around google and here but nothing found about this.
<div class="thumbBlock">
<?php foreach (get_categories() as $cat) : ?>
<div class="thumbInside">
<a href="<?php echo get_category_link($cat->term_id); ?>" title="<?php echo $cat->name; ?>" />
<img src="POST_THUMBNAIL HERE FOR EACH CATEGORY" alt="<?php echo $cat->name; ?>" />
</a>
<p><?php echo $cat->cat_name; ?></p>
</div>
<?php endforeach; ?>
</div>
as u see I want to list categories in a custom template but in the place post thumbnail I want to grab last post thumb for each category is it possible?
I think you'll find the_terms and get_the_terms to be better functions
$terms = get_terms( 'my_taxonomy' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul>';
foreach ( $terms as $term ) {
echo '<li>' . $term->name . '</li>';
/* use the term id to find matching posts in this taxonomy */
$args = array (
'showposts' => 1,
'orderby' => 'date',
'order' => 'DESC',
'tax_query' => array(
array(
'taxonomy' => 'my_taxonomy',
'field' => 'term_id',
'terms' => $term->term_id,
),
),
);
$new_query = new WP_Query( $args );
if ( $new_query->have_posts() ) {
while ( $new_query->have_posts() ) {
$new_query->the_post();
$post_id = get_the_ID();
echo '<li>';
echo get_the_post_thumbnail( $post_id, 'thumbnail' );
echo '</li>';
} // end while
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
}
echo '</ul>';
}
This hasn't been tested, but should get ya pretty close.

How to list subcategories of a category in categories page template (wordpress)

I have a categories page template, listing all categories with featured images. But I want to show only subcategories of a parent category. I don't know where to modify the template. Here is the code.
get_header(); ?>
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<h1 class="border-radius-5"><?php the_title(); ?></h1>
<div id="page" class="post-content">
<?php the_content(); ?>
<?php
$terms = get_terms("category", $args);
$count = count($terms);
$categories = array();
if ($count > 0) {
echo '<ul class="listing-cat">';
foreach ($terms as $term) {
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'show_count' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $term->slug
)
)
);
$video_from_categorie = new WP_Query( $args );
if( $video_from_categorie->have_posts() ){
$video_from_categorie->the_post();
}else{}
$term->slug;
$term->name;
?>
<li class="border-radius-5 box-shadow">
<?php echo get_post_image();?>
<span><?php echo $term->name; ?></span>
<span class="nb_cat border-radius-5"><?php echo $term->count; ?> <?php if ( $term->count > 1 ){
_e( 'videos', get_theme_name() );
}else{
_e( 'video', get_theme_name() );
} ?></span>
</li>
<?php
}
echo '</ul>';
echo '<div class="clear"></div>';
}
?>
</div><!-- #page -->
<?php endwhile; ?>
<?php endif; ?>
Pass the ID of the desired parent term/category to the child_of parameter of get_terms():
$terms = get_terms( 'category', array( 'child_of' => TERM_ID_GOES_HERE ) );

Categories