Adrian
Adrian

Reputation: 2291

How to add dynamically number of posts in a query in wordpress

I have the following query in my slider

<?php query_posts( 'category_name=Uncategorized&posts_per_page=3' );

and instead of 3 i need to add dynamically the posts_per_page. For triggering an option for my functions.php theme i usually do

<?php $settings = get_option('mytheme_options'); echo $settings['postspage'];?>

I have tried

<?php 
 query_posts('category_name=Uncategorized&posts_per_page=$settings["postspage"]'); 
?>

and it doesn't do anything or echo any error

Upvotes: 5

Views: 373

Answers (2)

ggdx
ggdx

Reputation: 3064

$postsPage = $settings["postspage"];
query_posts('category_name=Uncategorized&posts_per_page='.$postsPage); 

Upvotes: 2

Dan Walker
Dan Walker

Reputation: 464

The reason is that you're using single quotes. If you use single quotes, PHP will not parse the string, and as such will not replace any variables it finds with their values, it is taken as a literal string.

You can use double quotes, and change the double quotes in the array square brackets to single quotes, or you can just use string concatenation which is the better option:

 query_posts('category_name=Uncategorized&posts_per_page='.$settings['postspage']); 

Use double quotes where you need the string parsing, and single where you do not. You do not need to parse inside the square brackets, so just use single quotes there too.

Upvotes: 1

Related Questions