I am working on properties project and need to get terms of specific taxonomy. I mean properties of cities base of their states.
Here is the code I have written for:
<div class="row">
<div class="col-md-2">
<?php
$taxonomy = 'property-state';
$terms = get_terms($taxonomy);
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li>
<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
<?php echo $term->name . ' ('. $term->count .')' ?>
</a>
</li>
<?php } ?>
</ul>
<?php
?>
</div>
<div class="col-md-2">
<?php
$taxonomy = 'property-city';
$terms = get_terms($taxonomy);
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li>
<a href="<?php echo get_term_link($term->slug, $taxonomy); ?>">
<?php echo $term->name . ' ('. $term->count .')'?>
</a>
</li>
<?php } ?>
</ul>
<?php
?>
</div>
</div> <!-- end of row -->
What I need is to list properties of cities base of state, how should I change these codes?
I found my answer myself, I wanted to list properties of specific state with specific status.
for example the properties of Kabul state with status of (for-rent) and the properties count on each city of that state.
<?php $terms = get_terms("property-city");
foreach($terms as $term)
{
$items = get_posts(array(
'numberposts' => -1,
'post_type' => 'property',
'post_status' => 'publish',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'property-city',
'field' => 'slug',
'terms' => $term->slug
),
array(
'taxonomy' => 'property-state',
'field' => 'name',
'terms' => 'هرات',
'operator' => 'NOT IN'
),
)
));
$count = count( $items );
?>
<ul>
<li>
<a href="<?php echo get_term_link($term);?>" <?php if ($count == 0) echo " style='display: none';"; ?>>
<?php echo $term->name. ' ('. $count .')';?>
</a>
</li>
</ul>
<?php
}
?>
Related
I have a problem, i have a custom type post (authors) and taxonomy (manager and team) called position
I need to get all authors of the team on a page and need the manager to be at the first one of them
the authors ordered by name but the manager name begin with "T"
what can I do with this situation.
my code is
<?php
/**
* The template for displaying auther custom type on custom taconomy position filterd By the page name and position name
* Template Name: taxonomies
*
*/
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
get_header(); ?>
<?php
// echo the $slug name of the page
$page_names = get_the_title(); ?>
<section id="articles" class="articles">
<div class="<?php echo $classcase ?>">
<?php
$loop = new WP_Query( array( 'post_type'=>'authors','posts_per_page' => '-1','orderby' => 'title','order'=>'ASC' ) );
if ( $loop->have_posts() ) :
while($loop->have_posts()): $loop->the_post( );
$terms = get_the_terms( $post->ID, 'position' );
foreach ( $terms as $term ) {
if($term->name == $page_names) { ?>
<div class="tax-container">
<a href="<?php the_permalink( );?>">
<div class="parent-before">
<img class="tax-img" src=" <?php the_post_thumbnail_url( ); ?> " data-tool-tip="<?php the_title( );?>" />
<div class="tax-div-p" data-tool-tip="<?php the_title( );?>">
<p class="tax-p" data-tool-tip="<?php the_title( );?>"></p>
</div>
</div>
</a>
</div>
<?php } } ?>
<?php endwhile; wp_reset_query(); endif; ?>
</div>
</section>
<div class='endauthors'></div>
For this task you need to use tax_query https://developer.wordpress.org/reference/classes/wp_tax_query/
First we take all managers sorted by title, then the entire team
Try this code
<?php
/**
* The template for displaying auther custom type on custom taconomy position filterd By the page name and position name
* Template Name: taxonomies
*
*/
defined( 'ABSPATH' ) || exit; // Exit if accessed directly
get_header(); ?>
<?php
// echo the $slug name of the page
$page_names = get_the_title(); ?>
<section id="articles" class="articles">
<div class="<?php echo $classcase ?>">
<?php
$args = array(
'post_type' => 'authors',
'posts_per_page' => '-1',
'orderby' => 'title',
'order'=>'ASC'
'tax_query' => array(
array(
'taxonomy' => 'position',
'field' => 'slug',
'terms' => array( 'manager' )
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while($loop->have_posts()): $loop->the_post( );
$terms = get_the_terms( $post->ID, 'position' );
foreach ( $terms as $term ) {
if($term->name == $page_names) { ?>
<div class="tax-container">
<a href="<?php the_permalink( );?>">
<div class="parent-before">
<img class="tax-img" src=" <?php the_post_thumbnail_url( ); ?> " data-tool-tip="<?php the_title( );?>" />
<div class="tax-div-p" data-tool-tip="<?php the_title( );?>">
<p class="tax-p" data-tool-tip="<?php the_title( );?>"></p>
</div>
</div>
</a>
</div>
<?php } } ?>
<?php endwhile; wp_reset_query(); endif; ?>
<?php
$args = array(
'post_type' => 'authors',
'posts_per_page' => '-1',
'orderby' => 'title',
'order'=>'ASC'
'tax_query' => array(
array(
'taxonomy' => 'position',
'field' => 'slug',
'terms' => array( 'team' )
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) :
while($loop->have_posts()): $loop->the_post( );
$terms = get_the_terms( $post->ID, 'position' );
foreach ( $terms as $term ) {
if($term->name == $page_names) { ?>
<div class="tax-container">
<a href="<?php the_permalink( );?>">
<div class="parent-before">
<img class="tax-img" src=" <?php the_post_thumbnail_url( ); ?> " data-tool-tip="<?php the_title( );?>" />
<div class="tax-div-p" data-tool-tip="<?php the_title( );?>">
<p class="tax-p" data-tool-tip="<?php the_title( );?>"></p>
</div>
</div>
</a>
</div>
<?php } } ?>
<?php endwhile; wp_reset_query(); endif; ?>
</div>
</section>
<div class='endauthors'></div>
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; ?>
I am working with this code in template page for WP.
<div id="content" role="main">
<?php
$query = array(
'post_type' => 'post',
'category_name' => 'jewellery-design',
'orderby' => 'date',
'order' => 'DESC'
);
$featured_home = new WP_Query( $query );
if( $featured_home->have_posts() ) {
?>
<div class="row">
<?php while ( $featured_home->have_posts() ) : $featured_home->the_post();?>
<span class="cat"><?php the_category(); ?>
<div class="col-3">
<a href="<?php the_permalink(); ?>">
<div class="featured-home-img" <?php
if ( $thumbnail_id = get_post_thumbnail_id() ) {
if ( $image_src = wp_get_attachment_image_src( $thumbnail_id, 'normal-bg' ) )
printf( ' style="background-image: url(%s);"', $image_src[0] );
}?>>
<div class="blog-info-content">
<h3><?php the_title(); ?></h3>
<div class="obsah"><?php the_content(); ?></div> </span>
<p class="postmetadata">
Posted in <?php the_category(', ') ?>
<strong>|</strong>
<?php edit_post_link('Edit','','<strong>|</strong>'); ?>
</span>
</div>
</div>
</a>
</div>
<?php
endwhile;
?>
</div>
<?php
}
wp_reset_postdata();
?>
</div>
And I am getting output like this:
But, i´d like to have output like this:
Can someone tell me how can I do it? Or may I use some plugin for Wordpress to make it easier for me?
Thank you.
I might think about sorting them into groups first, then re-looping over the sorted groups. I am a little rusty on Wordpress so I have set it up essentially, but all the functions/methods need to return values, not echo, so you will need to adjust that:
<?php
# Create base function
function getItemsByCategoryType($category)
{
$query = array(
'post_type' => 'post',
'category_name' => $category,
'orderby' => 'date',
'order' => 'DESC'
);
# Get query
$featured_home = new WP_Query($query);
# Just stop if empty
if(!$featured_home->have_posts())
return false;
# Set a default
$array = array();
while($featured_home->have_posts()){
$featured_home->the_post();
$thumbnail_id = get_post_thumbnail_id();
$image_src = ($thumbnail_id)? wp_get_attachment_image_src($thumbnail_id,'normal-bg')) : false;
# You are looking to store the category title as the key
# That will isolate each group
$array[the_category()][] = array(
'thumb_id' => $thumbnail_id,
'image' => $image_src,
'permlink' => the_permalink(),
'title' => the_title(),
'content' => the_content(),
'categories' => the_category(', '),
'edit_mode' => edit_post_link('Edit','','<strong>|</strong>')
);
}
wp_reset_postdata();
# Send back stored data as an array
return $array;
}
# To use, get the data from your function
$Items = getItemsByCategoryType('jewellery-design') ?>
<div id="content" role="main">
<?php if(!empty($Items)): ?>
<div class="row">
<?php foreach($Items as $categories => $rows): ?>
<span class="cat"><?php echo $categories ?>
<?php foreach($rows as $row): ?>
<div class="col-3">
<a href="<?php echo $row['permlink'] ?>">
<div class="featured-home-img"<?php if(!empty($row['image'][0])) echo ' style="background-image: url('.$row['image'][0].');"' ?>>
<div class="blog-info-content">
<h3><?php echo $row['title'] ?></h3>
<div class="obsah">
<?php echo $row['content'] ?>
</div>
<p class="postmetadata">Posted in <?php echo $row['categories'] ?><strong>|</strong><?php $row['edit_mode'] ?></p>
</div>
</div>
</a>
</div>
<?php endforeach ?>
</span>
<?php endforeach ?>
</div>
<?php endif ?>
</div>
NOTE: Just to reiterate, I am not up to speed on WP so you need to take the idea of what I am proposing and fixing it, specifically when it comes to returning values vs allowing the WP functions to echo content.
EDIT:
You want an array to come out of the function similar to:
Array (
[ Fibre Collection ] => Array (
[ 0 ] => Array (
[ title ] => FIBRE_BRACELET
[ content ] => etc...,
etc...
)
),
[ Tetragon Collection ] => Array (
[ 0 ] => Array (
[ title ] => Test2
[ content ] => etc...,
etc...
),
[ 1 ] => Array (
[ title ] => TETRAGÓN_BRACELET
[ content ] => etc...,
etc...
)
)
)
I found a solution to the output I needed. If this helps anyone :)
$cat_args=array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => 2
);
$categories=get_categories($cat_args);
foreach($categories as $category) {
$args=array(
'showposts' => -1,
'category__in' => array($category->term_id),
'caller_get_posts'=>1
);
$posts=get_posts($args);
if ($posts) {
echo '<div class="test"><p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
foreach($posts as $post) {
setup_postdata($post); ?>
<p><?php the_title(); ?></p>
<p><a href="<?php the_permalink() ?>" <?php the_content(); ?></a></p>
<?php
} // foreach($posts
echo '</div>';} // if ($posts
} // foreach($categories
?>
The 'parent' => 2 means ID of parent category from which you want to get your posts.
Have a nice day.
Trying to order some posts I'm displaying on a single custom post type page with random, but they aren't random at all. :/
<?php
// Grab the taxonomy term slug for the current post
$terms = get_the_terms( get_the_ID(), 'category-staff' );
if ( $terms && ! is_wp_error( $terms ) ) :
$draught_links = array();
foreach ( $terms as $term ) {
$draught_links[] = $term->slug;
}
$on_draught = join( ", ", $draught_links );
?>
<div class="container hidden-xs">
<div class="row">
<div class="col-sm-12">
<hr />
<h3 class="text-center">Other People At Our Great Resort</h3>
</div>
</div>
<div class="row">
<div class="col-sm-12 col-lg-10 col-lg-offset-1">
<div class="row staff-list">
<?php
// WP_Query arguments
$args2 = array (
'post_type' => 'staff',
'tax_query' => array(
array(
'taxonomy' => 'category-staff',
'field' => 'slug',
'terms' => $on_draught,
),
),
'nopaging' => false,
'posts_per_page' => '4',
'order' => 'DESC',
'orderby' => 'rand',
);
// The Query
$query2 = new WP_Query( $args2 );
// The Loop
if ( $query2->have_posts() ) {
while ( $query2->have_posts() ) {
$query2->the_post(); ?>
<div class="staff staff-other col-sm-3 text-center">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php echo get_the_post_thumbnail( $_post->ID, 'large', array( 'class' => 'img-responsive img-circle img-staff' ) ); ?>
<h4><?php the_title(); ?></h4>
<?php if (get_field('staff_job')) { ?>
<p><?php the_field('staff_job'); ?></p>
<?php } ?>
</a>
</div>
<?php }
} else { ?>
<?php }
// Restore original Post Data
wp_reset_postdata(); ?>
</div>
</div>
</div>
</div>
<?php endif; // terms if statement ?>
Turns out it was something to do with WPEngine. They disable rand() from the server and it needs to be enabled manually.
Another solution may be to add this code before running the new WP_Query($args) function.
remove_all_filters('posts_orderby');
https://developer.wordpress.org/reference/functions/remove_all_filters/
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 ) );