how to display wordpress categories in two columns? - php

I'd like to display my categories 2 at the time - 2 in each row and have as many rows as needed.
I've got so far with this code (which displays all the categories):
<?php
$i = 1;
$args = array(
'orderby' => 'name',
'parent' => 0,
'hide_empty' => 0
);
$categories = get_categories( $args );
echo count( $categories );
echo "<div class='a'>\n";
foreach ( $categories as $category ) {
echo "<div class='b'>\n";
echo "<div class='c'><a href='" . get_category_link( $category->term_id ) . "'>" . $category->name . "</a></div>\n";
echo "</div>\n";
}
echo "</div>\n";
?>
I know it can be done using list, but this is something I want to avoid.

So this is it (took a while) and it works just fine ;-)
$args = array(
'orderby' => 'name',
'parent' => 0,
'hide_empty' => 1
);
$categories = get_categories( $args );
$list_cats = array();
foreach ( $categories as $category ) {
array_push($list_cats, $category->name);
}
$total_cats=count($categories);
echo "<div class='a'>";
for ($a=0; $a<$total_cats;) {
for ($j=0; $j<2; $j++) {
echo " <div class='b'><div class='c'>" . $list_cats[$a] . "</div></div>";
$a++;
}
echo "</div>";
if($a<$total_cats){
echo "<div class='a'>";
}
}

Related

How to sort an array I get using Wordpress function get_term?

I am getting certain items using the get_term() function and displaying the result as a list.
It works perfectly as intended, but how do I sort the results alphabetically?
Code is as follows:
<?php
$terms = get_terms(array(
'taxonomy' => 'behandlungsfeld',
'parent' => 0,
'post_status' => 'publish',
));
foreach ($terms as $term) {
$the_term_children = get_terms(array(
taxonomy' => 'behandlungsfeld',
'child_of' => $term->term_id,
));
echo '<li data-dropdown-text="' . $term->name . '">';
echo '<ul>';
foreach ($the_term_children as $term_child) {
$term_child_fetched = get_term($term_child, 'behandlungsfeld');
echo '<li>' . $term_child_fetched->name . '</li>';
}
echo '</ul>';
}
?>
You can use the orderby arg like so:
get_terms(['orderby' => 'name']);

How to Show Related Products by Category in WordPress

