Get the parent term with child term - php

How can I get the parent and child term of a custom post type. I have code below that partly works, the problem is it does not generate child and parent term in same div but different div as loop. Due to lack of knowledge on PHP, I am not really sure how to fix it.
What I want if I have parent and child as;
- USA
- New York
The out come should be;
USA, New York
but with below code it generates as
,USA
,New York
Code;
<div class="address">
<?php
$terms = get_terms(array(
'taxonomy' => 'people-country',
'hide_empty' => false,
));
?>
<?php foreach ($terms as $term) : ?>
<?php
$re = explode('-', $term->name);
$args = array (
'post_type' => 'people', //
'posts_per_page' => 2,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'people-country',
'field' => 'id',
'terms' => $term->term_id,
),
),
);
$query = new WP_Query( $args );
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
$title = get_the_title();
// $info = get_post_meta(get_the_ID(), '_post_info', true);
// $link = get_term_meta($term->term_id, 'link', true);
?>
<div class="text">
<span class="number"><?php echo ",".$re[0] ?>
</span>
</div>
<?php
}
}
endforeach; ?>
</div>

I just solved it by changing it with below code;
<?php
$cats = get_the_terms(get_the_ID(), 'people-country');
$names = array();
if ($cats) foreach ($cats as $cat) {
if ($cat->parent) {
array_push($names, $cat->name);
}
}
if ($cats) foreach ($cats as $cat) {
if (!$cat->parent) {
array_push($names, $cat->name);
}
}
?>
<?php echo implode(', ', $names) ?>

Related

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;

order fields inside CFS() loop wordpress

Here is my code:
$args = array(
'posts_per_page' => - 1,
'post_type' => array('post-type-name'),
'tax_query' => $taxquery,
'orderby' => 'meta_value_num',
'meta_key' => 'date_of_issue',
'order' => 'DESC',
);
global $post;
$post_list = get_posts($args);
if ($post_list) {
foreach ($post_list as $post_item) { ?>
<?php
$fields = CFS()->get('anouncements', $post_item->ID);
if ( ! empty ( $fields ) ){
foreach ($fields as $field_eti) { ?>
<div class="item"><?php echo $field_eti['date_of_issue']; ?></div>
<?php }
}
?>
<?php
}
}
I'm using Custom Field suite plugin. I want to order those fields by this date which is one of the custom fields of that loop ($field_eti['date_of_issue'];). How you see in my $args I added that meta_key and ordered it by custom field value which is date. But ordering isn't right.
Try this: I have updated my answer.
foreach ($post_list as $post_item) {
setup_postdata($post_item); // Set postdata
?>
<?php
$fields = CFS()->get('anouncements');
$temp=array();
if ( ! empty ( $fields ) ){
foreach ($fields as $field_eti) {
$temp[] = strtotime($field_eti['date_of_issue']);
?>
<div class="item"><?php echo $field_eti['date_of_issue']; ?></div>
<?php }
$new_field_eti[] = arsort($temp);
print_r($new_field_eti);
}
?>
<?php
}

Get woocommerce subcategory products

