Multiple taxonomy with next_post_link
-
Hi,
I currently have two problems I can not solve.
The first is that I created two custom taxonomy (produit_type and produit_marque). I correctly displays the post list of each taxonomy (in taxonomy-produit_type.php and taxonomy-produit_marque.php). My problem comes when I view the page single-produit.php which corresponds to a product in one of the selected taxonomy. I shall post the good product but I will wish 2 things:
– Display a list of the same product type (5 at random from the same class and with the same value). If I have the taxonomy ‘produit_type’ whose value is ‘chair’ I must show only products whose taxonomy is ‘produit_type’ and the value is ‘chair’. Knowing that a product can belong to several produit_type (eg ‘light’, ‘accessory and decorative’).
– Have a simple paging where I post a link to the previous one post or the post next (if any) of the same taxonomy and taxonomy of the same value. As before I will be able to navigate between posts having for example taxonomy ‘produit_type’ and value ‘chair’, knowing also that the post may have more value for the taxonomy produit_type.=> 2 in case I’m supposed to know where the person just: click on a menu produit_type (eg, lamps, chairs, accessories and deco), then click on a product from the list of this taxonomy, I will show Product with this information, 5 products mini (described above) and a previous one link and another product next product.
=> My problem here is that when I get on the single-page produit.php I am unable to know what I come taxonomy, since if the product belongs to several produit_type I am not able to find if I clicked on one or the other to come to the single page.
=> You can find the dev site here : 5francs
The second I will present once found the first big_smile for you too take the lead!
Thank you in advance for your help!
DDD
-
I really need help…
Did someone have a idea ?
In your single page template, this should generate the random list of related posts based on the taxonomy terms of the current post: https://pastebin.com/8LNec18N
This is untested; please let me know if it works.
Hi,
before you answer me i found a solution and it’s like in your sample !
The small difference is that i have to check where i come form in order to search only in the taxonomy i just select. My problem was that some product could be in 2 taxonomy or more.
Here is my function code :
function generate_url($produit_type = null, $produit_marque = null, $prix = null, $produits = null, $search = false) { $url_query = "?"; $noEmpty = false; if(isset($produit_type)) { if($noEmpty) $url_query .= '&'; $url_query .= REWRITE_PRODUIT_TYPE . "=" . $produit_type; $noEmpty = true; } if(isset($produit_marque)) { if($noEmpty) $url_query .= '&'; $url_query .= REWRITE_PRODUIT_MARQUE . "=" . $produit_marque; $noEmpty = true; } if(isset($prix)) { if($noEmpty) $url_query .= '&'; $url_query .= REWRITE_PRODUIT_PRIX . "=" . $prix; $noEmpty = true; } if($search) { if($noEmpty) $url_query .= '&'; $url_query .= REWRITE_SEARCH . "=1"; } return $url_query; } function decompose_url() { $data = array(); // Dans tous les cas je récupère les produits dans les cookies if(isset($_COOKIE[REWRITE_PRODUITS]) && !empty($_COOKIE[REWRITE_PRODUITS])) { $data[REWRITE_PRODUITS] = str_replace('\\','', $_COOKIE[REWRITE_PRODUITS]); } if(isset($_GET[REWRITE_PRODUIT_TYPE]) && !empty($_GET[REWRITE_PRODUIT_TYPE])) $data[REWRITE_PRODUIT_TYPE] = array( 'taxonomy' => 'produit_type', 'field' => 'slug', 'terms' => $_GET[REWRITE_PRODUIT_TYPE] ); if(isset($_GET[REWRITE_PRODUIT_MARQUE]) && !empty($_GET[REWRITE_PRODUIT_MARQUE])) $data[REWRITE_PRODUIT_MARQUE] = array( 'taxonomy' => 'produit_marque', 'field' => 'slug', 'terms' => $_GET[REWRITE_PRODUIT_MARQUE] ); if(isset($_GET[REWRITE_PRODUIT_PRIX]) && !empty($_GET[REWRITE_PRODUIT_PRIX])) $data[REWRITE_PRODUIT_PRIX] = $_GET[REWRITE_PRODUIT_PRIX]; if(isset($_GET[REWRITE_SEARCH]) && !empty($_GET[REWRITE_SEARCH]) && ($_GET[REWRITE_SEARCH]==1)) $data[REWRITE_SEARCH] = 1; // echo "<pre>"; // print_r($data); // echo "</pre>"; return $data; } /** * Génère la requete de taxonomie à partir de l'url * @return taxquery */ function get_query_args_from_url() { $data = decompose_url(); if(isset($data[REWRITE_SEARCH])) $search = $data[REWRITE_SEARCH]; if(isset($data[REWRITE_PRODUIT_TYPE])) $type = $data[REWRITE_PRODUIT_TYPE]; if(isset($data[REWRITE_PRODUIT_MARQUE])) $marque = $data[REWRITE_PRODUIT_MARQUE]; if(isset($data[REWRITE_PRODUIT_PRIX])) $prix = $data[REWRITE_PRODUIT_PRIX]; // SI ON VIENT DE LA RECHERCHE if(isset($search)) { $meta_query = array(); if( isset($type) && $type['terms']!=ALL_CATEGORIE) { $query_args['produit_type'] = $type['terms']; $searchArgs[REWRITE_PRODUIT_TYPE] = $type['terms']; } if(isset($marque) && $marque['terms']!=ALL_CATEGORIE) { $query_args['produit_marque'] = $marque['terms']; $searchArgs[REWRITE_PRODUIT_MARQUE] = $marque['terms']; } if(isset($prix) && $prix!=ALL_CATEGORIE && preg_match(PATTERN_PRODUIT_PRIX, $prix)) { $searchArgs[REWRITE_PRODUIT_PRIX] = $prix; $expprix = explode("-", $prix['terms']); if(count($expprix)>1) { array_push($meta_query, array( 'key' => 'produit_prix', 'value' => $expprix, 'compare' => 'BETWEEN', 'type' => 'NUMERIC' )); } else { array_push($meta_query, array( 'key' => 'produit_prix', 'value' => $prix, 'compare' => '>=', 'type' => 'NUMERIC' )); } } $query_args['orderby'] = 'produit_prix'; $query_args['order'] = 'ASC'; $query_args['post_type'] = 'produit'; $query_args['post__not_in'] = array(get_the_ID()); $query_args['hide_empty'] = '0'; if(isset($meta_query) && is_array($meta_query) && count($meta_query)>0) $query_args['meta_query'] = $meta_query; } // SINON ON VIENT DES AUTRES PAGES else { $tax_query = array(); if( isset($marque) && isset($type)) $tax_query = array( 'relation' => 'AND', $type, $marque ); else if(isset($type)) $tax_query = array($type); else if(isset($marque)) $tax_query = array($marque); $query_args = array( 'post_type' => 'produit', 'post__not_in' => array(get_the_ID()), 'posts_per_page' => 5, 'orderby' => 'rand', 'tax_query' => $tax_query ); } return $query_args; } /** * Génrère le lien de retour à la liste de produit de départ */ function get_back_to_list_link() { $data = decompose_url(); // echo "<pre>"; // print_r($query_args); // echo "</pre>"; if(isset($data[REWRITE_SEARCH])) $search = $data[REWRITE_SEARCH]; if(isset($data[REWRITE_PRODUIT_TYPE])) $type = $data[REWRITE_PRODUIT_TYPE]; if(isset($data[REWRITE_PRODUIT_MARQUE])) $marque = $data[REWRITE_PRODUIT_MARQUE]; if(isset($data[REWRITE_PRODUIT_PRIX])) $prix = $data[REWRITE_PRODUIT_PRIX]; if(isset($data[REWRITE_PRODUITS])) $produits = $data[REWRITE_PRODUITS]; // On vient de la page de recherche if($search) { //TODO: GERER LE RETOUR A LA PAGE DE RECHERCHE //?deco-industrielle=table&marque=tolix&prix=500-999&produit_search=1&submit=? $search_url = '?'; if(isset($type)) $search_url .= REWRITE_PRODUIT_TYPE . '=' . $type['terms']; if(isset($marque)) { if(strlen($search_url)>2) $search_url .= '&'; $search_url .= REWRITE_PRODUIT_MARQUE . '=' . $marque['terms']; } if(isset($prix)) { if(strlen($search_url)>2) $search_url .= '&'; $search_url .= REWRITE_PRODUIT_PRIX . '=' . $prix; } return site_url('/' . $search_url . REWRITE_SEARCH_FIN, 'http'); } // Si on vient d'une taxonomy particulière else if(isset($type) || isset($marque)) { if(isset($type)) return site_url('/' . REWRITE_PRODUIT_TYPE . '/' . $type['terms'], 'http'); if(isset($marque)) return site_url('/' . REWRITE_PRODUIT_MARQUE . '/' . $marque['terms'], 'http'); } // Si on vient des autres pages else { return home_url(); } } /** * Génère un tableau contenant l'élément précédent et/ou le suivant * @return multitype:string */ function get_nav_produit_links() { $current_produit_id = get_the_ID(); $data = decompose_url(); // echo "<pre>"; // print_r($data); // echo "</pre>"; if(isset($data[REWRITE_PRODUIT_TYPE])) $type = $data[REWRITE_PRODUIT_TYPE]; if(isset($data[REWRITE_PRODUIT_MARQUE])) $marque = $data[REWRITE_PRODUIT_MARQUE]; if(isset($data[REWRITE_PRODUIT_PRIX])) $prix = $data[REWRITE_PRODUIT_PRIX]; if(isset($data[REWRITE_PRODUITS])) $produits = $data[REWRITE_PRODUITS]; if(isset($data[REWRITE_SEARCH])) $search = $data[REWRITE_SEARCH]; $produits_copy = $produits; $liens = array(); $nbProduits = 0; $compteur = 0; $previous = -1; $current = -1; $next = -1; $current_post_find = false; if($current_produit_id!=null && $produits_copy!=null && is_serialized($produits_copy)) { $produits_copy = maybe_unserialize($produits_copy); if(is_array($produits_copy)) { $nbProduits = count($produits_copy); foreach ($produits_copy as $produit_id) { if(preg_match(PATTERN_PRODUIT_ID, $produit_id)) { if(!$current_post_find) { if($compteur==0) $previous = -1; else $previous = $current; $current = $produit_id; }else { if($compteur==($nbProduits)) $next = -1; else $next = $produit_id; } // Le post suivant vient d'être récupéré, on s'arrête. if($current_post_find) break; // Si on trouve l'id du post courant on s'arrête au prochain tour if($current_produit_id==$produit_id) $current_post_find = true; } $compteur++; } $link = (!isset($url_query))?generate_url($type['terms'], $marque['terms'], $prix, $produits, $search):''; if($previous!=-1) $liens['previous'] = '<a href="' . get_permalink($previous) . $link . '" ><span class="meta-nav" title="' . get_the_title($previous) . '"> </span></a>'; if($next!=-1) $liens['next'] = '<a href="' . get_permalink($next) . $link . '" ><span class="meta-nav" title="' . get_the_title($next) . '"> </span></a>'; } } return $liens; }
Thanks for your help
Hi i have problem with this code,the READ MORE option is not working my wordpress site,full code is here…kindly plz help me..its urgent..
<?php
/**
* @category Fishpig
* @package Fishpig_Wordpress
* @license https://fishpig.co.uk/license.txt
* @author Ben Tideswell <[email protected]>
*/
?>
<?php $posts = $this->getPosts() ?>
<?php if (count($posts) > 0): ?>
<div class=”block-content”>
<?php foreach($posts as $post): ?>
<?php $post->setExcerptSize($this->getExcerptLength()) ?>
<?php if ($this->canDisplayExcerpt()): ?>
getPermalink() ?>”>
<p class=”post-excerpt”>
<?php $content = $post->getPostExcerpt(); $content = strip_tags($content); echo substr($content, 0, 250); ?>
</p>
<p style=”text-align: right;”>Read More</p>
<?php endif; ?>
<?php endforeach; ?>
<script type=”text/javascript”>decorateList(‘<?php echo $this->getListId() ?>’)</script>
<?php if ($this->canShowPager()): ?>
<?php echo $this->getPagerHtml() ?>
<?php endif; ?>
</div>
<?php endif; ?>Please
Hi kabbo21.
There is something wrong with your Code in Line 15.
ThatgetPermalink() ?>">
doesn’t work.Please look in your Codeeditor at this Line.
Second: You should make a link to your post like this:
<?php the_content('Read more...'); ?>
Can You help me sir…kindly give me one info first how i make box like as
“tripled” comments for code box,i have no knowladge about this,then may be i will showing you proper code…please
Sorry. i am not an english speaking person and i doen’t understand
“tripled” comments for code box
Bro kindly check this..Here is my function code this words under one box,where’s he show his function code…how to create this..
bro kindly check this code,is in there read more option is not working::
here is code
<?php /** * @category Fishpig * @package Fishpig_Wordpress * @license https://fishpig.co.uk/license.txt * @author Ben Tideswell <[email protected]> */ ?> <?php $posts = $this->getPosts() ?> <?php if (count($posts) > 0): ?> <div class="block-content"> <?php foreach($posts as $post): ?> <?php $post->setExcerptSize($this->getExcerptLength()) ?> <?php if ($this->canDisplayExcerpt()): ?> <a href="<?php echo $post->getPermalink() ?>"> <p class="post-excerpt"> <?php $content = $post->getPostExcerpt(); $content = strip_tags($content); echo substr($content, 0, 250); ?> </p> <p style="text-align: right;">>Read More</p> <?php endif; ?> <?php endforeach; ?> <script type="text/javascript">decorateList('<?php echo $this->getListId() ?>')</script> <?php if ($this->canShowPager()): ?> <?php echo $this->getPagerHtml() ?> <?php endif; ?> </div> <?php endif; ?>
Sorry this is actual code where is not working “read more”option kindly help me pleaseeeeeeee
<?php /** * @category Fishpig * @package Fishpig_Wordpress * @license https://fishpig.co.uk/license.txt * @author Ben Tideswell <[email protected]> */ ?> <?php $posts = $this->getPosts() ?> <?php if (count($posts) > 0): ?> <div class="block-content"> <?php foreach($posts as $post): ?> <?php $post->setExcerptSize($this->getExcerptLength()) ?> <?php if ($this->canDisplayExcerpt()): ?> <a href="<?php echo $post->getPermalink() ?>"> <p class="post-excerpt"> <?php $content = $post->getPostExcerpt(); $content = strip_tags($content); echo substr($content, 0, 250); ?> </p> <p style="text-align: right;">Read More</p> </a> <?php endif; ?> <?php endforeach; ?> <script type="text/javascript">decorateList('<?php echo $this->getListId() ?>')</script> <?php if ($this->canShowPager()): ?> <?php echo $this->getPagerHtml() ?> <?php endif; ?> </div> <?php endif; ?>
Try this:
<?php /** * @category Fishpig * @package Fishpig_Wordpress * @license https://fishpig.co.uk/license.txt * @author Ben Tideswell <[email protected]> */ ?> <?php $posts = $this->getPosts() ?> <?php if (count($posts) > 0): ?> <div class="block-content"> <?php foreach($posts as $post): ?> <?php $post->setExcerptSize($this->getExcerptLength()) ?> <?php if ($this->canDisplayExcerpt()): ?> <a href="<?php echo $post->getPermalink() ?>"> <p class="post-excerpt"> <?php $content = $post->getPostExcerpt(); $content = strip_tags($content); echo substr($content, 0, 250); ?> </p> <p style="text-align: right;"><?php the_content('Read more...'); ?></p> </a> <?php endif; ?> <?php endforeach; ?> <script type="text/javascript">decorateList('<?php echo $this->getListId() ?>')</script> <?php if ($this->canShowPager()): ?> <?php echo $this->getPagerHtml() ?> <?php endif; ?> </div> <?php endif; ?>
Sir
<p style="text-align: right;"><?php the_content('Read more...'); ?></p> </a> <?php endif; ?>
is in this line have one link for generating content from that’s link,and this link included by href!but i don’t understand where i including thats link again and how?
please….when anyone click read more button then its generating some data from one link,and that’s link included by href .but now is in there have no any link option or href.so how i include link for READ MORE button?
Hi. I really doen’t understand all your problems.
You can take this to generate a link to a post:
<a href="<?php the_permalink(); ?>Read more ...</a>
In your code (https://www.ads-software.com/support/topic/multiple-taxonomy-with-next_post_link?replies=13#post-3931745) is a permalink in line 15. The Excerpt is in the “” and the “” Tags
And only can guess that you will do this? I think this is the solution:
<?php /** * @category Fishpig * @package Fishpig_Wordpress * @license https://fishpig.co.uk/license.txt * @author Ben Tideswell <[email protected]> */ ?> <?php $posts = $this->getPosts() ?> <?php if (count($posts) > 0): ?> <div class="block-content"> <?php foreach($posts as $post): ?> <?php $post->setExcerptSize($this->getExcerptLength()) ?> <?php if ($this->canDisplayExcerpt()): ?> <p class="post-excerpt"> <?php $content = $post->getPostExcerpt(); $content = strip_tags($content); echo substr($content, 0, 250); ?> </p> <p style="text-align: right;"><a href="<?php echo $post->getPermalink() ?>">Read more</a></p> <?php endif; ?> <?php endforeach; ?> <script type="text/javascript">decorateList('<?php echo $this->getListId() ?>')</script> <?php if ($this->canShowPager()): ?> <?php echo $this->getPagerHtml() ?> <?php endif; ?> </div> <?php endif; ?>
Hi,
I think your problem is that you have 2 links…
The first is
<a>getPermalink() ?>">...</a>
And the second link is inside the first one ! :
<a>getPermalink() ?>">...<?php the_content('Read more...'); ?>...</a>
I must not have a link include in an other link !
For your information i do that in my page :
<a class="entry-content produit <?php echo $class;?>" href="<?php the_permalink(); ?><?php if(isset($url)) echo $url;?>"> <div class="vendu" style="<?php echo $vendu_display;?>"></div> <div class="fadehover"> <?php echo wp_get_attachment_image(get_field('my_custom_field1'), 'produit-apercu', $icon, array('class'=>'apercu-noir-et-blanc') ); echo wp_get_attachment_image(get_field('my_custom_field2'), 'produit-apercu', $icon, array('class'=>'apercu') ); ?> </div> <ul> <?php $data = get_field('my_custom_field3'); if ($data):?> <li><h4><?php echo truncateHtml($data,28, '...', true); ?></h4></li> <?php endif; $taxonomies = get_the_terms(get_the_ID(),my_id); $brand = ""; if(!empty($taxonomies) && is_array($taxonomies)) { foreach($taxonomies as $t){ $brand = $t->name; break; } } $price = get_field(my_custom_field4); if ($brand && price ):?> <li class="brand-price"><?php echo $brand;?> - <?php echo price; ?> €<?php echo $loop->current_post;?></li> <?php endif; ?> </ul> </a>
Using this function :
/** * truncateHtml can truncate a string up to a number of characters while preserving whole words and HTML tags * * @param string $text String to truncate. * @param integer $length Length of returned string, including ellipsis. * @param string $ending Ending to be appended to the trimmed string. * @param boolean $exact If false, $text will not be cut mid-word * @param boolean $considerHtml If true, HTML tags would be handled correctly * * @return string Trimmed string. */ function truncateHtml($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true) { if ($considerHtml) { // if the plain text is shorter than the maximum length, return the whole text if (strlen(preg_replace('/<.*?>/', '', $text)) <= $length) { return $text; } // splits all html-tags to scanable lines preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER); $total_length = strlen($ending); $open_tags = array(); $truncate = ''; foreach ($lines as $line_matchings) { // if there is any html-tag in this line, handle it and add it (uncounted) to the output if (!empty($line_matchings[1])) { // if it's an "empty element" with or without xhtml-conform closing slash if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) { // do nothing // if tag is a closing tag } else if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) { // delete tag from $open_tags list $pos = array_search($tag_matchings[1], $open_tags); if ($pos !== false) { unset($open_tags[$pos]); } // if tag is an opening tag } else if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) { // add tag to the beginning of $open_tags list array_unshift($open_tags, strtolower($tag_matchings[1])); } // add html-tag to $truncate'd text $truncate .= $line_matchings[1]; } // calculate the length of the plain text part of the line; handle entities as one character $content_length = strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2])); if ($total_length+$content_length> $length) { // the number of characters which are left $left = $length - $total_length; $entities_length = 0; // search for html entities if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) { // calculate the real length of all entities in the legal range foreach ($entities[0] as $entity) { if ($entity[1]+1-$entities_length <= $left) { $left--; $entities_length += strlen($entity[0]); } else { // no more characters left break; } } } $truncate .= substr($line_matchings[2], 0, $left+$entities_length); // maximum lenght is reached, so get off the loop break; } else { $truncate .= $line_matchings[2]; $total_length += $content_length; } // if the maximum length is reached, get off the loop if($total_length>= $length) { break; } } } else { if (strlen($text) <= $length) { return $text; } else { $truncate = substr($text, 0, $length - strlen($ending)); } } // if the words shouldn't be cut in the middle... if (!$exact) { // ...search the last occurance of a space... $spacepos = strrpos($truncate, ' '); if (isset($spacepos)) { // ...and cut the text in this position $truncate = substr($truncate, 0, $spacepos); } } // add the defined ending to the text $truncate .= $ending; if($considerHtml) { // close all unclosed html-tags foreach ($open_tags as $tag) { $truncate .= '</' . $tag . '>'; } } return $truncate; }
- The topic ‘Multiple taxonomy with next_post_link’ is closed to new replies.