0

For several days I have been trying to create a link for the categories, basically they are displayed but are not clickable to access the categories page. Do you have a solution or a function to put in function.php

enter image description here

Thanks in advance, that would be great

2 Answers 2

0

You can use this for single category name with link:

$category = get_the_category(); 
echo '<a href="'.get_category_link($category[0]->cat_ID).'"><img src="'.$category[0]->cat_name.'" alt="'.$category[0]->cat_name.'" /></a>';

Or for the loop you can try this:

foreach(get_the_category() as $category){
    echo '<a href="'.get_category_link($category->cat_ID).'"><img src="'.$category->cat_name.'" alt="'.$category->cat_name.'" /></a>';
}
2
  • hi, I tried but it doesn't work. add_filter( 'post_link', 'filter_permalink_categories', 10, 3 ); function filter_permalink_categories( $permalink, $post, $catname) { $category = get_the_category(); echo '<a href="'.get_category_link($category[0]->cat_ID).'"><div>'.$category->cat_name[0].'"</div></a>'; return $permalink; } I think the function is not good. Commented May 22 at 21:04
  • Hi, I think the function is not good. it displays the name of the category but the link is not correct. I would have liked the function to loop current articles and for the badge to retrieve the link. It breaks the whole site for me by displaying the list of categories and the href link is not written in full. I don't want this to display relative to the image and I already have the image showing, all my Design is broken. Thank you.I can't take it anymore 😩 Commented May 24 at 9:26
0

If you're working with term objects or ids, then you can retrieve the category page url with get_category_link(). It is a handy wrapper for get_term_link() as it ensures the return value is always a string.

If you're dealing with the Loop on a more general level, then the_category() is a valid option as it prints a HTML list of post's categories with links.

Basic example.

<?php

// are there any posts to loop through?
if ( have_posts() ) {
    // The Loop
    while ( have_posts() ) {
        // Setup next post for current loop iteration
        the_post();
        ?>
        <article>
            <!-- post featured image -->
            <?php the_post_thumbnail(); ?>

            <h2>
                <!-- link to the post -->
                <a href="<?php the_permalink(); ?>">
                    <!-- post title -->
                    <?php the_title(); ?>
                </a>
            </h2>

            <!-- Option 1: all post categories with links -->
            <?php the_category(); ?>

            <!-- Option 2: first post category -->
            <?php $post_categories = get_the_category(); ?>
            <!-- link to category archive -->
            <a href="<?php echo esc_url( get_category_link( $post_categories[0] ) ); ?>">
                <!-- category title -->
                <?php echo esc_html( $post_categories[0]->name ); ?>
            </a>
        </article>
        <?php
    } 
}
2
  • Hello Antti, Thank you for your feedback 😊. But I tried several things but nothing works. I want to understand in order to learn, do you have the loop to do so that I can analyze it? Here are my latest attempts: function filter_post_link( $permalink, $post, $leavename ) { return $permalink; }; add_filter( 'post_link', 'filter_post_link', 10, 3 ); Commented May 22 at 9:53
  • @gestionlydie-tv I added a basic example to my answer. This should get you started. I would highly recommend you to take a look at the Loop documentation I've linked above to familiarize yourself with it. The Loop is one of the basic concepts of WordPress. Commented May 22 at 19:22

Your Answer

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

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