当前网址添加和删除term-> slug

时间:2015-07-24 05:56:01

标签: php arrays wordpress loops taxonomy

我在网址上添加/删除分类法存在一些问题。

$ arch_postID - 我从当前存档页面的循环中获取帖子ID。

工作原理:

1 - 当我按<a class="cat-hide-link" href="'. $URI .''. $res_slug[$f] .'" >+</a>我正在寻找适当的页面网址'+ slug'

2 - 当我按<a href="'. $linkk .'" >-</a>我从当前网址中删除相应的“+ slug”时(如果存在)

现在代码就像那样:

我要添加3个slugs,“other-1”,“other-2”,“other-3”。我逐个添加它们,首先是“other-1”,最后是“other-3”

当前网址如下 - “domain / other / other-1 + other-2 + other-3”

当我试图在slug“other-1”上推“ - ”时,我会回到当前的整个网址

当我在slug“other-2”上推“ - ”时,我得到了URL - “domain / other / other-1”。但不是URL - “domain / other / other-1 + other-3”

当im pushng“ - ”在“other-3”上时,我得到了正确的值。 URL - “domain / other / other-1 + other-2”

有人,请帮忙!谢谢你的时间!

$Path=$_SERVER['REQUEST_URI'];
    $Prev_path=$_SERVER['HTTP_REFERER'];
    $URI='http://gradrich.tmweb.ru'.$Path;
    global $arch_postID;
    if ( !empty($arch_postID) && is_archive() ){ 
        for($i = 0; $i <= $b-1; $i++){  //goes a 100 time from 0, to show all parameters from wp_get_post_terms array
        $terms_array = wp_get_post_terms($arch_postID[$i],'other');

        for($a = 0; $a <= 100; $a++){
        $terms_name[] = $terms_array[$a]->name; // create array of terms->name
        $terms_slug[] = $terms_array[$a]->slug; // create array of terms->slug

        }  
        }   

        $result_name = array_unique($terms_name); // check name array for unique variables 
        $result_slug = array_unique($terms_slug); // check slug array for unique variables 

        for($f = 0, $s = 0; $f <= count($terms_name), $s <= count($terms_slug); $f++, $s++){ // do loop for all unique variables

        if(!empty($result_name[$f]) && !is_category() || !empty($result_slug[$s]) && !is_category() ){//check if variables not empty than get proper name and slug from two arrays

        $res_slug[$f] = '+'.$result_slug[$s].'';
        $linkk[$f] = substr($URI, 0, strrpos($URI, $res_slug[$f]));
        echo '<div class="tagCloud-cover" id="cat-hide-'. $s .'"><a class="cat-hide-link" href="'. $URI .''. $res_slug[$f] .'" >+</a>';
        echo '<a href="'. get_post_type_archive_link() .''. $result_slug[$s] .'" rel="tag">' . $result_name[$f] . '</a>';   
        echo '<a href="'. $linkk .'" >-</a></div>'; 

        if (false !== strpos($Path, $result_slug[$s])) {
        echo '<style>#cat-hide-'. $s .'{display:none;}</style>';
        echo '<div class="tagCloud-cover" id="cat-hide"><span class="cat-hide-link" >+</span>';
        echo '<a href="'. get_post_type_archive_link() .''. $result_slug[$s] .'" rel="tag">' . $result_name[$f] . '</a>';
        echo '<a href="'. $linkk[$f] .'" >-</a></div>';          
        }
        } else{
        }
        }  
    }

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,用于查找当前网址以查找添加的标签 - &gt; slug,删除它并获取新网址:

    $only_tags = substr($URI, strrpos($URI,'/')+1);
    $tags = explode('+',$only_tags);
    $tags_string = implode('+',$tags);

以下工作代码:

  for($f = 0, $s = 0; $f <= count($terms_name), $s <= count($terms_slug); $f++, $s++){

        if(!empty($result_name[$f]) && !is_category() || !empty($result_slug[$s]) && !is_category() ){

        $only_tags = substr($URI, strrpos($URI,'/')+1);
        $tags = explode('+',$only_tags);

        foreach ($tags as $t_key=>$t_value) {
        if ($t_value == $result_slug[$s]) unset ($tags[$t_key]);
         }
        $tags_string = implode('+',$tags);

        echo '<div class="tagCloud-cover" id="cat-hide-'. $s .'"><a class="cat-hide-link" href="'. $URI .'+'. $result_slug[$s] .'" >+</a>';
        echo '<a href="'. $result_slug[$s] .'" rel="tag">' . $result_name[$f] . '</a>'; 
        echo '<a href="'. $tags_string .'" >-</a></div>';
        if (false !== strpos($Path, $result_slug[$s])) {
        echo '<style>#cat-hide-'. $s .'{display:none;}</style>';
        echo '<div class="tagCloud-cover" id="cat-hide"><span class="cat-hide-link" >+</span>';
        echo '<a href="'. $result_slug[$s] .'" rel="tag">' . $result_name[$f] . '</a>';
        echo '<a href="'. $tags_string .'" >-</a></div>'; 

        }

        }