Hello i create a function to display on list all subcategory of master category of WooCommerce and not display the subcategory that are empty without any products.
I use this code but i continue to display on list also the subcategory empty. Where wrong?
<ul>
<?php foreach($term_children as $child) :
$term = get_term_by( 'id', $child, 'product_cat' );
?>
<li><?php echo $term->name; ?></li>
<?php // Skip empty terms
if( $child->count <= 0 ) {
continue;
} ?>
<?php endforeach; ?>
</ul>
Related
Topic kinda says it all. I need to only pull in the first two fields of an ACF repeater field.
Here is what I am trying to use but it is obviously not right:
<?php $args = [ 'posts_per_page' => 2, 'order' => 'desc']; ?>
<?php $ar = new WP_Query( $args ); ?>
<?php if( $ar->have_rows('prodImgs') ): while ( $ar->have_rows('prodImgs') ) : $ar->the_row(); ?>
<img src="<?php the_sub_field('prodImg'); ?>" alt="">
<?php endwhile; ?>
<?php endif; ?>
How am I supposed to do this?
<?php
// check if the repeater has data
if( have_rows('prodImgs') ) {
//counter
$i=0;
//loop through the rows
while( have_rows('prodImgs') ) {
the_row();
//check if 2 subfields have been shown
if ( $i > 1 ) { break; }
echo "<img src='" . get_sub_field('prodImg_sub') . "' alt='Lorem ipsum'>";
$i++;
}
}
?>
You are mixing apples and pears, WP_Query and ACF Repeater field. WP_Query returns the post data, whilst the ACF function have_rows( $repeater_field_name, $post_id ); checks whether there are any data in the custom field repeater that is on your page/post (or if you specify $post_id on the relevant post/page). More info at https://www.advancedcustomfields.com/resources/repeater/
<?php $rows = get_field('prodImgs'); ?>
<?php $i = 0; if ($rows):?>
<?php foreach($rows as $row): ?>
<img src="<?php echo $rows[$i][/* name of first field */]; ?>" alt="">
<?php echo $rows[$i][/* name of Second field */]; ?>
<?php $i++; endforeach; ?>
<?php endif; ?>
I found this code:
<?php
$args = array(
'orderby' => 'id',
'hide_empty'=> 0,
'child_of' => 2,
'depth' => 5,
);
$categories = get_categories($args);
foreach ($categories as $cat) {
echo '<div>';
echo '<h1>'.$cat->name.'<img src="'.$cat->term_icon.'" alt="" class="alignleft"/>'.'<br />'.'<span class="solutions">'.$cat->description.'</span>'.'</h1>';
//echo '<br />';
$args3= array("orderby"=>'name', "category" => $cat->cat_ID, 'depth' => 5); // Get Post from each Sub-Category
$posts_in_category = get_posts($args3);
foreach($posts_in_category as $current_post) {
echo '<span>';
?>
<li><h1><?=$current_post->post_title;?></li></h1>
<?php
echo '</span>';
}
echo '</div>';
}
?>
This lists all categories and posts from a category. But i want it from all categories. But when i fill in 'child_of' => 2 it lists everything, but it does not get formatted well. grandchilds have the same hierarchical status as the children.
what i want for example:
Parent
child 1
grandchild 1
post 1
grandchild 2
post 2
child 2
post 3
so: all cats should be able to handle posts, and if there are only posts in the grandchildren, only list those… thanks!
-edit- actually it should be the same as wp_list_categories, only i should be able to edit parents, children, grandchildren and post titles separately. (for example, i must be able to remove the href off the child-categories, but not the grandchild-categories..
Try This :
<!-- Category Archive Start -->
<ul class="catArchive">
<?php
$catQuery = $wpdb->get_results("SELECT * FROM $wpdb->terms AS wterms INNER JOIN $wpdb->term_taxonomy AS wtaxonomy ON ( wterms.term_id = wtaxonomy.term_id ) WHERE wtaxonomy.taxonomy = 'category' AND wtaxonomy.parent = 0 AND wtaxonomy.count > 0");
$catCounter = 0;
foreach ($catQuery as $category) {
$catCounter++;
$catStyle = '';
if (is_int($catCounter / 2)) $catStyle = ' class="catAlt"';
$catLink = get_category_link($category->term_id);
echo '<li'.$catStyle.'><h3>'.$category->name.'</h3>';
echo '<ul style="margin-left:15px;">';
query_posts('category__in='.$category->term_id.'&showposts=5');?>
<?php while (have_posts()) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
<li>More <strong><?php echo $category->name; ?></strong></li>
<li> <?php
$sub_cat_id = $category->term_id;
$get_sub_args = array('child_of' =>$sub_cat_id);
$categories_arr = get_categories($get_sub_args);
//print_r ($categories_arr);
foreach ($categories_arr as $sacategory) {
//Display the sub category information using $category values like $category->cat_name
echo '<h2>'.$sacategory->name.'</h2>';
echo '<ul style="margin-left:15px;">';
foreach (get_posts('cat='.$sacategory->term_id) as $post) {
setup_postdata( $post );
echo '<li>'.get_the_title().'</li>';
}
echo '</ul>';
}
?></li>
</ul>
</li>
<?php } ?>
</ul>
<!-- Category Archive End -->
</div>
I'm currently working on a Wordpress-project which has to display all the categories, subcategories and posts within these subcategories of a custom post type & taxonomy.
I should become something like this:
Category 1
subcategory
post 1
post 2
subcategory
post 3
Category 2
subcategory
post 4
At this moment the code returns a list of all the categories and subcategories
in the taxonomy between the h3-tags. Only the parent categories should be displayed here.
<?php
$terms = get_terms('resource_category', array('hierarchical' => false));
foreach ($terms as $term) {
$cat_slug = $term->slug;
$cat_id = $term->term_id;
$subcats = get_categories('child_of='.$cat_id.'&taxonomy=resource_category');
if ( have_posts() ) :
/* CATEGORY */ ?>
<div class="resources">
<?php echo '<h3>'.$term->name.'</h3>';
/* SUBCATEGORY */
foreach ($subcats as $subcat) {
if ( have_posts() ) :
echo '<h4>' . $subcat->name .'</h4>';
query_posts('post_type=resources&resource_category='.$subcat->cat_name.'&hide_empty=1'); ?>
<?php while ( have_posts() ) : the_post();
/* SUBCATEGORY POSTS */?>
<div class="resource-item">
<ul>
<li><?php the_title(); ?></li>
</ul>
</div>
<?php endwhile; endif; wp_reset_query();} ?>
</div>
<?php endif; wp_reset_query(); } ?>
Big thanks if anyone can help me with this!
$terms = get_terms('mobile_category', array('hide_empty'=> 0,'orderby'=> 'count','parent' => 0));
foreach ($terms as $term) {
$cat_slug = $term->slug;
$cat_id = $term->term_id;
$subcats = get_categories('child_of='.$cat_id.'&taxonomy=mobile_category');
// if ( have_posts() ) :
/* CATEGORY */ ?>
<div class="resources">
<?php echo '<h3>cat pr1 -- '.$term->name.'</h3>';
/* SUBCATEGORY */
foreach ($subcats as $subcat) {
if ( have_posts() ) :
echo '<h4>sub 2 ' . $subcat->name .'</h4>';
query_posts('post_type=mobile&mobile_category='.$subcat->cat_name.'&hide_empty=1'); ?>
<?php while ( have_posts() ) : the_post();
/* SUBCATEGORY POSTS */?>
<div class="resource-item">
<ul>
<li><?php the_title(); ?></li>
</ul>
</div>
<?php endwhile;
endif;
wp_reset_query();}
I have a Wordpress recipe blog that gives users recipes for "breakfast" and "lunch." Each recipe is a separate Wordpress custom post type tagged as either "breakfast" or "lunch." (I'm using tags, not categories for a specific reason I won't get into.) The custom post types are called "recipes."
If a user is on a post tagged "breakfast" they see a list of all other posts tagged "breakfast." I've created this list by querying (with WP_Query) all posts tagged "breakfast," and displaying the titles of those posts in a list. The titles also link to the respective posts so the user can jump to any "breakfast"-tagged post while reading a "breakfast"-tagged post.
My problem: How do I add a class of "active" to the post title that corresponds to the post I am currently reading? (So if I'm on the post with title "bacon," the title "bacon" in my list of "breakfast"-tagged posts should have a class of "active." Below is the code I'm using on my single.php page to generate my lists of posts by tags:
<?php if (has_tag( 'breakfast' )) { ?>
<?php $loop = new WP_Query( array( 'post_type' => 'recipes', 'tag' => 'breakfast') ); ?>
<ul>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<?php the_title( '', '' ); ?>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
<?php } elseif (has_tag( 'lunch' )) { ?>
<?php $loop = new WP_Query( array( 'post_type' => 'recipes', 'tag' => 'lunch') ); ?>
<ul>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<?php the_title( '', '' ); ?>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul>
<?php } else { ?>
<?php } ?>
Before the custom queries, put the ID of the current post in a var:
$temp = get_the_ID();
Then, in your custom loop, check if the IDs match:
$class = ( $temp == get_the_ID() ) ? 'active' : '';
and apply the appropriate class to the title:
class="' . $class . '"
I have the following code that generates a list of terms in a taxonomy term, and then POSTS that are under each term.
I want to have a current-page-item class added to the current item, so that when you are on a page under the taxonomy term, its related item in the nav is styled. Here is my code:
<?php $terms = get_terms('benefit-cats');
echo "<ul>";
foreach ($terms as $term) {
$wpq = array ('taxonomy'=>'benefit-cats','term'=>$term->slug,'order'=>'asc','orderby'=>'title');
$query = new WP_Query ($wpq);
echo "<li class=".$term->slug."><span class=\"list-item\"><span class=\"text-arrow\">►</span> ".$term->name."</span>"; ////
echo "<ul class=\"children\">";
?>
<?php
if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
<li><span class="text-arrow">►</span> <?php the_title();?></li>
<?php endwhile; endif; wp_reset_query(); ?>
<?php
echo "</ul></li>";
}
echo "</ul>";
?>
You can try something like this...
Specify the current post_id before your loop, then condition to see if post loop contains your post_id.
// before loop
$page_id = $wp_query->get_queried_object_id();
// replace <li><span class="text-arrow">►</span>
if($page_id ==$query->post->ID ) $class = " current-page-item";
<li><span class="text-arrow<?php echo $class; ?>">►</span>