PHP echo variable within function arguments
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 get开发者_StackOverflow社区s 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.
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') ?>
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.
精彩评论