2017-09-18 72 views
2

我想在下面的代碼中選擇設置默認按鈕默認:設置在PHP按鈕代碼

<div class="accordion ui-group"> 
<h3><a href="#">Certification</a></h3> 
<div class="button-group js-radio-button-group" data-filter-group="certification" > 
<button class="button is-checked" data-filter="">All</button> 

<?php 
    $term_id = 69; 
    $taxonomy_name = 'project_category'; 
    $term_children = get_term_children($term_id, $taxonomy_name); 
    foreach ($term_children as $child) { 
     $term = get_term_by('id', $child, $taxonomy_name); 
     if($term->count > 0) { 
      echo '<button class="button" data-filter=".'. $term->slug .'">'. $term->name .'</button>'; 
     }; 
    }; 
?> 

</div> 
</div> 

的方案正在填充Project CategoriesCertificationsid=69)的母公司。

現在,ALL被默認選中,但我想指定一個默認的特定選項。我知道這應該很容易,但我無法弄清楚!

任何幫助將是偉大的。

+0

'data-filter'是一個不尋常的屬性。我假設你使用jQuery Isotope?在這種情況下,解決方案是jQuery驅動的,而不是PHP。 –

+0

@DeniseH ***「現在,默認情況下選擇«ALL»,但我想指定一個默認的特定選項。」***你想詳細說明你的意思嗎?**所有**?是否全部在''或所有生成的按鈕?什麼將符合您的***特定***選項? – Poiz

+0

抱歉不清楚。並感謝大家迄今爲止的幫助! – DeniseH

回答

0

這取決於$ term-> name的設置。我通常會使用empty($term->name) ? "Default" : $term->name按鈕名稱應該去的地方。

如果從未設置的名稱,使用$term->name ?? "Default"相反,對於PHP 7

0

的代碼片段如下相當註釋,從而不言自明的。你可能想給它一個去...

<div class="accordion ui-group"> 
    <h3><a href="#">Certification</a></h3> 
    <div class="button-group js-radio-button-group" data-filter-group="certification" > 
     <!-- THIS BUTTON IS OUTSIDE THE LOOP AND HAS THE CLASS: is-checked --> 
     <!-- CLEARLY, ONLY THIS BUTTON WILL DANCE & RESPOND TO THE 'is-checked' RHYTHM --> 
     <!-- COULD IT BE THAT YOU WANT ONLY ONE CHECKED BUTTON WITHIN THE LOOP? --> 
     <!-- WELL; LETS TRY THAT APPROACH AND SEE WHAT WE'D GET... --> 
     <!-- ...BY THE WAY, IF YOU EXPLICITLY NEED THE BUTTON BELOW, PLEASE UNCOMMENT IT... --> 
     <!-- <button class="button is-checked" data-filter="">All</button> --> 

     <?php 
      $term_id  = 69; 
      $taxonomy_name = 'project_category'; 
      $term_children = get_term_children($term_id, $taxonomy_name); 
      // LET'S FIND OUT THE LENGTH OF THE ARRAY SO WE CAN GENERATE A RANDOM NUMBER WITHIN IT'S RANGE 
      $iLength  = count($term_children); 
      // NOW WE GENERATE A RANDOM NUMBER BETWEEN 0 AND $iLength MINUS 1: ($iLength - 1) 
      $iRandNum  = rand(0, ($iLength - 1)); 
      // SO, NOW WE HAVE AN ARBITRARY TARGET INDEX (NUMERIC ARRAY KEY).... 
      // THEN WE JUST MODIFY OUR foreach LOOP BY JUST ADDING THE ITERATION INDEX: $key=> 
      // AFTERWARDS; WE USE THIS $key TO MATCH OUR $iRandNum AND THEN 
      // SET THE ELEMENT AT THAT INDEX TO HAVE THE is-checked CLASS... 
      foreach ($term_children as $key=>$child) { 
       // WE DO OUR CHECK & CREATE THE CLASS ON THE FLY HERE USING TERNARY COMPARISON: 
       $sClass  = ($iRandNum == $key) ? "is-checked" : ""; 
       $term  = get_term_by('id', $child, $taxonomy_name); 
       if($term->count > 0) { 
        // WE JUST ADD THE CLASS ABOVE TO THE BUTTON..... 
        echo "<button class='button {$sClass}' data-filter='{$term->slug}'>{$term->name}</button>"; 
       }; 
      }; 
     ?> 

    </div> 
</div> 
+0

謝謝@Poiz!我覺得我有點不清楚 - 對不起!我想你確實瞭解我需要的大部分。我在上面添加了一些評論,希望能夠澄清一些問題。我確實需要所有作爲選項(只是沒有選中類),這很容易,但我希望頁面上的特定選項在進入頁面時已被選中(並非全部),我還發布了一個鏈接可能會更有意義。 – DeniseH