0

I am trying to get wordpress to pull an ID from a variable to use within a category query but for some reason it isn't working. It's probably something with the syntax could you just give me a helping hand.

Here is what I have...

$catPosts1 = new WP_Query('category=$cat1&offset=5&posts_per_page=3');

Basically what I want it to do is get the category ID from $cat1 (I have tested and it is entering a category id in the variable), offset the number of posts by 5 and display 3 posts linked with that category. At the moment the code is just displaying posts offset by 5.

Any ideas?

Mark

3 Answers 3

2

Now you're just sending $cat1 along as a string, the code should look like this.

$catPosts1 = new WP_Query('category='.$cat1.'&offset=5&posts_per_page=3');
Sign up to request clarification or add additional context in comments.

Comments

2

If it is showing, literally, "$cat1" in the output, you might need to switch from single quotes to double quotes to get the substitution.

That is to say, do this:

$catPosts1 = new WP_Query("category=$cat1&offset=5&posts_per_page=3");

...if you're trying to get the contents of the variable into the WP_Query call.

Comments

0

use double quotes instead of single.it is simple.

Comments

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.