Skip to content

Instantly share code, notes, and snippets.

@SahilMepani
Last active February 24, 2018 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SahilMepani/3f46a4941389645ae74bfb4963004aa7 to your computer and use it in GitHub Desktop.
Save SahilMepani/3f46a4941389645ae74bfb4963004aa7 to your computer and use it in GitHub Desktop.
WPML: Get posts from each term/category in accordion style for different languages.
<?php
/*=============================================================================
= WPML Get posts from each terms in accordion style for =
=============================================================================*/
$args = array(
'taxonomy' => 'TAXONOMY', // defaults to 'category'
'parent' => 0, // top level only
);
$terms = get_terms( $args );
?>
<ul class="list-accordions">
<?php foreach ( $terms as $term ) : ?>
<li>
<?php
$query = new WP_Query(
array(
'post_type' => 'POST_TYPE',
'posts_per_page' => -1,
'order' => 'ASC',
'post__not_in' => array( $post->ID ), // do not show current post
'suppress_filters' => FALSE,
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'terms' => (int) apply_filters( 'wpml_object_id', $term->term_id, $term->taxonomy ),
),
)
)
);
var_dump($query);
?>
<h4 class="heading"><?php echo $term->name; ?></h4>
<div class="content">
<ul>
<?php $post_id = get_the_id(); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<li <?php if( $post_id == $post->ID ) { echo 'class="current-menu-item"'; } ?>>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
</div> <!-- .content -->
<?php wp_reset_postdata(); ?>
</li>
<?php endforeach; ?>
</ul> <!-- .list-accordions -->
<?php
/*
* More examples can be found at
* https://developer.wordpress.org/reference/functions/get_terms/
*/
get_terms( string|array $args = array() ); // return array of objects
Retrieve the terms in a given taxonomy or list of taxonomies.
// Returned Array
array(1) {
[0] = ;
object(WP_Term) (11) {
["term_id"] = ; //int
["name"] = ; //string
["slug"] = ; //string
["term_group"] = ; //int
["term_taxonomy_id"] = ; //int
["taxonomy"] = ; //string
["description"] = ; //string
["parent"] = ; //int
["count"] = ; // int
["filter"] = ; //string
["meta"] = ; array(0) { // presumably this would be some returned meta-data?
}
}
}
/* Args default values */
array (
'taxonomy' => 'category', //empty string(''), false, 0 don't work, and return empty array
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true, //can be 1, '1' too
'include' => 'all', //empty string(''), false, 0 don't work, and return empty array
'exclude' => 'all', //empty string(''), false, 0 don't work, and return empty array
'exclude_tree' => 'all', //empty string(''), false, 0 don't work, and return empty array
'number' => false, //can be 0, '0', '' too
'offset' => '',
'fields' => 'all',
'name' => '',
'slug' => '',
'hierarchical' => true, //can be 1, '1' too
'search' => '',
'name__like' => '',
'description__like' => '',
'pad_counts' => false, //can be 0, '0', '' too
'get' => '',
'child_of' => false, //can be 0, '0', '' too
'childless' => false,
'cache_domain' => 'core',
'update_term_meta_cache' => true, //can be 1, '1' too
'meta_query' => '',
'meta_key' => array(),
'meta_value' => '',
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment