2

I'm currently using a wordpress function in order to display posts from a specific category. A simplified example is shown below:

<?php query('cat_name=cat1&posts=1') ?>

Essentially this gets 1 post from the category cat1. However I have a variable saved which gets the current category (this is on category pages):

<?php $thiscat = get_the_category(); ?>
Current Category: <?php echo $thiscat ?>

How can I now echo the variable $thiscat into the arguments of my query above so that the category name is filled in for me? This function is applied on different category pages so having it automatically passed to the arguments of my query saves a lot of hassle.

Thanks in advance for any help.

2 Answers 2

1

You only echo something when you want to output it to the browser, here we concatenate the query string with the variable:

<?php $thiscat = get_the_category(); ?>
<?php query('cat_name=' . $thiscat . '&posts=1') ?>
Sign up to request clarification or add additional context in comments.

1 Comment

Of course mines better because it concatenates the variable... improves readability ;-)
1

Not sure I understand the question, but it sounds like you want to use $thiscat in your query. This should do it:

<?php

$thiscat = get_the_category();
query("cat_name=$thiscat&posts=1")

?>

Note the double quotes, which are necessary. If you use single quotes, the variable will not get expanded.

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.