У меня есть функция php, которую я пытаюсь сделать максимально эффективной, но есть избыточность, от которой я не могу избавиться. Поэтому мне нужна помощь.
Вот фактический код, если вы хотите посмотреть
function array_tags( $classes, $item, $args ){
if ( 'themes' === $args->theme_location ) {
$tagsString = "";
//cannot have spaces!!!!! change the page_title for something else
$cat = get_queried_object();
$catSlug = $cat->slug;
$args = array(
'post_type' => 'product',
'product_cat' => $catSlug,
'posts_per_page' => -1,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$tagsString .= strip_tags($product->get_tags()) . ", ";
endwhile;
} else {
}
wp_reset_postdata();
$arr_tags = explode( ", ", $tagsString );
var_dump(array_unique($arr_tags));
if( !in_array($item->title, array_unique($arr_tags)) ){
$classes[] = "not-the-droid-you-are-looking-for";
}
else{
}
}
return $classes;
}
add_filter('nav_menu_css_class', 'array_tags', 10, 3);
var_dump($arr_tags);
array(12) {
[0]=> string(20) "Discover your Wonder"[1]=> string(3) "web"[2]=> string(6) "aliens"[3]=> string(9) "astrology"[4]=> string(16) "celestial bodies"[5]=> string(20) "Discover your Wonder"[6]=> string(5) "humor"[7]=> string(9) "mythology"[8]=> string(7) "science"[9]=> string(5) "space"[10]=> string(20) "Discover your Wonder"[11]=> string(0) ""}
array(12) {
[0]=> string(20) "Discover your Wonder"[1]=> string(3) "web"[2]=> string(6) "aliens"[3]=> string(9) "astrology"[4]=> string(16) "celestial bodies"[5]=> string(20) "Discover your Wonder"[6]=> string(5) "humor"[7]=> string(9) "mythology"[8]=> string(7) "science"[9]=> string(5) "space"
и так далее, почему var_dump в $ arr_tags создает несколько массивов?
var_dump($tagsString)
string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, "
var_dump(strip_tags($product->get_tags()));
string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, "
var_dump((string)strip_tags($product->get_tags()));
string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, " string(25) "Discover your Wonder, web" string(91) "aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space" string(20) "Discover your Wonder" string(142) "Discover your Wonder, web, aliens, astrology, celestial bodies, Discover your Wonder, humor, mythology, science, space, Discover your Wonder, "
В вашем tagsString есть синтаксическая ошибка: вы не заключаете строку в строку, вместо этого вы просто используете «,».
Вот рабочий код вашего
<?php
$tagsString = "cats, dogs, dogs, dogs";
$array_tags = explode(", ", $tagsString);
$unique = array_unique($array_tags);
print_r($unique);
Других решений пока нет …