How to Show Related Products by Category in WordPress?
Products Category:
Raised Access Flooring
Ancillary Products
Knauf Flooring
Recycled Panels
E.g current page is Raised Access Flooring only display Ancillary Products, Knauf Flooring, Recycled Panels in Related Products.
I added code in single-products.php see below:
$orderby = 'name';
$order = 'random';
$hide_empty = true ;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
);
$product_categories = get_terms( 'product_category', $cat_args );
if( !empty($product_categories) ){
echo '<div class="container">';
echo '<div class="row">';
foreach ($product_categories as $key => $category) {
$description = $category->description;
echo '<div class="col-lg-6">';
echo '<div class="card">';
echo '<a href="'.get_term_link($category).'" >';
$image = get_field('product_category', $category );
if($image) {
echo '<img class="card-img-top" src="' . $image['url'] . '" alt="' . $image['alt'] .'">';
} else {
echo '<img class="card-img-top" src="/wp-content/uploads/2018/07/placeholder.png">';
}
echo '<div class="card-content">';
echo '<h3>' . $category->name . '</h3>';
echo '<p>';
echo strlen($description) > 60 ? substr($description, 0, 100) . '...' : $description;
echo '</p>';
echo '<button class="button btn_medium btn_orange btn_squared btn_normal_style" href="'.get_term_link($category).'" >Discover more</button>';
echo '</div>';
echo '</a>';
echo '</div>';
echo '</div>';
}
echo '</div>';
echo '</div>';
}
else {
// no posts found
echo wpautop( 'Sorry, no products were found' );
}
First, get the terms of the category associated to the current product:
$terms = wp_get_post_terms( get_the_id(), 'product_cat', array('fields' => 'ids') );
The id of current product is obtained with the function: get_the_id()
Second, get the other products that sharing the same terms (excluding from the result the current product):
$products = get_posts(array(
'post_type' => 'product',
'post_status' => 'publish',
'numberposts' => -1,
'post__not_in' => array(get_the_id()),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $terms,
'include_children' => false
)
)
));
The complete code would be:
$terms = wp_get_post_terms( get_the_id(), 'product_cat', array('fields' => 'ids') );
$products = get_posts(array(
'post_type' => 'product',
'post_status' => 'publish',
'numberposts' => -1,
'post__not_in' => array(get_the_id()),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $terms,
'include_children' => false
)
)
));
Look that I'm getting an array with only the ids of the terms:
$terms = wp_get_post_terms( get_the_id(), 'product_cat', array('fields' => 'ids') );
So, you should edit your snippet of code:
foreach ($terms as $key => $term) {
as follows:
foreach ($terms as $key => $term_id) {
$term = get_term($term_id);
By the way in your piece of code you are not using the $products variable with the array of related products, that was your initial question.
#MikeD This only display current product not other 3 products display, have l done it wrong?
`<?php
$orderby = 'name';
$order = 'random';
$hide_empty = true ;
$cat_args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
);
$terms = wp_get_post_terms( get_the_id(), 'product_category', array('fields' => 'ids') );
echo '<div class="container">';
echo '<div class="row">';
foreach ($terms as $key => $term) {
$term = get_term($term_id);
echo '<a href="' .get_term_link($term). '" ></a>';
echo '<div class="col-lg-6">';
echo '<div class="card">';
echo '<a href="'.get_term_link($term).'" >';
$image = get_field('product_category', $term );
if($image) {
echo '<img class="card-img-top" src="' . $image['url'] . '" alt="' . $image['alt'] .'">';
} else {
echo '<img class="card-img-top" src="/wp-content/uploads/2018/07/placeholder.png">';
}
echo '<div class="card-content">';
echo '<h3>' . $term->name . '</h3>';
//echo $category->name;
//echo '<p>' . $category->description . '</p>';
echo '<p>';
echo strlen($description) > 60 ? substr($description, 0, 100) . '...' : $description;
echo '</p>';
//echo '<p>' . mb_strimwidth($category->description, 0, 30, "...") . '</p>';
echo '<button class="button btn_medium btn_orange btn_squared btn_normal_style" href="'.get_term_link($term).'" >Discover more</button>';
echo '</div>';
echo '</a>';
echo '</div>';
echo '</div>';
}
echo '</div>';
echo '</div>';
?>`

Foreach - recursion?

I have this code that is getting all categories and subcategories from a woocommerce based website in a hierarchical order, and it works but each and every time when I want to add a new level of depth
category
->sub-cat
->sub-sub-cat
->sub-sub-sub-cat
->etc
I have to add one more foreach loop inside the last loop (I'm not even sure if you can understand me to cause honestly I don't even know how to explain this - sorry, please be patient, it is my second month in PHPs 'fields')
So, I'm trying to get rid of all those foreach loops (to make it more dynamic) and I heard that there is a way to do that by using something called recursion, the kind of stuff that I can use to make this happen. I don't really understand how that works and I've read like the whole day about it so if there is somebody who can help me understand how to get this done I'll be happy?
Here is my code, part of it is taken from StackOverflow.
<ul>
<?php
$taxonomy = 'product_cat';
$orderby = 'name';
$hierarchical = 1;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'hierarchical' => $hierarchical
);
$cat = get_categories( $args );
foreach ($cat as $c) {
if($c->category_parent == 0) {
$catID = $c->term_id;
echo '<li>'. $c->name .'';
$args = array(
'taxonomy' => $taxonomy,
'parent' => $catID,
'orderby' => $orderby,
'hierarchical' => $hierarchical
);
$cat = get_categories( $args );
if($cat) {
echo '<ul>';
foreach($cat as $c) {
$catID = $c->term_id;
echo '<li>'. $c->name , apply_filters( 'woocommerce_subcategory_count_html', ' (' . $c->count . ')', $category ) .'';
$args = array(
'taxonomy' => $taxonomy,
'parent' => $catID,
'orderby' => $orderby,
'hierarchical' => $hierarchical
);
$cat = get_categories( $args );
if($cat){
echo '<ul>';
foreach ($cat as $c) {
$catID = $c->term_id;
echo '<li>'. $c->name , apply_filters( 'woocommerce_subcategory_count_html', ' (' . $c->count . ')', $category ) .'';
$args = array(
'taxonomy' => $taxonomy,
'parent' => $catID,
'orderby' => $orderby,
'hierarchical' => $hierarchical
);
$cat = get_categories( $args );
if($cat){
echo '<ul>';
foreach ($cat as $c) {
$catID = $c->term_id;
echo '<li>'. $c->name , apply_filters( 'woocommerce_subcategory_count_html', ' (' . $c->count . ')', $category ) .'</li>';
}
echo '</ul>';
}
echo '</li>';
}
echo '</ul>';
}
echo '</li>';
}
echo '</ul>';
}
echo '</li>';
}
}
?>
It works perfectly, though I'm aware that it is not too smart, so please don't be too harsh on me.
Here is an example of recursion:
You create a function that looks at the item of the array if it's an array you call the function again and pass the item to it (the subarray).
If it's not an array you echo it.
$arr = [1,2,3,[4,5,[6]]];
print_items($arr);
Function print_items($arr){
Foreach($arr as $item){
If(is_array($item)){
print_items($item);
}Else{
Echo $item;
}
}
}
https://3v4l.org/vbjaO
A WordPress-specific printing function might look like this:
<?php
function display_child_categories_of($category_id) {
$subcategories = get_categories(array('child_of' => category_id));
foreach ($subcategories as $subcategory) {
echo '<li>';
echo $subcategory->name;
echo '<ul class="children">';
display_child_categories_of($subcategory);
echo '</ul>';
echo '</li>';
}
}
?>
<ul>
<?php
display_child_categories_of(0); // gets all top-level categories
?>
</ul>
This is a barebones version; you'll want to add in the additional parameters you're using to the get_categories() arguments and then tweak the HTML as needed.
However, as #jaswrks mentioned in the comments, you should use the built-in walk_category_tree() WordPress function instead if it works for your situation.

Count number of posts in child custom taxonomy

i have this code:
<?php
$taxonomyName = "free";
$terms = get_terms($taxonomyName,array('parent' => 0));
foreach($terms as $term) {
$term_children = get_term_children($term->term_id,$taxonomyName);
echo '<ul>';
foreach($term_children as $term_child_id) {
$term_child = get_term_by('id',$term_child_id,$taxonomyName);
echo '<li>' . $taxtitle . ' ' . $term_child->name . '</li>';
}
echo '</ul>';
}
?>
I want to count posts from each child taxonomy of this parent taxonomy.
I want to result something like this:
echo '<li>' . $taxtitle . ' ' . $term_child->name . ' (COUNT - Episodes)</li>';
Thank you.
it should be fairly simple :
<?php
$taxonomyName = "free";
$terms = get_terms($taxonomyName,array('parent' => 0));
foreach($terms as $term) {
$term_children = get_term_children($term->term_id,$taxonomyName);
echo '<ul>';
foreach($term_children as $term_child_id) {
$term_child = get_term_by('id',$term_child_id,$taxonomyName);
$q = new WP_Query(array(
'nopaging' => true,
'tax_query' => array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $term_child_id,
'include_children' => true,
)));
$count = $q->post_count;
echo '<li>' . $taxtitle . ' ' . $term_child->name . '(Season-'.$count.')</li>';
}
echo '</ul>';
}
?>

