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' ));?>
Related
I was working on adding the product categories to under the shop image on my site. I have it working using the code below, but the categories do not have spaces between them. I am a bit of a novice here so wondered if someone can help me where I could add a space of comma between each category listing that would be great! Thanks in advance.
add_action( 'woocommerce_after_shop_loop_item', 'after_shop_loop_item_title', 1 );
function after_shop_loop_item_title() {
global $post;
$terms = get_the_terms( $post->ID, 'videocategories' );
$text = "<h3>Category: ";
foreach ($terms as $term) {
$text .= $term->name;
}
$text .= "</h3>";
echo $text;
}
You can use the PHP implode() function. This will create a string from an array and separate them with a separator of your choosing.
In the example below I first created an array of the categories and then used the implode() function in combination with the printf() function to print a comma separated list enclosed by h3 tags:
add_action( 'woocommerce_after_shop_loop_item', 'after_shop_loop_item_title', 10 );
function after_shop_loop_item_title() {
global $post;
$terms = get_the_terms( $post->ID, 'videocategories' );
$product_cats = array();
if ( !empty( $terms ) ) {
// Get an array of the categories
foreach ( $terms as $key => $term ) {
$product_cats[] = sprintf( '%s', get_term_link( $term->slug, 'videocategories' ), $term->name );
}
// Convert product category array into comma separated string
printf( '<h3>Category: %s</h3>', implode( ', ', $product_cats ) );
}
}
I have added project-types to my website portfolio https://littleseabear.com/portfolio but it lists all possible tags, and not the tags directly linked to the portfolio.
For example, Cast should only be film, screenplay and work in progress, while When the Boys Come Home should be Radio and Produced.
Here is my code:
$taxonomy = 'jetpack-portfolio-type' ;
$tax_terms = get_terms( 'jetpack-portfolio-type' );
if( is_archive() || is_home() || is_front_page() ){
?>
<div class="post-wrapper">
<?php
infinity_news_post_thumbnail();
}
?>
<div class="post-thumbnail">
<?php
foreach ( $tax_terms as $tax_term ) {
echo '<a class="filter" href="/project-type/'. $tax_term->slug.'">' . $tax_term->name .'</a>, ';
}
?>
<div class="article-details <?php if( is_single() ){ echo 'single-article-details'; } ?>">
Also... is there a way to get rid of the commas? Thanks
get_terms retrieves all available tags, not only the ones associated to the current post. Instead, modify the get_terms line at the top like so:
$taxonomy = 'jetpack-portfolio-type' ;
$tax_terms = get_the_terms( get_the_ID(), $taxonomy );
This will collect terms of the $taxonomy associated to the current post.
Edit:
I missed the question about the commas. To totally get rid of those, you can just remove them from the output:
foreach ( $tax_terms as $tax_term ) {
echo '<a class="filter" href="/project-type/'. $tax_term->slug.'">' . $tax_term->name .'</a> ';
}
I removed the , right behind the closing </a>, so it will now only be separated by whitespaces. If you want to have a special separator only between items, you can achieve this like so:
$separator = ''; // initialize empty
foreach ( $tax_terms as $tax_term ) {
echo $separator.'<a class="filter" href="/project-type/'. $tax_term->slug.'">' . $tax_term->name .'</a>';
if (empty($separator)) {
// Choose a custom separator HERE:
$separator = ', ';
}
}
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);
}
I am using the following to list the terms within my custom taxonomy
<?php $application_terms = wp_get_object_terms($post->ID, 'application');
if(!empty($application_terms)){
if(!is_wp_error( $application_terms )){
foreach($application_terms as $application){
echo $application->slug;
}
}
}
?>
The code works fine and it displays the terms however, if a post type has two "application" terms assigned to it, the two terms appear without space in betweeen them e.g ig apple and banana it appears applebanana. How do you change the code so that there is space in between the terms?
Thanks
Create an array of slugs and implode it with a blank space
$application_terms = wp_get_object_terms($post->ID, 'application');
if ( $application_terms && !is_wp_error( $application_terms ) {
$slugs = wp_list_pluck( $application_terms, 'slug' );
$string = implode( ' ', $slugs );
echo $string;
}
It should work in the way, you want it to be
<?php $application_terms = wp_get_object_terms($post->ID, 'application');
if(!empty($application_terms)){
if(!is_wp_error( $application_terms )){
$i = 0;
foreach($application_terms as $application){
if($i>0)
echo ', ';
echo $application->slug;
$i++;
}
}
}
?>
so if a post have two category assigned, it will display like apple, banana
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