I have the following function:
function my_acf_load_field( $field )
{
global $post;
global $current_user;
$field['choices'] = array();
wp_reset_query();
$query = new WP_Query(array(
'post_type' => 'gear',
'orderby' => 'title',
'order' => 'ASC',
'author' => $current_user->ID,
'posts_per_page' => -1,
));
foreach($query->posts as $product_id=>$macthed_product){
$zz = get_field('brand_preselect',$macthed_product->ID);
$yy = get_field('category_tax',$macthed_product->ID);
$aa = get_field('weight',$macthed_product->ID);
foreach( $yy as $term ):
$choices[$macthed_product->ID] = '<u class="cat">' . $term->name . '</u>' . '<i class="brand">' . $zz . '</i>' . $macthed_product->post_title . ' (' . $aa . ' kg)';
endforeach;
}
$field['choices'] = array();
if( is_array($choices) )
{
foreach( $choices as $key=>$choice )
{
$field['choices'][$key] = $choice;
}
}
// wp_reset_query();
return $field;
}
add_filter('acf/load_field/name=choices', 'my_acf_load_field');
This works well in that it populates the checkboxes with the category name, brand and title. For some reason $term->name in the foreach only shows the child category. If I echo out $term->name before the $choices[$macthed_product->ID] it shows all that apply. How do I get $term->name in the foreach to show all taxonomy matches (parent and child)
I am creating a filtering system to show all users within a Wordpress site (they have signed up via the User Registration Plugin).
I want to show a list of all users, filtering by their company. I have managed to call the company on each li by using:-
.str_replace( ' ', '-', $user->user_registration_company ) .
However I am struggling to do this for the buttons that actually filter, as it displays company names multiple times. Please see full code below:-
<div class="networking-list">
<?php
$args = array(
'role' => 'subscriber',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users( $args );
foreach ( $users as $user ) {
echo
'<button class="filter" data-filter="all">All</button>
<button class="filter" data-filter="all">'.str_replace( ' ', '-', $user->user_registration_company ) . '</button>';
}
$args = array(
'role' => 'subscriber',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users( $args );
echo '<ul>';
foreach ( $users as $user ) {
echo
'<li class="mix ' .str_replace( ' ', '-', $user->user_registration_company ) . '">' . get_avatar( $user->ID, $size = 280 ) .'<h2 class="filter-txt">' . esc_html( $user->first_name ) . ' ' . esc_html( $user->last_name ) .'</h2><h3></li>';
}
echo '</ul>';
?>
</div>
So I basically want the button to show the user company, but only once so I can filter.
Thank you!
The function you are searching for is array_unique
Just load all companies into an array and run the function on it. All duplicates will be removed and you are good to go.
<?php
$companies = array();
$args = array(
'role' => 'subscriber',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users($args);
foreach ($users as $user)
{
$companies[] = $user->user_registration_company;
}
$companies = array_unique($companies);
foreach ($companies as $company)
{
echo '<button class="filter" data-filter="all">All</button>
<button class="filter" data-filter="all">' . $company . '</button>';
}
$args = array(
'role' => 'subscriber',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$users = get_users($args);
echo '<ul>';
foreach ($users as $user)
{
echo '<li class="mix ' . str_replace(' ', '-', $user->user_registration_company) . '">' . get_avatar($user->ID, $size = 280) . '<h2 class="filter-txt">' . esc_html($user->first_name) . ' ' . esc_html($user->last_name) . '</h2><h3></li>';
}
echo '</ul>';
?>
I create a catalog of companies by cities using the Wordpress categories.
Categories are cities.
Custom field is a region in which cities are included.
For custom field used - Advanced Custom Fields
For build custom taxanomy order used - WP Term Order
$terms = get_terms( 'catalog_categories', array(
'depth' => 1,
'number' => 100,
'parent' => 0,
'orderby' => 'taxon_name', // order array name
'order' => 'DESC',
'hide_empty' => false,
'meta_query' => array(
'taxon_name' => array(
'key' => 'taxon' // acf custom field name
) )
) );
foreach ($terms as $key => $object) {
$region = get_field('taxon', $object);
echo $region . ' ' . $object->name . '<br>';
}
I recive:
Wielkopolskie Poznań
Wielkopolskie Bojanowo
Mazoweckie Warszawa
Dolnoslaskie Wrocław
Dolnoslaskie Jawor
But i need:
Wielkopolskie
-- Poznań
-- Bojanowo
Mazoweckie
-- Warszawa
Dolnoslaskie
-- Wrocław
-- Jawor
Help please with the decision of this problem
UPDATE
*Help please add a link (slug) to the Wordpress category to the code
Here we collect all the cities of one region:
$collection[$region][] = $object->name;
We need to add to each city a slug
$object->slug;
The slug we need to use it as value for option.
'<option value="' . **SLUG** . '" data-select2-id="'. $data_select_id . '-' .$i .'">' . $city . '</option>';
UPDATE:
*Working code
We added to - $collection[$region][] = $object->name; - $object->slug and combined them into an array
$collection[$region][] = array('name' => $object->name, 'slug' => $object->slug);
And for the output we use - $city['name'] and $city['slug']
<?php
$terms = get_terms( 'catalog_categories', array(
'depth' => 1,
'number' => 100,
'parent' => 0,
'orderby' => 'taxon_name', // order array name
'order' => 'DESC',
'hide_empty' => false,
'meta_query' => array(
'taxon_name' => array(
'key' => 'taxon' // acf custom field name
) )
) );
$collection = [];
foreach ($terms as $key => $object) {
$region = get_field('taxon', $object);
if (!isset($collection[$region])) {
$collection[$region] = [];
}
$collection[$region][] = array('name' => $object->name, 'slug' => $object->slug);
}
$data_select_id = 1;
echo '<div>';
foreach ($collection as $region => $cities) {
$i = 0;
echo '<div label="' . $region . '" data-select2-id="' . $data_select_id . '">'. $region;
foreach ($cities as $city) {
$i++;
echo '<option value="' . $city['slug'] . '" data-select2-id="'. $data_select_id . '-' .$i .'">' . $city['name'] . '</option>';
}
echo '</div>';
$data_select_id ++;
}
echo '</div>';
;?>
You could use an additional array to sort out cities into regions like this:
$collection = [];
foreach ($terms as $key => $object) {
$region = get_field('taxon', $object);
if (!isset($collection[$region])) {
$collection[$region] = [];
}
$collection[$region][] = $object->name;
}
foreach ($collection as $region => $cities) {
echo $region . '<br/>';
foreach ($cities as $city) {
echo '--' . $city . '<br/>';
}
}
Hi I am working with a Wordpress gallery plugin.
I would like the first link in the filter menu to automatically be active when the page loads. This is as by default it loads ALL the images from all the categories and it's too many! Thanks.
<?php
wp_enqueue_script('imagesloaded', plugins_url('/assets/plugins/imagesloaded/imagesloaded.pkgd.min.js', __FILE__), array('jquery'), GO_GALLERY_VERSION, true);
wp_enqueue_script('isotope', plugins_url('/assets/plugins/isotope/isotope.pkgd.min.js', __FILE__), array('jquery'), GO_GALLERY_VERSION, true);
wp_enqueue_script('go-gallery', plugins_url('/assets/js/gallery.js', __FILE__), array('jquery', 'isotope', 'imagesloaded'), GO_GALLERY_VERSION, true);
wp_enqueue_style('go-gallery', plugins_url('/assets/css/gallery.css', __FILE__), null, GO_GALLERY_VERSION);
wp_enqueue_script('tos', plugins_url('/assets/plugins/tos/js/jquery.tosrus.min.custom.js', __FILE__), array('jquery'), GO_GALLERY_VERSION, true);
wp_enqueue_style('tos', plugins_url('/assets/plugins/tos/css/jquery.tosrus.custom.css', __FILE__), null, GO_GALLERY_VERSION);
$labels = array(
'name' => _x('Media Categories', 'taxonomy general name', 'go_gallery'),
'singular_name' => _x('Media Category', 'taxonomy singular name', 'go_gallery'),
'search_items' => __('Search Media Categories', 'go_gallery'),
'all_items' => __('All Media Categories', 'go_gallery'),
'parent_item' => __('Parent Media Category', 'go_gallery'),
'parent_item_colon' => __('Parent Media Category:', 'go_gallery'),
'edit_item' => __('Edit Media Category', 'go_gallery'),
'update_item' => __('Update Media Category', 'go_gallery'),
'add_new_item' => __('Add New Media Category', 'go_gallery'),
'new_item_name' => __('New Media Category Name', 'go_gallery'),
'menu_name' => __('Media Categories', 'go_gallery'),
);
$args = array(
'hierarchical' => TRUE,
'labels' => $labels,
'show_ui' => TRUE,
'show_admin_column' => TRUE,
'query_var' => TRUE,
'rewrite' => TRUE,
);
register_taxonomy('attachment_category', 'attachment', $args );
$output = '';
$args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'posts_per_page' => $atts['limit'],
'order' => 'DESC',
'orderby' => $atts['sort'],
'post_mime_type' => 'image/jpeg,image/gif,image/jpg,image/png'
);
$categories = array();
$atts['icat'] = array_map('sanitize_title', explode(',', $atts['icat']));
foreach ( $atts['icat'] as $category ) {
if ( $term = get_term_by('slug', $category, 'attachment_category') ) {
$categories[$term->term_id] = $term;
}
}
if ( !empty($categories) ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'attachment_category',
'field' => 'term_id',
'terms' => array_keys($categories)
)
);
}
$atts['menu_gap'] = min($atts['menu_gap'], 100);
$classes[] = 'go-gallery';
$classes[] = 'menu-' . $atts['menu_pos'];
$classes[] = go_gallery_bool($atts['menu_show']) ? 'menu-show' : '';
$classes[] = 'size-' . $atts['size'];
$classes[] = 'style-' . $atts['style'];
$attributes = array();
$attributes['class'] = join(' ', $classes);
$attributes['id'] = 'go-' . substr(md5(mt_rand(0, PHP_INT_MAX)), 0, 6);
$attributes['data-gap'] = intval($atts['gap']);
$attributes['data-border-color'] = $atts['border_color'];
$attributes['data-lightbox'] = go_gallery_bool($atts['lightbox']) ? 'yes' : 'no';
$attributes['data-desc-color'] = $atts['desc_color'];
$attributes['data-menu-color'] = $atts['menu_color'];
$attributes['data-menu-bg'] = $atts['menu_bg'];
$attributes['data-menu-bg-hover'] = $atts['menu_bg_hover'];
$attributes['data-menu-gap'] = $atts['menu_gap'];
$attributes['data-bg'] = $atts['bg'];
$attributes['data-border-size'] = $atts['border_size'];
$attributes['data-overlay-color'] = go_gallery_hex2rgb($atts['overlay_color']);
$thumb_size = 'medium';
if ( $atts['size'] == 'large' || ($atts['style'] == 'squared' && in_array($atts['size'], array('medium', 'large'))) ) {
$thumb_size = 'large';
}
foreach ( $attributes as $attribute => $value ) {
$attributes[$attribute] = $attribute . '="' . $value . '"';
}
$query = new WP_Query($args);
$output .= '<div ' . join(' ', $attributes) . '>';
$output .= '<ul class="go-gallery-filters">';
$output .= '<li>';
$output .= '<a data-filter="" href="#">' . __($atts['menu_button'], 'go_gallery') . '</a>';
$output .= '</li>';
foreach ( $categories as $category ) {
if ( !empty($category) ) {
$output .= '<li class="active">';
$output .= '<a data-filter="' . $category->slug . '" href="#">' . $category->name . '</a>';
$output .= '</li>';
}
}
$output .= '</ul>';
$output .= '<div class="go-gallery-list-wrapper">';
$output .= '<ul class="go-gallery-list">';
foreach ( $query->posts as $post ) {
$category_terms = wp_get_post_terms($post->ID, 'attachment_category');
$classes = array();
$classes[] = 'go-gallery-item';
foreach ( $category_terms as $category_term ) {
$classes[] = 'category-' . $category_term->slug;
}
$image_source = wp_get_attachment_image_src($post->ID, 'full');
$output .= '<li data-source="' . $image_source[0] . '" class="' . join(' ', $classes) . '">';
$output .= '<a class="image-wrap" href="' . $image_source[0] . '">';
$output .= '<figure>';
$output .= wp_get_attachment_image($post->ID, $thumb_size);
$output .= '<div class="image-overlay">';
if ( go_gallery_bool( $atts['hover_data'] ) ){
$output .= '<h3>' . $post->post_title . '</h3>';
$output .= '<h4>' . $post->post_content . '</h4>';
}
$output .= '</div>';
$output .= '</figure>';
$output .= '</a>';
$output .= '</li>';
}
$output .= '</ul>';
$output .= '</div>';
$output .= '</div>';
return $output;
if(strlen($hex) == 3) {
$r = hexdec(substr($hex,0,1).substr($hex,0,1));
$g = hexdec(substr($hex,1,1).substr($hex,1,1));
$b = hexdec(substr($hex,2,1).substr($hex,2,1));
} else {
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
}
$rgb = array($r, $g, $b);
return 'rgba(' . join(', ', $rgb) . ', ' . $alpha .')';
That depends what you mean with "active".
:active is a CSS pseudo selector that can change the appearance of a link while it is being activated. If you want to add a css-class "active" to the first li-element in your loop, you could try something like the following:
foreach($categories as $category) {
$counter = 0; // COUNTER OF CATEGORIES...
$selected = ''; // THE STRING TO PRINT IN SELECTED LI-ELEMENT
if ( !empty($category) ) {
$selected = ($counter == 0) ? 'class="active"' : ''; // SET CLASS ACTIVE FOR THE FIRST CATEGORY
$output .= '<li '. $selected .'>';
$output .= '<a data-filter="' . $category->slug . '" href="#">' . $category->name . '</a>';
$output .= '</li>';
$counter++; //INCREMENT COUNTER OF CATEGORIES
}
}
Assuming your categories get sent via a simple link like so:
first category
second category
then you would have something like this in php:
if(isset($_GET['category']) {
$category_id = $_GET['category'];
} else {
$category_id = 1; // default
}
// this is a blank guess, we don't know where you get your data from:
$category = $categories[$category_id];
The rest depends on your data-structure.
But this is how you could set a default category.
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'>";
}
}