Sorting wordpress posts by date published

im using the following code to display my posts on a page in wordpress that use a featured image:
$mypages = get_pages( array() );
if ( !empty( $mypages ) ) {
echo '<ul>';
foreach ( $mypages as $mypage ) {
if ( get_the_post_thumbnail( $mypage->ID ) ) {
echo '<div class="featured-container">';
echo '<div class="featured-image">';
echo '<li><a class="feat-hover" href="' . get_permalink( $mypage->ID ) . '">' . get_the_post_thumbnail( $mypage->ID ) . '</a></li>';
echo '</div>';
echo '<div class="featured-text">';
echo '' . get_the_title($mypage->ID ) . '';
echo '</div>';
echo '</div>';
}
}
echo '</ul>';
}
but before printing this information out i want to sort the $mypages array so that they display by date published. ive tried this code:
<?php $args = array(
'sort_order' => 'ASC',
'sort_column' => 'post_date',
);
$mypages = get_pages($args);
?>
but it doesnt seem to work, am i missing something or doing this the wrong way?
thanks in advance.
FULL CODE BEING USED:
$args = array(
'sort_order' => 'ASC',
'sort_column' => 'post_date'
);
$mypages = get_pages( array($args) );
if ( !empty( $mypages ) ) {
echo '<ul>';
foreach ( $mypages as $mypage ) {
if ( get_the_post_thumbnail( $mypage->ID ) ) {
echo '<div class="featured-container">';
echo '<div class="featured-image">';
echo '<li><a class="feat-hover" href="' . get_permalink( $mypage->ID ) . '">' . get_the_post_thumbnail( $mypage->ID ) . '</a></li>';
echo '</div>';
echo '<div class="featured-text">';
echo '' . get_the_title($mypage->ID ) . '';
echo '</div>';
echo '</div>';
}
}
echo '</ul>';
}
The issue is that you're passing $args into another array(). You only need to pass in the $args directly, since it's already an array(). Change the top block of code to:
$args = array(
'sort_order' => 'ASC',
'sort_column' => 'post_date'
);
$mypages = get_pages( $args );

Categories