How to dynamically change the number of posts per page in WordPress
<p>I would like to change the number of posts displayed on certain pages on my WordPress site. </p>
<p>I found the following code snippet here: </p>
<pre class="brush:php;toolbar:false;">$postsPage = $settings["postspage"];
query_posts('posts_per_page='.$postsPage);</pre>
<p>Call before
<code>if (have_posts()) while (have_posts()) the_post()</code></p>
<p>I'm very new to php, but what I'm trying is this</p>
<pre class="brush:php;toolbar:false;">...
if ( is_front_page()) {
$postsPage = $settings["postspage"] 1;
}elseif ( is_tag()) {
$postsPage = $settings["postspage"] - 2;
} else {
$postsPage = $settings["postspage"];
}
query_posts('posts_per_page='.$postsPage);
...</pre>
<p>But unfortunately it doesn't work. Instead, it only returns 1 post from the homepage instead of 1 of 5 set in the WP menu. I can hardcode the desired value like $postPage = 6; </code> for is_front_page and it works fine.
For some reason I can't add/subtract anything from the value given by <code>$settings["postspage"]</code>. It also works if I don't change the value (else-clause). What did I miss? </p>
<p>I've tried converting the value of <code>$settings["postspage"]</code> to an integer because I thought it might be returned as a string. I use: <code>$postsPage = (int)$settings["postspage"] 1;</code> or <code>$postsPage = intval($settings["postspage"]) 1;</ code>But without success. Since I'm not very familiar with wordpress functions and their variables, I was looking for the reason why I can't modify the number of posts.
I'd appreciate any tips! </p>