PHP over trim! Text disappearing! But why? - php

I just want to display the categories for my blog posts, but to SOME of the categories (and especially if they stand alone, the last bit get's trimmed away ~ "Music" becomes "Mu", and "Adventure" becomes "Adventur" ... any help? Please!
// Category boxes :P
function showcatz() {
global $post;
echo '<div class="categz_wrapper"><div class="categz">';
// get the category IDs assigned to post
$categories = wp_get_post_categories( $post->ID, array( 'fields' => 'ids' ) );
// separator between links
$separator = '</div><div class="categz"> ';
if ( $categories ) {
// List categories
$cat_ids = implode( ',' , $categories );
// Remove ONE category from the list
$kill = array("411,", "411");
$killit = str_replace($kill, "", $cat_ids);
$cats = wp_list_categories( 'title_li=&style=none&echo=0&include=' . $killit);
$cats = rtrim( trim( str_replace( '<br />', $separator, $cats ) ), $separator );
// Only show categories if there is any
if ( $killit ) { echo $cats; }
}
echo '</div></div>';
}

your passing a parameter to rtrim called $separator which has the value </div><div class="categz"> so when the following statement is executed it will remove the following chars from your string. div<>clastegz
rtrim( str_replace( '<br />', $separator, $cats ) ), $separator );
Solution, remove the second parameter to rtrim

Related

Display a ACF Category Taxonomy Field With a List of all Categories

I added a Text field to the Category taxonomy using ACF so that I can add a font awesome icon code to be displayed in a list of all categories.
The end result I am looking for is a list of all categories names linked to their respective category pages (if they have posts or not) with the icon from the custom field before each category name.
Reviewing the ACF documents and looking around for a solution I have only been able to display one icon at a time or just create errors.
Is there a more straight forward way I can loop through all of the categories and include the name, link and icon from my ACF custom field in the way I would do so for posts that have a custom field added on to it?
Here is the code I have so far but it only shows one at a time even though I am trying to use a foreach to loop through them all:
<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
foreach( $categories as $category ) {
$terms = get_the_terms( get_the_ID(), 'category');
if($terms) {
$term = array_pop($terms);
$icon = get_field('cat_icon', $term );
}
echo $icon;
$output .= '' . esc_html( $category->name ) . '' . $separator;
}
echo trim( $output, $separator ); ?>
Any help is appreciated, thank you!
As requested here is a screenshot, you can see it is displaying the icon from my custom field but only one category out of the 3 I currently have added.
You can use get_terms. try the below code.
<?php
$categories = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => false
) );
$separator = ' ';
$output = '';
foreach( $categories as $category ) {
if($category) {
$icon = get_field('cat_icon', $category );
}
echo $icon;
$output .= '' . esc_html( $category->name ) . '' . $separator;
}
echo trim( $output, $separator );
?>

Wordpress taxonomy last child separated by "en" instead of comma

