0

I'm trying to load some code depending on which portfolio category i am , but cant seem to get it quite right... I've read that portfolio categories are called taxonomies, but don't know if thats right.. tried this code in my functions.php but crashed my whole site!

add_action('wp_footer', 'ava_auto_click');
function ava_auto_click(){
$the_terms = get_the_terms( $post->ID , 'portfolio_entries');
for ($i=0;$i< count($the_terms); ++$i) {
    var_dump($the_terms[$i]);
if($the_terms[$i] =="projects"){     
?>
<script>
    window.onload = function(){     
        var elem = document.querySelector('.wrap-filter').querySelectorAll('li')
        var event = new Event('click');
        document.getElementById('projects').classList.remove('none')
        elem[0].setAttribute("data-filter", ".offices, .retail, .residential, .stands, .cultural");
        elem[0].firstElementChild.setAttribute("data-filter", ".offices, .retail, .residential, .stands, .cultural");
        elem[1].classList.toggle('active')
        elem[1].querySelector('a').click();

    }
</script>
<?php

    }
    else { 


        ?>
        <script>
            window.onload = function(){     
                var elem = document.querySelector('.wrap-filter').querySelectorAll('li')
                var event = new Event('click');
                document.getElementById('projects').classList.remove('none')
                elem[1].classList.toggle('active')
                elem[1].querySelector('a').click();

            }
        </script>
        <?php
         }
    }
}

Thanks in advance for the help!!

I think the main structure is right but the if and the $the_terms = get_the_terms( $post->ID , 'portfolio_entries'); might be wrong

2
  • A "portfolio category" could just be a Core category, or could be a custom taxonomy - but also could be postmeta, just depends on how it was originally set up. So, your first step is to determine what type of data these portfolio categories actually are. You can see if it's a Core Category by logging in, hovering over the Posts menu, and clicking Categories. If the portfolio categories are there, they're Core Categories. If not, search through your theme or plugin's code to find a call to register_taxonomy() to confirm whether they're custom taxonomies.
    – WebElaine
    Commented Jan 27, 2020 at 20:18
  • Alright will try that! Meanwhile i just did the if statement with the current url of the page instead of the porfolio category. Commented Jan 28, 2020 at 9:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.