我有一个 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"
等等,为什么 $arr_tags 上的 var_dump 会产生多个数组?
另外,这里是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, "