I'm trying to create a shortcode that outputs my custom taxonomies, separated by a comma, but i want the last comma to be "en" instead of a comma. So like this:
taxonomy, taxonomy, taxonomy en taxonomy
So far i have this:
// Assortiment shortcode
function verlichting_type( ){
$terms = get_the_terms( $post->ID, 'verlichting_type' );
foreach($terms as $term) {
$entry_terms .= $term->name . ', ';
}
$entry_terms = rtrim( $entry_terms, ', ' );
return '<span class="verlichting__type"> ' . $entry_terms . ' </span>';
}
add_shortcode( 'verlichting_type', 'verlichting_type' );
WordPress already have custom printf function with localize the output
So if your site language is Francais means
wp_sprintf_l( '%l', ['Hello', 'world', 'never', 'give', 'up'] )
The above code will output
Hello, world, never, give, et up
For Espanol:
Hello, world, never, give y up
As you noted based on the language the last comma will be added/removed
As I dont have an example of the $terms or $entry_terms variable I had to make up some dummy data, but I think you should be able to extract my example and place it into your code.
I made use of the ternary operator (https://www.php.net/manual/en/language.operators.comparison.php) to determine whether or not the final comma should be ',' or 'en':
<?php
function verlichting_type() {
$entry_terms = "";
$terms = [
(object)['name' => 'taxonomy'],
(object)['name' => 'taxonomy'],
(object)['name' => 'taxonomy'],
(object)['name' => 'taxonomy']
];
echo '<span class="verlichting__type">';
foreach ( $terms as $index => $term) {
$enIndex = sizeof($terms) - 2;
$end = (isset($terms[$enIndex]) && $index == $enIndex ? ' en ' : ', ');
$entry_terms .= $term->name . $end;
}
$entry_terms = rtrim( $entry_terms, ', ' );
return $entry_terms . '</span>';
}
This outputs:
<span class="verlichting__type">taxonomy, taxonomy, taxonomy en taxonomy</span>
This should work with any array length, e.g. if $terms only has 2 elements:
<span class="verlichting__type">taxonomy en taxonomy</span>
Or 1 element:
<span class="verlichting__type">taxonomy</span>

Comma separate custom taxonomies (no links)

Here's just a simple little question I can't seem to figure out. I have one custom taxonomy which has multiple options. But I want to show the taxonomies without links. So I use this code:
<li>
<?php
$terms_as_text = get_the_term_list($post->ID, 'opties');
if (!empty($terms_as_text)) echo '', strip_tags($terms_as_text) , '';
?>
</li>
This display's only the selected custom taxonomies of opties. Because this taxonomy has multiple options I would like to comma separate them. But it won't let me.
Normally you would use:
<?php
echo get_the_term_list( $post->ID, 'opties', '<ul><li>', '</li><li>', '</li></ul>' ); ?>
The first part = before.
The middle part = separate.
The last part = after.
But this makes links of the custom taxonomy terms, and I don't want that to happen.
But because of the strip_tags($terms_as_text) I can't comma separate them.
How can I get them to separate with a comma?
You can try the following:
global $post;
$opties = wp_get_post_terms($post->ID, 'opties', array("fields" => "names"));
if (count($opties) > 0)
{
echo implode(', ', $opties);
}
try this
$list = get_the_term_list( $post->ID, 'opties');
$terms ="";
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
if(preg_match_all("/$regexp/siU", $list, $matches, PREG_SET_ORDER))
{
foreach($matches as $match)
{
$terms .= $match[3].","; // Get Text Only //nolinks
}
}
echo rtrim($terms,","); // display and remove extra ,
Try this:
if( is_singular( 'custom_post_type' ) ) {
get_the_term_list( get_the_ID(), 'custom_post_type_name', '', ', ' );
}
elseif( is_singular( 'post' ) ) {
get_the_category_list( ', ', '', get_the_ID() ) . '';
}
The above will return categories or custom post taxonomies with comma separated and a link to the response category or taxonomy.
The simple way to get the category or term without link is to use strip_tags() function as follow:
<?php echo strip_tags(get_the_term_list( get_the_ID(), 'category_name' ));?>

add a filter to add a class to tag link in wordpress

I want to add a filter to revise the link generated by get_the_tag_list in WP. It calls up get_the_term_list
function get_the_term_list( $id, $taxonomy, $before = '', $sep = '', $after = '' ) {
$terms = get_the_terms( $id, $taxonomy );
if ( is_wp_error( $terms ) )
return $terms;
if ( empty( $terms ) )
return false;
$links = array();
foreach ( $terms as $term ) {
$link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $link ) ) {
return $link;
}
$links[] = '' . $term->name . '';
}
I want to add class="tag" but i'm not sure how to write a filter for my functions.php file to target only the $links[] bit of that function. Could I just exclude the old link set and add in my modified one somehow?
I was thinking to add something like this, but I have it wrong somehow:
add_filter('get_the_term_list','replace_content');
function replace_content($links[])
{
$links[] = str_replace('<a href="', '<a class="tag" href="', $links[]);
return $links[];
}
You made a couple of mistakes. First add filter on get_the_term_list won't work, because it's not a filter. If you look in the code of get_the_term_list you'll see a line like this (depending on your WP version)
$term_links = apply_filters( "term_links-$taxonomy", $term_links );
So you can add a filter on term_links-$taxonomy in your case the taxonomy is tag.
The second mistake you made is the str_replace in combination with an array. If you want to use an array you don't need to add the [] after the variable. This is only for assigning the part after the = to the next item of an array. In this case you do a str_replace on the entire array so you should use $links instead of $links[] both in the assigning and in the str_replace otherwise you would add a new array (with the string replacement) after all the links of your current array.
add_filter( "term_links-post_tag", 'add_tag_class');
function add_tag_class($links) {
return str_replace('<a href="', '<a class="tag" href="', $links);
}

WordPress get_terms not outputting URL

I've got the following PHP code that I'm using to output a list of all custom taxonomy values, then group them into alphabetical order by first letter. This is working fine, except that the URL isn't being outputted. Anyone able to help?
<?php
$list = '';
$tags = get_terms( 'film-categories' );
$groups = array();
if( $tags && is_array( $tags ) ) {
foreach( $tags as $tag ) {
$first_letter = strtoupper( $tag->name[0] );
$groups[ $first_letter ][] = $tag;
}
if( !empty( $groups ) ) {
foreach( $groups as $letter => $tags ) {
$list .= '<div class="cat-group"><h3>' . apply_filters( 'the_title', $letter ) . '</h3>';
$list .= '<ul>';
foreach( $tags as $tag ) {
$url = esc_attr( get_tag_link( $tag->term_id ) );
$name = apply_filters( 'the_title', $tag->name );
$list .= '<li>' . $name . '</li>';
}
$list .= '</ul></div>';
}
}
} else $list .= '<p>Sorry, but no tags were found</p>';
echo $list;
?>
I'm afraid you've confused.
According to your second line - you're fetching terms of custom tax and not tags.
$tags = get_terms( 'film-categories' );
Therefore , any function related to tags won't work correctly.
In order to get the url of the term use the get_term_link() function.
Just replace the current line with:
$url = esc_attr( get_term_link( $tag ) );
Should work.

Categories