2014-08-29 21 views
0
<?php 
    $rows = get_field('quote'); 
    //$rows now contains all the quotes that have been added. 

    $rand_row = $rows[ array_rand($rows) ]; 
    //Puts all the rows from $rows in an array and randomly selects a single row. 

    $rand_row_quote = $rand_row['quote']; 
    $rand_row_auteur = $rand_row['naam_auteur']; 
    //Gets the values from the subfields. 

    $quote = get_field($rand_row_auteur, $rand_row_quote); 
    //Puts the values in a single variable. 

    echo $quote; 
    //Echo's the values of each row. 
?> 

所以我已經將上面的代碼添加到Wordpress中的ACF(高級自定義字段)。我已經做了幾個子字段,我分別稱爲引號和naam_auteur(作者的英文名稱)。它們包含在名爲quote的字段中。Wordpress randomize報價功能不顯示任何輸出

現在當我輸入上面的PHP代碼來顯示我的隨機引用與他們的作者,但它是空白的。

任何想法或建議嗎?


上解決了我的問題的代碼如下位置:

<?php 
     $rows = get_field('quotes'); 
     $row_count = count($rows); 
     $i = rand(0, $row_count - 1); 
    ?> 
    <p>Quotes van de fans:</p> 
    <h2><?php echo $rows[ $i ]['quote']; ?></h2> 
    <h3><?php echo $rows[ $i ]['naam_auteur']; ?></h3> 

回答

1

的ACF功能get_field()只接受一個參數,你傳遞了兩個。另外請確保您傳遞給函數的變量實際上是的字段名稱。

檢索字段作爲變量就像下面這樣:

<?php 

$variable = get_field('field_name'); 

// do something with $variable 

?> 

如果你的領域包含多個子領域,read this manual如何顯示它們。

+0

感謝您的鏈接,結果讓我意識到我在代碼中犯了一個錯誤。我將編輯我的問題以添加最終代碼。謝謝您的幫助。 – 2014-08-29 08:41:54