Widgets & WP Query & duplicate posts
-
Hi, sometimes we need to place some ads between posts on homepage, so I decided to use widgets and sidebar on homepage. For example, Post Widget / Post Widget / Ads / Post Widget / etc.. Unfortunately if I place more widgets, it just shows same 2 posts. It was all fine as long as the code was on one page but it stopped working when I moved it to widgets.
I use $donotduplicate array which is then added to WP_Query as post__not_in. I also tried to put global $donotduplicate to functions.php or front-page.php but that didn’t work either.
Code can be checked at: http://pastebin.com/fgXpAnv7
-
Hi iGreenie
It seems the global $post object is not set. Use setup_postdata() to also use functions as get_template_part().
http://codex.wordpress.org/Function_Reference/setup_postdataTry it with this: http://pastebin.com/bNKUTqHD
Oh thanks. I just tried to add global $post and it’s now working. 🙂
Thanks!I hope it’s not bad to reopen the topic but I encountered the same problem again, yet I can’t get it resolved even with setup_postdata().
I have another widget to show posts from 1 category in the sidebar, but I want to exclude posts that are on the homepage. When I tried to add global $donotduplicate to the widget’s function widget(), it didn’t remember the posts added earlier in HP news widget and returned 0 on var_dump.
Here is code: http://pastebin.com/1pbbh2F5
It’s working in class “mz_hp_otvirak”, “mz_hp_single_news” and I also want to use it in “mz_fcm_komentare_widget” widget.Can someone help me with that?
Another plugin could alter the $donotduplicate variabe. Rename it to something else.
Do you set it to 0 anywhere else?
Good point keesiemeijer. I changed the variable from $donotduplicate to $fcm_hp_duplicate_news but unfortunately that didn’t help either.
I have this variable only in the parts as shown in the pastebin, plus in functions.php
function mz_global_variable() { global $donotduplicate; $donotduplicate = array(); global $fcm_hp_duplicate_news; $fcm_hp_duplicate_news = array(); } add_action( 'init', 'mz_global_variable' );
The topic ‘Widgets & WP Query & duplicate posts’ is closed to new replies.