1

I'm setting up a project gallery using isotope jquery plugin for layout and filterability. I've seen examples of how to add a list of category links and modify them to filter on click, but... I want to keep the url in the href attr, so that searchbots (or the theoretical non-js user) can click thru to project category pages. My code works until I set up the $link variable and try to insert it into the html. Yeah.... my php is weak. How do I add the link to the href attr?

/**
 * Set up list of category links for gallery filter
 */
function galleryFilterLinks() {
    $categories = get_categories('exclude=1');
    $link = the_permalink();
    $html = '<ul class="gallery-filter">';
    $html .= '<li><a href="#" data-filter="*" class="selected">All</a></li>';
    foreach ($categories as $category) {
        $html .= "<li><a href='{$link}' data-filter='{$category->category_nicename}'>{$category->cat_name}</a></li>";   
    }
    $html .= '</ul>';
    echo $html;
}
1

Use get_permalink (which returns the URL) instead of the_permalink (which prints the URL).

Try:

foreach ( $categories as $category ) {
    $link = get_term_link( $category );
    $html .= "<li><a href='{$link}' data-filter='{$category->category_nicename}'>{$category->cat_name}</a></li>";   
}
  • I tried that, but it gets the link of the current page, as opposed to the link of the cat page. – heytricia Aug 26 '14 at 16:54
  • My bad, check revision. – TheDeadMedic Aug 26 '14 at 17:00
  • 1
    You're the bomb! Note I also had to move the $link var down a few lines. Thanks much! – heytricia Aug 26 '14 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.