how to check if current loop is main loop?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

how to check if current loop is main loop?

Konrad Karpieszuk
hello

i want to add smth before first post on the page. i am thinking about
action hack loop_start, is it a right direction?

but i want to add this smth only in main loop. if it is homepage (or
any archive page), only before first post in main loop. if it is
single post, only before first post.

because theme developers usually put in theme many special loops (for
example loop for featured posts, used in some sliders etc) i want to
avoid to run my function in this special loop. how can i check (inside
the function) if loop_start is fired in context of main loop for
particular page?

--
(en) regards / (pl) pozdrawiam
Konrad Karpieszuk
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: how to check if current loop is main loop?

Christopher Ross
Konrad,

I assume this is in your own theme, if it is I would recommend adding it to the loop.php or individual cases as a special case.

if (!isset($firstcount)) {$firstcount++; custom_function();}

(not tested)

Chris

On 2011-05-28, at 3:59 PM, Konrad Karpieszuk wrote:

> hello
>
> i want to add smth before first post on the page. i am thinking about
> action hack loop_start, is it a right direction?
>
> but i want to add this smth only in main loop. if it is homepage (or
> any archive page), only before first post in main loop. if it is
> single post, only before first post.
>
> because theme developers usually put in theme many special loops (for
> example loop for featured posts, used in some sliders etc) i want to
> avoid to run my function in this special loop. how can i check (inside
> the function) if loop_start is fired in context of main loop for
> particular page?
>
> --
> (en) regards / (pl) pozdrawiam
> Konrad Karpieszuk
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers



__

Christopher Ross
http://thisismyurl.com

Toronto - (416) 900-3731

_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: how to check if current loop is main loop?

Konrad Karpieszuk
On Sat, May 28, 2011 at 9:28 PM, Christopher Ross <[hidden email]> wrote:
> I assume this is in your own theme

no :( i am creating plugin which i want to publish for all. so i
wanted to make this most universally and avoid to ask people who will
install this to put special template tag in theme.


--
(en) regards / (pl) pozdrawiam
Konrad Karpieszuk
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: how to check if current loop is main loop?

Andrew Nacin-2
---------- Forwarded message ----------
From: Konrad Karpieszuk <[hidden email]>
Date: Sat, May 28, 2011 at 3:54 PM
Subject: Re: [wp-hackers] how to check if current loop is main loop?
To: [hidden email]

On Sat, May 28, 2011 at 9:28 PM, Christopher Ross <[hidden email]>
wrote:
> I assume this is in your own theme

no :( i am creating plugin which i want to publish for all. so i
wanted to make this most universally and avoid to ask people who will
install this to put special template tag in theme.

To check if you're on the main loop: $wp_the_query === $wp_query
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: how to check if current loop is main loop?

Konrad Karpieszuk
On Sat, May 28, 2011 at 11:50 PM, Andrew Nacin <[hidden email]> wrote:
> To check if you're on the main loop: $wp_the_query === $wp_query

its great, thank you Andrew!


--
(en) regards / (pl) pozdrawiam
Konrad Karpieszuk
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: how to check if current loop is main loop?

Konrad Karpieszuk
In reply to this post by Andrew Nacin-2
On Sat, May 28, 2011 at 11:50 PM, Andrew Nacin <[hidden email]> wrote:
> To check if you're on the main loop: $wp_the_query === $wp_query


i wrote my opinion little too fast :) it isnt working (or i dont
understand smth, or i didnt explain what i need well).

short plugin:

/* Plugin Name: Loop test */
function wyswietl_slider() {
 global $wp_the_query, $wp_query, $post, $wp;
 if ($wp_the_query === $wp_query) :
 echo "it is main loop";
 endif;
}
add_action('loop_start', 'wyswietl_slider');

it shows text "it is main loop" above main loop. but it shows also
above other loops.

for example in sidebar i have standard widget to to show "Last posts".
over last posts i see also text "it is main loop". Even in situation
when WP displays single post (and in sidebar i have few last posts, so
main loop and loop in widget isnt thesame)

Andrew (and others), what i did wrong? :)



--
(en) regards / (pl) pozdrawiam
Konrad Karpieszuk
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: how to check if current loop is main loop?

Konrad Karpieszuk
ok, looks that i found solution :)

/* Plugin Name: Loop test */
function wyswietl_slider($array) {
 global $wp_the_query, $wp_query, $post, $wp;
 if ($array === $wp_query) :
 echo "it is main loop";
 endif;
}
add_action('loop_start', 'wyswietl_slider');

the diffrence is that i passed $array to function and i checked if
$array === $wp_query



On Sun, May 29, 2011 at 6:57 PM, Konrad Karpieszuk
<[hidden email]> wrote:

> On Sat, May 28, 2011 at 11:50 PM, Andrew Nacin <[hidden email]> wrote:
>> To check if you're on the main loop: $wp_the_query === $wp_query
>
>
> i wrote my opinion little too fast :) it isnt working (or i dont
> understand smth, or i didnt explain what i need well).
>
> short plugin:
>
> /* Plugin Name: Loop test */
> function wyswietl_slider() {
>  global $wp_the_query, $wp_query, $post, $wp;
>  if ($wp_the_query === $wp_query) :
>  echo "it is main loop";
>  endif;
> }
> add_action('loop_start', 'wyswietl_slider');
>
> it shows text "it is main loop" above main loop. but it shows also
> above other loops.
>
> for example in sidebar i have standard widget to to show "Last posts".
> over last posts i see also text "it is main loop". Even in situation
> when WP displays single post (and in sidebar i have few last posts, so
> main loop and loop in widget isnt thesame)
>
> Andrew (and others), what i did wrong? :)
>
>
>
> --
> (en) regards / (pl) pozdrawiam
> Konrad Karpieszuk
>



--
(en) regards / (pl) pozdrawiam
Konrad Karpieszuk
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers