Support

Account

Home Forums General Issues get_category_link from taxonomy addon

Solving

get_category_link from taxonomy addon

  • I’m using the taxony addon so I can select a category in a repeater field and then display an image with link to that category in the final output.

    For some reason I’m having huge difficulties in getting the category ID that’s outputted to work correctly with then getting that category’s url. For example this

    <p><?php echo the_sub_field('site_cat_link'); ?></p>

    outputs the category’s ID using the taxonomy addon selector, but then when I add it to this it doesn’t seem to do anything other than output the category with ID of 1 for every repeater field.

    <?php
        $variable = get_sub_field('site_cat_link');
        $category_link = get_category_link( $variable );
    ?>
    <a href="<?php echo esc_url( $category_link ); ?>" title="Category Name">Category Name</a>

    This is the second ACF thing I’ve been trying to get to work this weekend and failing badly. I’m sure it’s close but can’t figure out why it won’t work. Any ideas or help would be greatly appreciated.

  • Hi @greencode

    Your code is missing the repeater field loop, so I can’t direct you much on this one without seeing whole picture.

    Have you debugged your code line by line? Do you know which line is not producing the correct result?

    Have you tested each variable?

    Thanks
    E

  • Apologies Elliot.

    Here’s all of the code that I’m using

    
    <?php while ( have_posts() ) : the_post(); ?>
    
    <?php if( get_field('site_cats') ): ?>
    <div class="clearfix" id="site-cats">
    <?php while( has_sub_field('site_cats') ): ?>
    <div class="cat-block g_6 clearfix">
    <div class="image">
    <?php echo wp_get_attachment_image(get_sub_field('site_cat_image'), 'half-img'); ?>
    </div>
    <div class="details">
    <h2><?php echo the_sub_field('site_cat_title'); ?></h2>
    <p><?php echo the_sub_field('site_cat_description'); ?></p>
    <p><?php echo the_sub_field('site_cat_link'); ?></p>
    
    <?php
        $variable = get_sub_field('site_cat_link');
        $category_link = get_category_link( $variable );
    ?>
    <a href="<?php echo esc_url( $category_link ); ?>" title=""><?php echo the_sub_field('site_cat_title'); ?></a>
    
    </div>
    </div>
    	<?php endwhile; ?>
    </div>
    <?php endif; ?>
    
    <?php endwhile; // end of the loop. ?>
    

    The three sub fields are working correctly i.e. title, description and link are all pulling in their fields but it’s when I then attempt to get the category ID from the site_cat_link field to then produce the category link that I’m having issues.

    p.s. I use ACF in a lot of my projects now and would like to donate each time I use it – do you have such an option?

  • Hi @greencode

    Can you please clarify your abpove statement:
    The three sub fields are working correctly i.e. title, description and link

    You then say
    ‘get the category ID from the site_cat_link field to then produce the category link that I’m having issues.’

    I can’t see any code which ‘gets the category ID’

    In general, can you please debug your code. This is a neccessary part of web development. All this means is to use a function such as var_dump to dump out the variable or return value from a function. This will allow you to see what data you have.

    Please debug each line and return with your findings.

    Thanks
    E

  • Apologies for the confusion and thanks for helping out on this. I’ve removed the code that’s actually working from the example below and included the dump of what’s getting produced. It is getting the category ID from the ACF custom field.

    Custom fields in Use (to get category ID):
    site_cat_link = taxonomy field type (Taxonomy Add-on)
    cat_id = Text field type (I’ve just manually added the categories I want to pass, just to see if this works).

    Code:

    <?php if( get_field('site_cats') ): ?>
    	<?php while( has_sub_field('site_cats') ): ?>
    <?php
        $variable = get_sub_field('site_cat_link');
        var_dump($variable);
        $category_link = get_category_link( $variable );
        var_dump($category_link);
    ?>
    
    <p><?php echo the_sub_field('site_cat_link'); ?></p>
    <p><?php echo the_sub_field('cat_id'); ?></p>
    <a href="<?php echo esc_url( $category_link ); ?>" >Category</a>
    
    	<?php endwhile; ?>
    <?php endif; ?>
    

    Output when using cat_id (text field):
    Category string(3) “249” string(57) “http://www.mysite.com/inspiration-ideas/”
    249
    249
    Category string(3) “346” string(50) “http://www.mysite.com/tips-advice/”
    346
    346

    Output when using site_cat_link (taxonomy field):
    Category array(1) { [0]=> string(3) “249” } string(43) “http://www.mysite.com/news/”
    249
    249
    Category array(1) { [0]=> string(3) “346” } string(43) “http://www.mysite.com/news/”
    346
    346

    What I can’t understand is that even though both are outputting the correct category id’s. The taxonomy addon then doesn’t seem to be able to pass that to the link.

  • Umm, I think I may have fixed this but not entirely sure how. Will report back later after I’ve cleared my head!

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘get_category_link from taxonomy addon’ is closed to new replies.