Come ottenere l'URL della categoria da get_the_category?
Il mio loop qui sotto mostra gli ultimi 4 post della stessa categoria del post attualmente visualizzato. Si trova all'interno di single.php.
Sto cercando di ottenere l'URL di quella stessa categoria così da poter creare un link verso category.php per visualizzare tutti i post di quella categoria. Pensavo che prendere lo slug della categoria potesse funzionare ma il mio codice qui sotto non produce alcun output:
<?php
global $post;
$categories = get_the_category();
foreach ($categories as $category) :
$exclude = get_the_ID();
$posts = get_posts('posts_per_page=4&category='. $category->term_id);
foreach($posts as $post) :
if( $exclude != get_the_ID() ) { ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="post c-1"> Link al post effettivo</a>
<?php } endforeach; ?>
<a href="<?php bloginfo('url'); ?>/categories/<?php echo $childcat->cat_slug; ?>" title="Vedi tutti" class="btn border"><i class="i-right-double-arrow"></i> Vedi tutti <?php echo $childcat->cat_slug; ?></a>
<?php endforeach; wp_reset_postdata(); ?>
Utilizzo:
get_category_link( $category_id );
Vedi:
https://codex.wordpress.org/Function_Reference/get_category_link
Nel tuo caso specifico:
<?php
global $post;
$categories = get_the_category();
foreach ($categories as $category) :
$exclude = get_the_ID();
$posts = get_posts('posts_per_page=4&category='. $category->term_id);
foreach($posts as $post) :
if( $exclude != get_the_ID() ) { ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="post c-1"> Link all'articolo</a>
<?php } endforeach; ?>
<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>" title="Vedi tutti" class="btn border"><i class="i-right-double-arrow"></i> Vedi tutti <?php echo $category->name; ?></a>
<?php endforeach; wp_reset_postdata(); ?>

UN CODICE SEMPLICE E PULITO
Sono un principiante :)
Ho modificato i codici forniti da Adam e rimosso le parti non necessarie per rispondere alla domanda iniziale.
Per me ha funzionato al 100%.
Provate anche voi.
Fatemi sapere se ha funzionato anche per voi :)
<?php $categories = get_the_category();
foreach ($categories as $category) :
endforeach; ?>
<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>">
LINK ALLA CATEGORIA DEL POST CORRENTE >>
</a>
OPPURE
<?php $categories = get_the_category();
foreach ($categories as $category) : ?>
La categoria è:
<a href="<?php echo esc_url( get_category_link( $category->term_id ) ); ?>">
<?php echo $category->name; ?>
</a>
<?php endforeach; ?>
COMBINATE ALTRI ELEMENTI COME PREFERITE. SPERIMENTATE AMICI.

Non capisco il foreach vuoto. Come funziona? Seleziona semplicemente l'ultima voce in $categories come $category, quindi mostri solo il link per l'ultima categoria?

Sembra che foreach sia obbligatorio. Senza di esso, il codice non funzionerà correttamente. Puoi usare qualsiasi cosa come termine. Puoi decidere di cambiare in $category = get_the_category(); foreach ($category as $category) : Spero che questo aiuti. Non dimenticare, sono un principiante. Sto solo sperimentando con queste cose. Se hai notato, ho rimosso una riga di codice irrilevante. Divertiti.
