在PHP按钮代码中设置默认值

时间:2017-09-18 20:37:13

标签: php html wordpress button

我正在尝试设置以下代码中选择的默认按钮:

<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 Categories,其父级为Certificationsid=69)。

现在,默认情况下会选择ALL,但我想指定特定选项的默认值。我知道这应该很容易,但我无法理解!

任何帮助都会很棒。

2 个答案:

答案 0 :(得分:0)

这取决于$ term-&gt;名称的设置方式。我通常会使用按钮名称所在的empty($term->name) ? "Default" : $term->name

如果从未设置过该名称,请使用$term->name ?? "Default"代替PHP 7。

答案 1 :(得分:0)

下面的Code-Snippets是相当评论的,因此可以自我解释......你可能想要试一试......

<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>