Get the correct post and cat ID outside the loop
-
hi folks,
big problem i can’t solve alone anymore. i’m using wordpress as a cms on a language school site. to determine which sub-navigation points have to be dieplayed on the left side, i need to get the correct post and category ids outside the loop.
the site is language-school-germany.eu
in the sidebar on the left side you see the articles for the active category. the topnav links are categories. on those category pages i display just pages with the plugin category pages.
now if you go to language-school-germany.eu/en-german-courses/german-courses below you see the correct post and cat ids.
if you go to language-school-germany.eu/en-german-courses the wrong category and post id is outputed, thus also the wrong subnav is generated. cat 7 belongs to language-school-germany.eu/en-german-courses/regensburg (category), post 229 is more information about regensburg.
i can set the post 229 to draft. result: in the category above there’s another post and cat id displayed. but again the wrong one.
the code i use for the fetching and display of cat and post ids is:
global $post; foreach((get_the_category()) as $category)$postcat= $category->cat_ID; echo "post ".$wp_query->post->ID. " cat ".$postcat; if($postcat) { $tmp_post = $post; $postlist=array(); $get_posts_args="numberposts=-1&category=".$postcat; $myposts = get_posts($get_posts_args); foreach($myposts as $post) { setup_postdata($post); $array_key=(int)get_the_ID(); $postlist[$array_key]['permalink']=get_permalink(); $postlist[$array_key]['title']=get_the_title(); if(get_permalink()==($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']))$class="class=\"act\""; $output.="<li><a href=\"".get_permalink()."\" ".$class.">".get_the_title()."</a></li>"; unset ($class); } if(get_post_meta($post->ID,"reihenfolge","single")) { $reihenfolge=explode(",",get_post_meta($post->ID,"reihenfolge","single")); foreach ($reihenfolge as $nummer) { if(str_ireplace("https://","",$postlist[$nummer]['permalink'])==($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']))$class="class=\"act\""; $output_sorted.="<li><a href=\"".$postlist[$nummer]['permalink']."\" ".$class.">".$postlist[$nummer]['title']."</a></li>"; unset ($class); } } } $post = $tmp_post; if (!$output_sorted)echo "<ul>".$output."</ul>"; else echo "<ul>".$output_sorted."</ul>";
i just can’T find the error…
- The topic ‘Get the correct post and cat ID outside the loop’ is closed to new replies.