Categories as list with no links
-
Hello everyone!
For a website I’m developping I need to display in a loop the categories of the articles but I need them to not be links. Is it possible?
When I use the_categry(), it creates a list with links inside the<li>
.
Bear in mind that I’m very new to php and wordpress, I might need explanations with code examples to understand…
Thanks! ??-
This topic was modified 7 years, 10 months ago by
soph87.
-
This topic was modified 7 years, 10 months ago by
-
Hey,
URL to your WordPress page with the category list would be really helpful. Otherwise, it is hard to advise anything.
I already tried to use
get_the_category()
, but all I managed to do is store the array in a variable, I don’t know how to then display it in a list.Take that variable and use
foreach
to loop through all categories returned. Echo out the term name plus anything else you need for styling and formatting via CSS.foreach ( $your_cats_var as $cat ) { echo "{$cat->name}<br>"; }
Thank you! It works ?? Now I’m trying to create a shortcode that my clients can use in a post to link to another post and I have some trouble (still with the categories), maybe you can help for that too. Here’s what I have so far (in my functions.php) :
function linkarticles($atts) { $thepostid = intval($atts[id]); $output = ''; $post = get_post($thepostid); function categories_li(){ $cats = get_the_category($post->ID); foreach ( $cats as $cat ) { echo "<ul class='post-categories'><li>{$cat->name}</li></ul>"; }; } if ($post) : $output .= '<article class="articles_miniatures"> <div class="vignettes_articles"> <a class="filtres filtre_bleu" href="' . get_the_permalink($post->ID) . '"> </a> <a class="img_intro" href="' . get_the_permalink($post->ID) . '"> <div class="img_wrap">' . get_the_post_thumbnail($post->ID, 'post-thumbnail', ['class' => 'img-responsive']) . '</div></a> <a class="texte_vignettes" href="'. get_the_permalink($post->ID) . '"> <p class="lire_article">lire l\'article</p> <p class="plus">+</p> </a>' . categories_li() . '</div><div class="centrage"> <h2 class="titre_bleu">' . get_the_title($post->ID) . '</h2> </div>'. get_wp_term_image() .' </article>'; else : // failed, output nothing endif; return $output; } add_shortcode('include_post', 'linkarticles');
As a result I don’t have the categories for the post ID, but for the post I’m writting and it’s not placed at the right place in the html. Any advice ?
Sorry if I’m a pain, I only studied php for 5 days, still in the process of learning!!Not a pain at all. Everyone had to start out somewhere. 5 days? You’re doing very well!
If you haven’t already done so, I recommend coding with WP_DEBUG defined as
true
in wp-config.php. You’ll then be notified when there are any compiler issues with your code as soon as they are encountered. This can make debugging much easier, though the error messages are often as cryptic as some PHP syntax. You’ll eventually learn what the common ones really mean. Searching the ‘net using the main error message, without the specifics like line and file where it occurred, will often yield better explanations.There’s a few problems in your snippet that you may have been able to resolve yourself if WP_DEBUG had notified you. Or maybe you were notified but couldn’t decipher the message. That’s understandable. One thing though, WP_DEBUG defined as true on a public server is a slight security risk. Restore it to false if you will not be needing it active for any length of time. A day or two active is probably OK.
$thepostid = intval($atts['id']);
the id here needs to be quoted as shown. Array keys should always be quoted unless you are using a variable or constant to pass a value.The variable $post as used in categories_li() is undefined due to PHP variable scoping rules. Unless declared global within each context, any variable’s scope in PHP is local to the context in which it was assigned. In other words, the $post in categories_li() is not the same $post used in linkarticles(). It doesn’t matter if a function is local (within) to another function, it’s still a different function, so a different scope.
Thus, for categories_li() to be able to use $post assigned in linkarticles(), it must either be declared global in each context, or be passed. If you haven’t already noticed, you will learn many coders consider global variables to be abhorrent and have no place in proper code. There are some problems with globals, but they can be useful too. My own opinion is to avoid them where possible, try to limit their use, but use them if necessary.
In this case, $post could be passed as a parameter, there is no reason to use globals here. Example of passing a value to a function:
$post = get_post( 123 ); echo categories_li( $post ); // outputs '123' function categories_li( $fn_post ) { print_r( $fn_post ); return $fn_post->ID; }
I’m assuming get_wp_term_image() is a function you have declared on your site somewhere that returns HTML. Terms by WP default have no related images. Themes and plugins could certainly do so.
Having categories end up in the wrong place is a classic shortcode blunder that nearly everyone seems to make at first. You’re in good company! Any output from a shortcode must never be echoed out. This is how content ends up in the wrong place. Shortcode handlers execute before content is output, not when encountered in content. Thus echoes will end up in the wrong place.
All output needs to be collected in a single variable that is eventually returned by the handler. Other WP code then outputs the returned HTML at the appropriate location. You did well in the main handler function. The problem is the echo statement in categories_li(). This too should be collected into a variable that is then returned. Your main function then concatenates the returned string to the other HTML. Your main function is already trying to concatenate properly even though categories_li() is currently not returning anything.
FYI (Not needed here since you can revise categories_li()) if you should ever need to use code in a shortcode handler that echoes content, you can intercept that output into an output buffer, then concatenate the buffer content to your shortcode return.
One last thing. You should try to avoid very long lines of code, as in your
$output .= '<article class=" //...
Maybe you have line wrapping turned on in your editor so it’s not apparent how long it became. I recommend code editors not use line wrapping so this can be managed. It’s not wrong to have long lines, it’s just poor style, making it difficult for others to read your code.PHP ignores line feeds, so you can simply add your own to better format your code. PHP will consider it all one statement until a
;
is encountered. Since you are accumulating strings into $output, another way to keep lines down to a reasonable length is to repeatedly concatenate shorter segments, one statement at a time. A silly example:$output .= 'It demonstrates professional aesthetics to'; $output .= ' keep code lines down to a reasonable'; $output .= ' length in order for your code to'; $output .= ' be easily readable.';
Hi! Thank you for your extended explanation, it was very usefull! I managed to make it work (after a lot of head scratching though) ??
Thanks again!Always happy to help ??
Head scratching is necessity when learning. It is learning! It’ll get easier, but there will always be mysteries to solve ??
- The topic ‘Categories as list with no links’ is closed to new replies.