I am trying to get the woocommerce subcategories with products to show under the main categories.
<ul class="wsubcategs">
<?php
$wsubargs = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $category->term_id,
'taxonomy' => 'product_cat'
);
$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc):
?>
<li><?php echo $wsc->name;?>
</li>
<?php
endforeach;
?>
</ul>
So far this has gotten me the corrent subcategories under the correct main categories, but no products are being shown under them.
Any advice would be appreciated.
O.
There are many ways to do this in Wordpress. You could do a custom query using the WP_Query object to get all of the products in that category, which would be the most flexible option, but there is an easier way.
Woocommerce provides shortcodes specifically for showing products in a specific category. The output would use the templates that are already built in to Woocommerce.
<?php echo do_shortcode('[product_category category="appliances"]');?>
This will give you the products under a specific category.
In your code, you might do something like this:
<ul class="wsubcategs">
<?php
$wsubargs = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $category->term_id,
'taxonomy' => 'product_cat'
);
$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc):
?>
<li>
<?php echo $wsc->name;?>
<div class="products">
<?php echo do_shortcode('[product_category category="'.$wsc->slug.'"]');?>
</div>
</li>
<?php endforeach;?>
</ul>
I noticed in your question that you're outputting a list. It seems likely to me that you wouldn't want to actually output the products (detailed template) below each of the categories, but you might rather want to show the number of products or product titles in a sublist.
Here's how you would show the number of products:
count;?>
You would use this anywhere in the foreach loop you have above.
Here's how you would show a sublist of titles of products in the list element for each category:
<?php $subcategory_products = new WP_Query( array( 'post_type' => 'product', 'product_cat' => $wsc->slug ) );
if($subcategory_products->have_posts()):?>
<ul class="subcat-products">
<?php while ( $subcategory_products->have_posts() ) : $subcategory_products->the_post(); ?>
<li>
<a href="<?php echo get_permalink( $subcategory_products->post->ID ) ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile;?>
</ul>
<?php endif; wp_reset_query(); // Remember to reset ?>
And here's how that would look in your code above:
<ul class="wsubcategs">
<?php
$wsubargs = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => 0,
'parent' => $category->term_id,
'taxonomy' => 'product_cat'
);
$wsubcats = get_categories($wsubargs);
foreach ($wsubcats as $wsc):
?>
<li>
<?php echo $wsc->name;?>
<?php $subcategory_products = new WP_Query( array( 'post_type' => 'product', 'product_cat' => $wsc->slug ) );
if($subcategory_products->have_posts()):?>
<ul class="subcat-products">
<?php while ( $subcategory_products->have_posts() ) : $subcategory_products->the_post(); ?>
<li>
<a href="<?php echo get_permalink( $subcategory_products->post->ID ) ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile;?>
</ul>
<?php endif; wp_reset_query(); // Remember to reset ?>
</li>
<?php endforeach;?>
</ul>
**Show Category and Sub Category in search Box**
----------
<?php
//template name:house
?>
<style>
.abc {
margin-left: 10px;
}
</style>
<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$show_count = 0; // 1 for yes, 0 for no
$pad_counts = 0; // 1 for yes, 0 for no
$hierarchical = 1; // 1 for yes, 0 for no
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
?>
<select>
<?php
$all_categories = get_categories( $args );
foreach ($all_categories as $cat)
{
if($cat->category_parent == 0)
{
$category_id = $cat->term_id;
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
$abc=$cat->name;
if($abc=="") {
?>
<option value="<?php echo $cat->slug;?>"><?php echo $abc; ?></option>
<?php }
$args2 = array(
'taxonomy' => $taxonomy,
'child_of' => 0,
'parent' => $category_id,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$sub_cats = get_categories( $args2 );
if($sub_cats)
{
foreach($sub_cats as $sub_category)
{
?>
<?php
if($sub_cats->$sub_category == 0)
{
$suv= $sub_category->cat_name;?>
<option class="abc" value="<?php echo $sub_category->slug; ?>"><span class="abc"><?php echo $suv; ?></span></option>
<?php } } } } } ?>
</select>

Taxonomy archive + query attachments = duplicate results [wordpress]

I'm trying to get the attachments of a specific term (in its archive page).
But the results are showing the resulting images 5 times instead of one.
I have multiple loops in this page - one to show related posts, another to show related products (custom post), and this one to show related images. Custom posts and posts are working nicely, but I can't show the attachments in the right way. :S
<?php $queried_object = get_queried_object();
$term_id = $queried_object->term_id;
$args = array(
'post_status' => 'inherit',
'numberposts' => 0,
'post__not_in' => array_merge($do_not_duplicate,get_option( 'sticky_posts' )),
'post_type' => 'attachment',
);
$args['tax_query'] = array(
array(
'taxonomy' => 't-arte',
'terms' => $term_id,
'field' => 'id',
),
); ?>
<?php $t = $data['t-arte'];
$array = explode(" ", $t);
$array = array_unique($array);?>
<?php $media_query = array_unique($array); ?>
<?php $media_query = get_posts($args);
if( !empty( $media_query ) ) :
foreach ($media_query as $media_query) :
global $post; $post = $media_query;
setup_postdata($media_query);
?>
<div id="archivespage-media-item">
<?php $attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<div id="imagem">';
the_attachment_link( $attachment->ID, true );
echo '</div>';
}
}?>
</div>
<?php endforeach;else :?>
<p>Ainda não temos nenhuma imagem relacionada :(</p>
</div>
<?php endif; ?>
<?php wp_reset_query();?>'
I've done these and its working!
The result will show all attachments in a specific term inside term's archive page.
<?php $queried_object = get_queried_object();
$term_id = $queried_object->term_id;
global $wp_query;
$original_query['tax_query'] = array(
array(
'taxonomy' => 't-arte',
'terms' => $term_id,
'field' => 'id',
),);
$original_query = (array) $wp_query;
$attach_query = array(
'post_type'=> array( 'attachment' ),
'post_status' => array( null ));
$args = array_merge($original_query['query_vars'], $attach_query);
$media_query = new WP_Query( $args )?>
<?php if($media_query->have_posts()) :
while ($media_query->have_posts() ) : $media_query->the_post();
if( $post->ID == $do_not_duplicate ) continue; ?>
<div id="archivespage-media-item">
<div id="imagem">
<?php echo wp_get_attachment_link($attachment->ID, 'bigger-thumb');?>
</div>
</div>
<?php endwhile; else: ?>
//do stuff
</div>
https://wordpress.stackexchange.com/questions/29635/how-to-create-an-attachments-archive-with-working-pagination

Get all terms in a custom Wordpress Query?

I know how to get all of the Wordpress terms, but I need a "filtered" down version of the results. Is it possible to get all of the terms that are in the result of a Wordpress query? I have this query here:
<?php
$args=array(
'post_type' => 'gw_activity',
'post_status' => 'publish',
'orderby' => 'date',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'activity_category',
'value' => 'mindful_life',
'compare' => '='
)
),
'posts_per_page' => 10
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
$all_terms = array();
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $terms = wp_get_post_terms( $my_query->post->ID, array( 'gw_activity_tag' ) ); ?>
<?php
foreach ( $terms as $term ) {
$all_terms[] = $term->name;
}
?>
<?php endwhile; }
wp_reset_query();
?>
<!-- End Custom Query -->
<?php
$unique_terms = array_unique( $all_terms );
$result = array_unique($unique_terms);
foreach ($result as $value) {
echo $value . '<br />';
}
?>
But I can't figure how to run the query & put a "Where" clause in it, like you can with MySQL. Any help / suggestion or even point me in the right direction would be greatly appreciated. I'm stuck
Check this function: wp_get_post_terms()
Assuming your post supports two taxonomies called tax_a and tax_b, you can try something like this, exactly where you wrote your comment:
<?php $terms = wp_get_post_terms( $query->post->ID, array( 'tax_a', 'tax_b' ) ); ?>
<?php foreach ( $terms as $term ) : ?>
<p><?php echo $term->taxonomy; ?>: <?php echo $term->name; ?></p>
<?php endforeach; ?>
This will print all the terms for each post retrieved by the query.
EDIT
If what you want is all the terms in all the posts retrieved by the query, you can store the values in an array and then use a function like array_unique(), like this:
$all_terms = array();
foreach ( $terms as $term ) {
$all_terms[] = $term->name;
}
// ... and outside the WHILE loop
$result = array_unique( $all_terms );
foreach ( $result as $term ) {
echo $term . '<br/>;
}

Categories