add filter not working when cancatenating variables
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
I am getting a problem while applying a code. My code works fine when i use only one variable. e.g;
$posts_per_page = 'posts_per_page=3';
but it doesn't work if i use concatenation.e.g
$posts = "posts_per_page=";
$number = 3;
$posts_per_page = $posts. $number;
my full code looks like .
function blogs() ?>
<article>
<?php // Display blog posts on any page @ https://m0n.co/l
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$posts = 'post_per_page=';
$number = 3;
$posts_per_page = $posts . $number ;
$wp_query->query( apply_filters('change_number_of_posts_home', $posts_per_page) . '&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<h2><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></h2>
<?php openblogger_posted_on();
openblogger_posted_by(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</article>
<?php
Can anyone tell me why the doesn't work. Also, i am trying to use filter for $number = 3; so that user can change the number easily.
Thanks in advance.
theme-development
add a comment |Â
up vote
1
down vote
favorite
I am getting a problem while applying a code. My code works fine when i use only one variable. e.g;
$posts_per_page = 'posts_per_page=3';
but it doesn't work if i use concatenation.e.g
$posts = "posts_per_page=";
$number = 3;
$posts_per_page = $posts. $number;
my full code looks like .
function blogs() ?>
<article>
<?php // Display blog posts on any page @ https://m0n.co/l
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$posts = 'post_per_page=';
$number = 3;
$posts_per_page = $posts . $number ;
$wp_query->query( apply_filters('change_number_of_posts_home', $posts_per_page) . '&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<h2><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></h2>
<?php openblogger_posted_on();
openblogger_posted_by(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</article>
<?php
Can anyone tell me why the doesn't work. Also, i am trying to use filter for $number = 3; so that user can change the number easily.
Thanks in advance.
theme-development
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am getting a problem while applying a code. My code works fine when i use only one variable. e.g;
$posts_per_page = 'posts_per_page=3';
but it doesn't work if i use concatenation.e.g
$posts = "posts_per_page=";
$number = 3;
$posts_per_page = $posts. $number;
my full code looks like .
function blogs() ?>
<article>
<?php // Display blog posts on any page @ https://m0n.co/l
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$posts = 'post_per_page=';
$number = 3;
$posts_per_page = $posts . $number ;
$wp_query->query( apply_filters('change_number_of_posts_home', $posts_per_page) . '&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<h2><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></h2>
<?php openblogger_posted_on();
openblogger_posted_by(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</article>
<?php
Can anyone tell me why the doesn't work. Also, i am trying to use filter for $number = 3; so that user can change the number easily.
Thanks in advance.
theme-development
I am getting a problem while applying a code. My code works fine when i use only one variable. e.g;
$posts_per_page = 'posts_per_page=3';
but it doesn't work if i use concatenation.e.g
$posts = "posts_per_page=";
$number = 3;
$posts_per_page = $posts. $number;
my full code looks like .
function blogs() ?>
<article>
<?php // Display blog posts on any page @ https://m0n.co/l
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$posts = 'post_per_page=';
$number = 3;
$posts_per_page = $posts . $number ;
$wp_query->query( apply_filters('change_number_of_posts_home', $posts_per_page) . '&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<h2><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></h2>
<?php openblogger_posted_on();
openblogger_posted_by(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</article>
<?php
Can anyone tell me why the doesn't work. Also, i am trying to use filter for $number = 3; so that user can change the number easily.
Thanks in advance.
theme-development
asked Aug 12 at 3:09
Raashid Din
155
155
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Look at your filter:
apply_filters('change_number_of_posts_home', $posts_per_page)
The value being passed to it is $posts_per_page
, so of course they can't change $number
.
If you want only the number to be filtered then that's the part that should have the filter applied:
$number = apply_filters('change_number_of_posts_home', 3 );
Then the query can just look like this:
$wp_query->query( 'post_per_page=' . $number . '&paged='.$paged);
They're not relevant to the question, but some other things I noticed about your code:
- You shouldn't be overwriting
$wp_query
like that. Just use a new variable for your custom query. - You can pass the arguments to
new WP_Query( $args )
rather than needing to run->query()
as a separate step. - The code would be easier to read if you pass the arguments as an array, rather than a query string.
- Each individual post should be an
<article>
. You're wrapping all the posts in a single article tag.
can you please send me a code in which i can write an individual article rather than wrapping
â Raashid Din
Aug 12 at 3:38
? Just move the tags so they're inside the loop.
â Jacob Peattie
Aug 12 at 3:45
can't understand what you are saying. Can you please edit my code. so that i get the post or just post another answer.
â Raashid Din
Aug 12 at 3:54
now i got you you were talking of article tags
â Raashid Din
Aug 12 at 3:56
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Look at your filter:
apply_filters('change_number_of_posts_home', $posts_per_page)
The value being passed to it is $posts_per_page
, so of course they can't change $number
.
If you want only the number to be filtered then that's the part that should have the filter applied:
$number = apply_filters('change_number_of_posts_home', 3 );
Then the query can just look like this:
$wp_query->query( 'post_per_page=' . $number . '&paged='.$paged);
They're not relevant to the question, but some other things I noticed about your code:
- You shouldn't be overwriting
$wp_query
like that. Just use a new variable for your custom query. - You can pass the arguments to
new WP_Query( $args )
rather than needing to run->query()
as a separate step. - The code would be easier to read if you pass the arguments as an array, rather than a query string.
- Each individual post should be an
<article>
. You're wrapping all the posts in a single article tag.
can you please send me a code in which i can write an individual article rather than wrapping
â Raashid Din
Aug 12 at 3:38
? Just move the tags so they're inside the loop.
â Jacob Peattie
Aug 12 at 3:45
can't understand what you are saying. Can you please edit my code. so that i get the post or just post another answer.
â Raashid Din
Aug 12 at 3:54
now i got you you were talking of article tags
â Raashid Din
Aug 12 at 3:56
add a comment |Â
up vote
3
down vote
accepted
Look at your filter:
apply_filters('change_number_of_posts_home', $posts_per_page)
The value being passed to it is $posts_per_page
, so of course they can't change $number
.
If you want only the number to be filtered then that's the part that should have the filter applied:
$number = apply_filters('change_number_of_posts_home', 3 );
Then the query can just look like this:
$wp_query->query( 'post_per_page=' . $number . '&paged='.$paged);
They're not relevant to the question, but some other things I noticed about your code:
- You shouldn't be overwriting
$wp_query
like that. Just use a new variable for your custom query. - You can pass the arguments to
new WP_Query( $args )
rather than needing to run->query()
as a separate step. - The code would be easier to read if you pass the arguments as an array, rather than a query string.
- Each individual post should be an
<article>
. You're wrapping all the posts in a single article tag.
can you please send me a code in which i can write an individual article rather than wrapping
â Raashid Din
Aug 12 at 3:38
? Just move the tags so they're inside the loop.
â Jacob Peattie
Aug 12 at 3:45
can't understand what you are saying. Can you please edit my code. so that i get the post or just post another answer.
â Raashid Din
Aug 12 at 3:54
now i got you you were talking of article tags
â Raashid Din
Aug 12 at 3:56
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Look at your filter:
apply_filters('change_number_of_posts_home', $posts_per_page)
The value being passed to it is $posts_per_page
, so of course they can't change $number
.
If you want only the number to be filtered then that's the part that should have the filter applied:
$number = apply_filters('change_number_of_posts_home', 3 );
Then the query can just look like this:
$wp_query->query( 'post_per_page=' . $number . '&paged='.$paged);
They're not relevant to the question, but some other things I noticed about your code:
- You shouldn't be overwriting
$wp_query
like that. Just use a new variable for your custom query. - You can pass the arguments to
new WP_Query( $args )
rather than needing to run->query()
as a separate step. - The code would be easier to read if you pass the arguments as an array, rather than a query string.
- Each individual post should be an
<article>
. You're wrapping all the posts in a single article tag.
Look at your filter:
apply_filters('change_number_of_posts_home', $posts_per_page)
The value being passed to it is $posts_per_page
, so of course they can't change $number
.
If you want only the number to be filtered then that's the part that should have the filter applied:
$number = apply_filters('change_number_of_posts_home', 3 );
Then the query can just look like this:
$wp_query->query( 'post_per_page=' . $number . '&paged='.$paged);
They're not relevant to the question, but some other things I noticed about your code:
- You shouldn't be overwriting
$wp_query
like that. Just use a new variable for your custom query. - You can pass the arguments to
new WP_Query( $args )
rather than needing to run->query()
as a separate step. - The code would be easier to read if you pass the arguments as an array, rather than a query string.
- Each individual post should be an
<article>
. You're wrapping all the posts in a single article tag.
answered Aug 12 at 3:22
Jacob Peattie
12.2k41525
12.2k41525
can you please send me a code in which i can write an individual article rather than wrapping
â Raashid Din
Aug 12 at 3:38
? Just move the tags so they're inside the loop.
â Jacob Peattie
Aug 12 at 3:45
can't understand what you are saying. Can you please edit my code. so that i get the post or just post another answer.
â Raashid Din
Aug 12 at 3:54
now i got you you were talking of article tags
â Raashid Din
Aug 12 at 3:56
add a comment |Â
can you please send me a code in which i can write an individual article rather than wrapping
â Raashid Din
Aug 12 at 3:38
? Just move the tags so they're inside the loop.
â Jacob Peattie
Aug 12 at 3:45
can't understand what you are saying. Can you please edit my code. so that i get the post or just post another answer.
â Raashid Din
Aug 12 at 3:54
now i got you you were talking of article tags
â Raashid Din
Aug 12 at 3:56
can you please send me a code in which i can write an individual article rather than wrapping
â Raashid Din
Aug 12 at 3:38
can you please send me a code in which i can write an individual article rather than wrapping
â Raashid Din
Aug 12 at 3:38
? Just move the tags so they're inside the loop.
â Jacob Peattie
Aug 12 at 3:45
? Just move the tags so they're inside the loop.
â Jacob Peattie
Aug 12 at 3:45
can't understand what you are saying. Can you please edit my code. so that i get the post or just post another answer.
â Raashid Din
Aug 12 at 3:54
can't understand what you are saying. Can you please edit my code. so that i get the post or just post another answer.
â Raashid Din
Aug 12 at 3:54
now i got you you were talking of article tags
â Raashid Din
Aug 12 at 3:56
now i got you you were talking of article tags
â Raashid Din
Aug 12 at 3:56
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f311183%2fadd-filter-not-working-when-cancatenating-variables%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password