2014-08-29 3 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 (Advanced Custom Fields)에 추가했습니다. 나는 각각 quote와 naam_auteur (영어로 작성자 이름)이라고 불리는 몇 개의 하위 필드를 만들었습니다. 이들은 quote이라는 필드에 들어 있습니다.Wordpress randomize quote 함수가 출력을 표시하지 않습니다.

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

내 코드에서 실수를 저질렀 음을 알게 해주셔서 감사드립니다. 내 질문을 편집하여 최종 코드를 추가합니다. 도와 주셔서 감사합니다. –

관련 문제