php - Get the Category Name in WordPress - efficient or "bad" code? -


This is a question that is more about code best practices:

  $ cat_name = get_the_category () [0] - & gt; Name;   

Instead of doing so with some wars, in some steps:

  $ cat = get_the_category (); $ Cat_name = $ cat [0] - & gt; Name;   

Is this considered bad behavior? Or is it problematic in any way? If so, why? Then, my question is about the code, not the result.

I can say that it depends on you that you have php set to display a notice , And if you care about the undefined offset notice, here's the problem (IMO), see the beginning of the function:

  function get_the_category ($ id = false) {$ categories = get_the_terms ($ id , 'category'); If (! $ Ranges || is_wp_error ($ ranges)) $ categories = array ();   

You can see that if there are no categories or if there is an error, "the law" is empty. Perform a quick test ...

  & lt ;? Php $ categories = array (); Resonate $ ranges [0] - & gt; Name;   

get us

  notice: undefined offset: notice on line 5 in test.php: trying to get non-object property in test.php If the ($ cat) is $ {cat_name = $ cat [0] - & gt; Name; }    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -