I’m working with an array. This array is used to query posts in WordPress. What I would like to do is change the value for the key ‘category_name’ based on the category of the page.
This is the function for getting the category of the current page
$CategoryName = single_cat_title(); I’ve tested it and it works fine.
My problem is that when i go to change the value it doesn’t work. I can get it to work fine with a string, but I’m not not sure how to do it using a variable.
This function single_cat_title(); will output the category name. I don’t know what you mean by assigning it versus displaying it. In any case I want this
$CategoryName = single_cat_title();
$blogposts['category_name'] = $CategoryName;
?>
to be this after the code runs
$CategoryName = single_cat_title();
$blogposts['category_name'] = 'css';
/*css would just be one example*/
?>
I did read it. What do you mean by assign it? Your response makes no sense to me. I’m aware of the function. I included it in my original post. I don’t think there is an issue here with the function. I think the problem is how to update the value in this function $array[‘key’] = ‘new value’;
But you don’t want to output it, you want to retrieve the title and store it in the variable $CategoryName, and as in the documentation linked to by @mittineague, you need to supply a second parameter (as well as the first parameter which I’ve already forgotten) with a value of “false”. If you omit that parameter, it defaults to a value of “true”, which means “display the title”. There’s some sample code in that link that shows explicitly how to retrieve the title, and it’s different to your code.
This is weird, though, because the documentation says opposite. Is it possible that when you say you tested it and it works, that what you’ve done is use that code to retrieve the title, then immediately echo it so you can see the value, but what’s actually happened is that the function you call has output it, and your echo() has echoed a blank string? That is, if you test it and use var_dump() instead of echo, does it show a value in the string?
OK then, the last bit of code you show is how the doc suggests it should be done, I can offer nothing more. The issue, as you say, is retrieving the value rather than setting it into the array - maybe you could edit the thread title to reflect that and someone who knows Wordpress might be able to help. Also, did you try
using var_dump() instead of echo, in case it tells you anything more? Also I see a note that suggests you should check for is_category() or is_tag() to decide how to retrieve some of this stuff, but I’ve never used Wordpress so that might not be relevant.
Well it will, all you’re doing there is dumping the type, length and content of the variable, not assigning it to anything. It was just to see if there was anything at all in there.
Is this the result of the var_dump($CatergoryName), or are you doing a var_dump($blogposts[‘category_name’]) here? Is $blogposts[] defined within the scope of the code you’re trying to set it from? I guess it must be, because it works if you hard-code it.