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.
Related
I have successfully managed to make a shortcode into my product description, to show all my $taxonomy and $attribute_label values in a nice table form. Unfortunately, it returns the $attribute_label values as a link. I want them to be displayed as text.
$html .= get_the_term_list( $product->get_id(), $taxonomy, '<tr><td>' . $attribute_label . '</td><td style="text-align:right">' , '</td><td>', '</td></tr>' );
I have tried to use wp_get_object_terms but it wont work for me , it returns "Array" in all my Columns in the <table> Form.
My full Code so far:
function so_39394127_attributes_shortcode( $atts ) {
global $product;
if( ! is_object( $product ) || ! $product->has_attributes() ){
return;
}
// parse the shortcode attributes
$args = shortcode_atts( array(
'attributes' => array_keys( $product->get_attributes() ), // by default show all attributes
), $atts );
// is pass an attributes param, turn into array
if( is_string( $args['attributes'] ) ){
$args['attributes'] = array_map( 'trim', explode( '|' , $args['attributes'] ) );
}
// start with a null string because shortcodes need to return not echo a value
$html = '';
if( ! empty( $args['attributes'] ) ){
foreach ( $args['attributes'] as $attribute ) {
// get the WC-standard attribute taxonomy name
$taxonomy = strpos( $attribute, 'pa_' ) === false ? wc_attribute_taxonomy_name( $attribute ) : $attribute;
if( taxonomy_is_product_attribute( $taxonomy ) ){
// Get the attribute label.
$attribute_label = wc_attribute_label( $taxonomy );
// Build the html string with the label followed by a list of terms.
// Updated for WC3.0 to use getters instead of directly accessing property.
$html .= wp_get_object_terms( $product->get_id(), $taxonomy, '<tr><td>' . $attribute_label . '</td><td style="text-align:right">' , '</td><td>', '</td></tr>' );
}
}
// if we have anything to display, wrap it in a <table> for proper markup
if( $html ){
$html = '<table class="product-attributes">' . $html . '</table>';
}
}
return $html;
}
add_shortcode( 'display_attributes', 'so_39394127_attributes_shortcode' );
I am new to PHP so any help is very appreciated!
function ts_add_text_short_descr($atts) {
global $product;
if (!is_object($product) || !$product->has_attributes()) {
return;
}
// parse the shortcode attributes
$args = shortcode_atts(array(
'attributes' => array_keys($product->get_attributes()), // by default show all attributes
), $atts);
// is pass an attributes param, turn into array
if (is_string($args['attributes'])) {
$args['attributes'] = array_map('trim', explode('|', $args['attributes']));
}
// start with a null string because shortcodes need to return not echo a value
$html = '';
if (!empty($args['attributes'])) {
foreach ($args['attributes'] as $attribute) {
// get the WC-standard attribute taxonomy name
$taxonomy = strpos($attribute, 'pa_') === false ? wc_attribute_taxonomy_name($attribute) : $attribute;
if (taxonomy_is_product_attribute($taxonomy)) {
// Get the attribute label.
$attribute_label = wc_attribute_label($taxonomy);
// Build the html string with the label followed by a list of terms.
// Updated for WC3.0 to use getters instead of directly accessing property.
$html .= "<tr><td>$attribute_label</td>";
$terms_list = wp_get_object_terms($product->get_id(), $taxonomy);
foreach ($terms_list as $term) {
$html .= '<td style="text-align:right">' . $term->name . '</td>';
}
$html .= '</tr>';
}
}
// if we have anything to display, wrap it in a <table> for proper markup
if ($html) {
$html = '<table class="product-attributes">' . $html . '</table>';
}
}
return $html;
}
This will print like
Tested with a sample attribute Color
I´m trying to display a custom taxonomy item description in the frontend on a single post.
Following situation:
Created a CPT for "Weine"
Added a taxonomy "winzer"
Added the following code to my functions.php to display the "winzer" taxonomy item with its description in the single post using a shortcode:
function wpb_catlist_desc() {
$string = '<div>';
$catlist = get_terms( 'winzer' );
if ( ! empty( $catlist ) ) {
foreach ( $catlist as $key => $item ) {
$string .= '<div>'. $item->name . '<br />';
$string .= '<em>'. $item->description . '</em></div>';
}
}
$string .= '</div>';
return $string;
}
add_shortcode('wpb_categories', 'wpb_catlist_desc');
The code is working well, but it’s displaying all the items I created in the “Winzer” Taxonomy.
I just want to display the Item which is related to the single post.
Any ideas on how to change the code to get this done?
Cheers!!!!
You could use get_the_terms() instead of using get_terms(). So you could do something like this:
add_shortcode('wpb_categories', 'wpb_catlist_desc');
$the_id_of_current_post = get_the_ID(); // get the id of the current post
// pass the id to your function
function wpb_catlist_desc($the_id_of_current_post)
{
$string = '<div>';
$$catlist = get_the_terms( $the_id_of_current_post, 'winzer' );
if ( ! empty( $catlist ) ) {
foreach ( $catlist as $cat ) {
$string .= '<div>'. $cat->name . '<br />';
$string .= '<em>'. $cat->description . '</em></div>';
}
}
$string .= '</div>';
return $string;
}
Or an alternative way would be something like this:
add_shortcode('wpb_categories', 'wpb_catlist_desc');
function wpb_catlist_desc()
{
global $post;
$the_id_of_current_post = $post->ID; // get the id of the current post
$string = '<div>';
$$catlist = get_the_terms( $the_id_of_current_post, 'winzer' );
if ( ! empty( $catlist ) ) {
foreach ( $catlist as $cat ) {
$string .= '<div>'. $cat->name . '<br />';
$string .= '<em>'. $cat->description . '</em></div>';
}
}
$string .= '</div>';
return $string;
}
Let me know if you were able to get this to work!
I've got the following code which is a part of the widget that outputs the terms of the taxonomy 'season'
The taxonomy terms are output with space and comma in between them, but it also adds a comma at the very end.
How can I get rid off the last comma?
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
global $post;
$tags = get_the_terms( $post->ID, 'season' );
if( $tags ) : ?>
<?php foreach( $tags as $tag ) :
$tag_link = esc_url( get_term_link( $tag ) );
$tag_output = '';
$tag_output .= '<a href="' . $tag_link . '" class="listing-tag">';
$tag_output .= '<span class="tag__text">' . $tag->name . '</span></a>';
$tag_output .=", ";
echo $tag_output;
endforeach; ?>
<?php endif;
echo $args['after_widget'];
}
I'been trying to use the rtrim($tag_output,', '); but I just can't figure out where to put this rtrim string, to make this working.
Where in the code should the rtrim($tag_output,', '); sit to make this work?
It might be easier to map your array to one containing the strings you want and then display it using implode(). For example
if ($tags) {
$tagOutput = array_map(function($tag) {
return sprintf(
'<span class="tag__text">%s</span>',
esc_url( get_term_link( $tag ) ),
$tag->name
);
}, $tags);
echo implode(', ', $tagOutput);
}
echo $args['after_widget'];
I have made some changes to a RSS feed in my Wordpress, and I'm using fetch_feed() to show data to another website.
Imagine there are 2 websites called #Wordpress1 and #Wordpress2.
This is the code i've added to #wordpress1's functions.php file
add_action('rss2_item', 'dw_add_data_to_rss');
function dw_add_data_to_rss(){
global $post;
if( $post->post_type == 'product' ) {
$product = new WC_Product( $post->ID );
$output = '';
$thumbnail_ID = get_post_thumbnail_id( $post->ID );
$thumbnail = wp_get_attachment_image_src($thumbnail_ID, 'thumbnail');
$output = '<post-thumbnail>';
$output .= '<url>'. $thumbnail[0] .'</url>';
$output .= '<width>'. $thumbnail[1] .'</width>';
$output .= '<height>'. $thumbnail[2] .'</height>';
$output .= '</post-thumbnail>';
$output .= '<price>' . number_format( $product->get_price() ) . ' ' . get_woocommerce_currency_symbol() . '</price>';
echo $output;
}
}
this code adds product price and thumbnail to Rss feed now we need to display these data on #Wordpress2 , but i don't know how to do it
$rss = fetch_feed( 'http://localhost/wp/feed/?post_type=product' );
if ( ! is_wp_error( $rss ) ) {
$maxitems = $rss->get_item_quantity( 10 );
$rss_items = $rss->get_items( 0, $maxitems );
}
foreach ( $rss_items as $item ) {
echo '<img src="{MY_IMAGE_FROM_RSS}"> <span class="price">{MY_PRICE_FROM_RSS}</span>';
}
what should i use instead of MY_IMAGE_FROM_RSS and MY_PRICE_FROM_RSS in above code
You should use the get_item_tags() function and use blank for the required namespace.
For MY_IMAGE_FROM_RSS use $item->get_item_tags('','post-thumbnail')[0]['child']['']['url'][0]['data'] and for MY_PRICE_FROM_RSS use $item->get_item_tags('','price')[0]['data']
I've been trying to display the subtitle for each children page of the current parent page. At the moment i've got it working so that it shows all the title's of the children pages.
Basically I want it to show the subtitle underneath each title of the children pages.
// Get childern
$children = ($post->post_parent) ? wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0') : wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
// Set subtitle
$subtitle = get_the_title($post->the_subtitle);
echo $children;
echo $subtitle;
Any help will be much appreciated.
Cheers
You have two options for this:
1. The OOP approach
Extend the Walker_page class to create a custom iterator to use with wp_list_pages().
Add this to your functions.php:
class Subtitle_walker extends Walker_page {
function start_el(&$output, $page, $depth, $args, $current_page) {
if ( $depth )
$indent = str_repeat("\t", $depth);
else
$indent = '';
extract($args, EXTR_SKIP);
$css_class = array('page_item', 'page-item-'.$page->ID);
if ( !empty($current_page) ) {
$_current_page = get_page( $current_page );
_get_post_ancestors($_current_page);
if ( isset($_current_page->ancestors) && in_array($page->ID, (array) $_current_page->ancestors) )
$css_class[] = 'current_page_ancestor';
if ( $page->ID == $current_page )
$css_class[] = 'current_page_item';
elseif ( $_current_page && $page->ID == $_current_page->post_parent )
$css_class[] = 'current_page_parent';
} elseif ( $page->ID == get_option('page_for_posts') ) {
$css_class[] = 'current_page_parent';
}
$css_class = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
//Added subtitle support
$output .= $indent . '<li class="' . $css_class . '">' . $link_before . apply_filters( 'the_title', $page->post_title, $page->ID ) . $link_after . get_the_post_thumbnail($page->ID, array(72,72)) .''.' <span class="subtitle">'.get_the_subtitle($page->ID,'','',0).'</span>';
if ( !empty($show_date) ) {
if ( 'modified' == $show_date )
$time = $page->post_modified;
else
$time = $page->post_date;
$output .= " " . mysql2date($date_format, $time);
}
}
}
And call it in your template file:
$subtitle_menu = new Subtitle_walker();
$args = array(
'title_li' => '',
'child_of' => ($post->post_parent) ? $post->post_parent : $post->ID,
'echo' => 0,
'walker' => $subtitle_menu
);
$children = wp_list_pages($args);
echo $children;
Note: the previous code is based on this tutorial, you can read it to get an in-depth explanation of what it's doing
2. Using custom WP_Query loop
You can use get_page_children to query the child pages and then loop through them and build/echo your custom list items:
//Set up the objects needed
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));
//Get children
$children = ($post->post_parent) ? get_page_children( $post->post_parent, $all_wp_pages ) : get_page_children( $post->ID, $all_wp_pages );
//Build custom items
foreach($children as $child){
echo '<li class="page-item">';
echo ''.get_the_title($child->ID).'<br/>';
echo '<span class="subtitle">'.get_the_subtitle($child->ID).'</span>';
echo '</li>';
}