i'd like to create a shortcode for wordpress to display the category name and i found this code :
function categories_list_func( $atts ){
$categories = get_the_category();
if($categories) {
foreach($categories as $category) {
$output .= '<li class="cat-' . $category->cat_ID . '">'.$category->cat_name.'</li>';
}
$second_output = trim($output);
}
$return_string = '<h4>'.__( "Categories :", "my_site").'</h4><div class="overflow"><ul class="post-categories">' . $second_output . '</ul></div>';
return $return_string;
} // END Categories
add_shortcode( 'categories-list', 'categories_list_func' );
But i know nothing in PHP and i'd like to remove everything (the link and "Catégorie :" in h4) except the category name.
I managed to do this with CSS but i'd like a clean php code if possible.
Could you help me please ?
Thanks.
Nicolas.
this is the code you want with link to categories :
<?php
function categories_list_func( $atts ){
$categories = get_the_category();
if($categories) {
foreach($categories as $category) {
$output .= '<li class="cat-' . $category->cat_ID . '">'.$category->cat_name.'</li>';
}
$second_output = trim($output);
}
$return_string = '<ul>' . $second_output . '</ul>';
return $return_string;
} // END Categories
add_shortcode( 'categories-list', 'categories_list_func' );
?>
and this is only the names without any listing and any links
function categories_list_func( $atts ){
$categories = get_the_category();
if($categories) {
foreach($categories as $category) {
$output .= '<li>'.$category->cat_name.'</li>';
}
$second_output = trim($output);
}
$return_string = '<ul>' . $second_output . '</ul>';
return $return_string;
} // END Categories
add_shortcode( 'categories-list', 'categories_list_func' );
or if you want it in another way dont hesitate to tell me :)
Related
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 want two differents excerpts on my homepage, one for the posts inside the loop, the other one for the latest post published, outside the loop.
I managed to add the excerpt on the latest post outside of the loop like this
<?php $postslist = get_posts('numberposts=1&order=DESC&orderby=post_date');
foreach ($postslist as $post) :
setup_postdata($post); ?>
<?php wp_latest_excerpt('wp_latest', 'excerpt_more_latest'); ?>
<?php endforeach; ?>
While the excerpt for the posts inside the loop is
<?php wp_excerpt('wp_index'); ?>
For some reasons I am able to set the excerpt length differently for both of my excerpts, but the "more" stays the same. (both "more" have the class .view-article)
I thought I could create another excerpt with a different "more" function, but it does not work, here are my 2 different excerpts and more functions.
For the length
function wp_index($length)
{
return 21;
}
function wp_latest($length)
{
return 18;
}
For the "more"
function view_article($more)
{
return '... </br><a class="view-article" href="' . get_permalink($post->ID) . '">' . __('Continue Reading', '') . '</a>';
}
function latest_view_article($more)
{
return '... </br><a class="view-latest-article" href="' . get_permalink($post->ID) . '">' . __('Continue Reading', '') . '</a>';
}
add_filter('excerpt_more_latest', 'latest_view_article');
add_filter('excerpt_more', 'view_article');
And finally the excerpts
function wp_latest_excerpt($length_callback = '', $more_callback = '')
{
if (function_exists($length_callback)) {
add_filter('excerpt_length', $length_callback);
}
if (function_exists($more_callback)) {
add_filter('excerpt_more_latest', $more_callback);
}
$output = get_the_excerpt();
$output = apply_filters('wptexturize', $output);
$output = apply_filters('convert_chars', $output);
$output = '<p>' . $output . '</p>';
echo $output;
}
function wp_excerpt($length_callback = '', $more_callback = '')
{
if (function_exists($length_callback)) {
add_filter('excerpt_length', $length_callback);
}
if (function_exists($more_callback)) {
add_filter('excerpt_more', $more_callback);
}
$output = get_the_excerpt();
$output = apply_filters('wptexturize', $output);
$output = apply_filters('convert_chars', $output);
$output = '<p>' . $output . '</p>';
echo $output;
}
Is this a wrong way to achieve this or did I make some mistakes in my code ?
function Service_shortcode($atts , $content = null){
extract(shortcode_atts(array(
'title'=>'',
),$atts
));
$q = new WP_Query(
array('posts_per_page'=>3, 'post_type'=>'services')
);
$list = '
<h1>'.$title.'</h1>
<ul>
';
while ($q->have_posts()) : $q->the_post();
$list.='
<li>
<h3>'.get_the_title ().'</h3>
<h6>'.the_category(',').'</h6>
<p>'.get_the_content().'</p>
</li>
';
endwhile;
$list.='</ul>';
wp_reset_query();
return $list;
}
add_shortcode('service','Service_shortcode');
when use it the_category(',') in theme it's show the category name but when use it in shortcode not working. which code use to show category name by shortcode.
try this code :
function categories_list_func( $atts ){
$categories = get_the_category();
if($categories) {
foreach($categories as $category) {
$output .= '<li class="cat-' . $category->cat_ID . '">'.$category->cat_name.'</li>';
}
$second_output = trim($output);
}
$return_string = '<h4>'.__( "Categories :", "my_site").'</h4><div class="overflow"><ul class="post-categories">' . $second_output . '</ul></div>';
return $return_string;
} // END Categories
add_shortcode( 'categories-list', 'categories_list_func' );
I'm trying to output all the tags associated with a custom post type via shortcode and it only appears to be bringing in 1 tag inside the $output.
outside the $output the code is fine.
code is:
function display_stores() {
$args = array( 'post_type' => 'stores', 'posts_per_page' => 5 );
$success = new WP_Query( $args );
$output = '';
while( $success->have_posts() ) {
$success->the_post();
$tags = get_the_tags($post_ID);
foreach($tags as $tag) {
return '<li>'. $tag->name . '</li>' ;
}
$output .= sprintf( "<div class='story-content left'>" );
$output .= sprintf( "<h2>%s</h2>", get_the_title() );
$output .= sprintf( '%s</div>', get_the_content() );
$output .= sprintf( "Button");
$output .= sprintf( "<div class='story-tags right'>" );
$output .= sprintf( "<h4>Areas</h4><ul class='ul-arrows'>" );
$output .= sprintf( $tags );
$output .= sprintf( "</ul></div><hr>" );
}
wp_reset_query();
return $output;
}
add_shortcode( 'display_stores', 'display_stores' );
foreach($tags as $tag) {
return '<li>'. $tag->name . '</li>' ;
}
The first time this is ran it will exit the function and return the li. I imagine you meant to add it to output.
$tagHTML = '';
foreach($tags as $tag) {
$tagHTML .= '<li>'. $tag->name . '</li>' ;
}
//Later
$output .= $tagHTML;
I am using the get_categories() function to manually create myself a nav menu. I have a custom taxonomy I'm using called Category and I'm trying to return the link for it for my tags in the menu using the get_category_link() function.
foreach ($categories as $category) {
if ($category->parent == 0) { //Check to see it is a parent
$output .= '<li>';
$output .= '' . $category->name . ''; //display parent taxonomy category
}
}
But it always returns <a href="">. I can echo out the $category->cat_ID successfully so I know it is passing the ID into the function but I don't know why it's returning blank.
Am I missing something? Is it because these are custom taxonomies? They have slugs.
You need something like this for custom taxonomies:
$tax = 'cars';
$cats = get_terms( $tax, '' );
if ($cats) {
foreach($cats as $cat) {
$output .= "<li>";
$output .= '<a href="' . esc_attr(get_term_link($cat, $tax)) . '" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>' . $cat->name.'</a>';
$output .= "</li>";
}
}
Although you can easily add to the top of the script to get an array of all taxonomies to feed in if you wanted.