Thursday 15 August 2013

php - Avoiding Duplicate item in loop -


I wrote a question to show related posts in Wordpress, a brief description: first it checks which category of existing posts , Then an array ("$ cats") puts the names of those categories in the "slug", then using forerunners, I ask posts with the same taxonomies. When this post comes under more than one category and there is another post for this category, in this situation, this code works very well, in which case other posts appear two or more, depending on the How many categories they share,

The question is, how can I check that someone is repeating the post and how to stop it? Code:

  & lt ;? Php $ terms = Get_the_terms ($ current, 'taxonomy'); $ Cats = array_map (function ($ a) {return $ a-> slug;}, $ terms); Foreach ($ cat $ cat) $ $ args = array ('taxonomy' => $ cat, 'post_type' = & gt; 'apps',' post_status' = & gt; 'publish', ' Posts_per_page '=> 4,); Global $ wp_query; $ Wp_query = New WP_Query ($ Args); While ($ wp_query-> is_poz ()): $ wp_query-> The_post (); If ($ currentid! == get_the_ID ()) {? & Gt; & Lt; Li & gt; & Lt; A href = "& lt ;? php the_permalink ()? & Gt;" Rel = "Bookmark" title = "& lt ;? php the_title_attribute () ;? & gt;" & Gt; & Lt ;? Php if (has_post_thumbnail ()) {$ appfetured = wp_get_attachment_image_src (get_post_thumbnail_id (get_the_ID ())); $ Appfeturedimage = $ appfetured [0]; } And {$ appfeturedimage = '../img/defaultappicon.png'; }? & Gt; & Lt; Img class = "appfeatured" src = "& lt ;? php echo $ appfeturedimage ;? gt;" Alt = "& lt ;? php the_title_attribute () ;? & gt;" / & Gt; & Lt; / A & gt; & Lt; / Li & gt; & Lt ;? Php} timeline; Wp_reset_query (); // restore the global post data stomped by the_post (). } // end up? & Gt;    

If you are trying to do all the categories for the current post Try:

  & lt ;? Php $ args = array ('cat' = & gt; wp_get_post_categories ($ currentid), 'post_type' = & gt; 'app', 'post_status' = & gt; 'publish', 'posts_per_page' => 4 ); New WP_Query ($ Args);   

I have used Wordpress, so some time is happening, so I do not know the difference between categories and taxonomies, so you can try it: < Pre> & lt ;? Php terms = get_the_terms ($ currentid, 'taxonomy'); $ Cats = array_map (function ($ a) {return $ a-> slug;}, $ terms); $ Args = array ('taxonomy' = & gt; $ cats, 'post_type' = & gt; 'apps', 'post_status' = & gt; 'publish', 'posts_per_page' => 4,); Global $ wp_query; $ Wp_query = New WP_Query ($ Args);

No comments:

Post a Comment