Home › Forums › Front-end Issues › Help Please: Links in the category of custom fields
I’m about to buy some add-ons that I like but if I do not get it to work the first part I can not use the plugins,
Hello I hope your help, What I want is to show the selected taxonomy category that shows link to the selected categories, How can you achieve that, I am using the following code.
<?php $empresa_terms = get_field( 'empresa' ); ?>
<?php if ( $empresa_terms ): ?>
<?php foreach ( $empresa_terms as $empresa_term ): ?>
<?php echo $empresa_term->name; ?>
<?php endforeach; ?>
<?php endif; ?>
The categories appear but in plain text, as I can do to bring up the link to category page each category with comma.
So I want the category:
Places: category1, category2, category3
Hi @wilfredo123
The use of get_term_link and passing in the term item should do the trick for you for getting the link.
You can try the code below… I setup a counter in the test code to be sure not to have a comma at the end of your list. It should display as shown in your image, but please test it for bugs.
Hope this helps! Let me know.
<?php
$empresa_terms = get_field( 'empresa' );
if ( $empresa_terms ) {
$total = count( $empresa_terms );
$count = 1;
echo 'Places: ';
foreach ( $empresa_terms as $empresa_term ) {
echo '<a href="' . get_term_link( $empresa_term ) . '">';
echo $empresa_term->name;
echo '</a>';
if ( $count < $total ) {
echo ', ';
}
$count++;
}
}
?>
Thanks Keith Lock for the excellent after it has worked correctly, Please could you tell me how to delete the places code since in the beginning I already have configured the said word and shows duplicate.
My Code
<ul class="modulejobs">
<li> <strong>Places:</strong> <?php
$empresa_terms = get_field( 'empresa' );
if ( $empresa_terms ) {
$total = count( $empresa_terms );
$count = 1;
echo 'Places: ';
foreach ( $empresa_terms as $empresa_term ) {
echo '<a href="' . get_term_link( $empresa_term ) . '">';
echo $empresa_term->name;
echo '</a>';
if ( $count < $total ) {
echo ', ';
}
$count++;
}
}
?></li></ul>
No problem..
You can safely remove this line:
echo 'Places: ';
Thank you for your huge answer, I have my last query, My idea is to do on the page post categories with links and I have solved them, now as would be categias with commas and without links.
Please if you could help me with the okay, this is the last step.
<ul class="modulejobs">
<li> <strong>Places:</strong> <?php
$empresa_terms = get_field( 'empresa' );
if ( $empresa_terms ) {
$total = count( $empresa_terms );
$count = 1;
foreach ( $empresa_terms as $empresa_term ) {
echo $empresa_term->name;
if ( $count < $total ) {
echo ', ';
}
$count++;
}
}
?></li></ul>
You’re welcome 🙂
That code you posted looks right… it should display the category names, separated by commas, without links.
Is it not working?
Todo funciona gracias por todo, Gracias por la ayuda, Saludos a distancia.
Everything works thanks for everything, Thanks for the help, Greetings from a distance.
The topic ‘Help Please: Links in the category of custom fields’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.