0

I have a function which is searching through my posts to find a certain category:

$thisCatsPosts = $allPosts->findby('category', 'category-one');

That works fine and returns exactly what I want when I give it a string like that for it's "needle".

However, I want to use a previously declared variable in place of it. Like thus:

$thisCat = $page->title();

$thisCatsPosts = $allPosts->findby('category', $thisCat);

However that does not work. I have also tried putting the variable in quotes.

Some specific notes are that I am using the Kirby CMS so the functions I'm using are from it's docs. I'm a complete PHP novice. From my understanding though, this is a PHP question and not a Kirby related one.

Many thanks for any help.

EDIT: I have checked that $thisCat prints "category-one". I have also checked that the function works when "category-one" is inputted as a string literal.

13
  • 1
    There's no reason why that shouldn't work - have you checked that you're getting the right value in $thisCat? If you're after a category, are you sure you should be using a function that looks like it returns a page title? Commented May 24, 2013 at 16:43
  • 1
    $page->title(), according to the doc, already returns a string. There should be no reason for your code to fail unless findby is failing. Commented May 24, 2013 at 16:44
  • 1
    @sanjaypoyzer - can I ask you to add var_dump($thisCat); and edit the output into your question? Commented May 24, 2013 at 16:57
  • 1
    @sanjaypoyzer - Please add all of it; but that certainly sounds like $page->title() isn't just returning a string. Commented May 24, 2013 at 17:06
  • 1
    (if it's anything other than string(12) "category-one", that function doesn't return what you're expecting) Commented May 24, 2013 at 17:07

2 Answers 2

1

It looks like

$thisCat = $page->title();

Isn't returning a string - it's passing back an object that contains all the details of the page. You should be able to do what you want with:

$thisCatsPosts = $allPosts->findby('category', $thisCat->value);
Sign up to request clarification or add additional context in comments.

Comments

0

Try debugging? For instance, use

print_r($page->title());
print_r($thisCat);

to inspect the values of those variables. Make sure they contain valid data that your findby() method recognizes. If they don't print to the screen you may need to set up some logging calls to print the values to a log, where you can view them.

5 Comments

Yep, checked the results of those. $thisCat prints "category-one", which as string literal makes the function work but as a variable within it doesn't seem to call it.
Does it return an error, or just no result? maybe try trim($thisCat) or some other gambit to exclude hidden characters.
My guess is that the string $thisCat just doesn't match the category field for some reason - maybe an invisible character, character encoding, a typo or something like that
$thisCat prints the category field just fine though. Exactly the same, even case sensitive, to the test category I've put in as a string.
obviously if it was exactly the same, it would have exactly the same result :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.