isaacadams

ACF Fields

Jan 5th, 2021 (edited)
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. Display a field
  2. <p><?php the_field('field_name'); ?></p>
  3.  
  4. Retrieving a field as a variable
  5. <?php
  6. $variable = get_field('field_name');
  7. // do something with $variable
  8. ?>
  9.  
  10. Using conditional statements
  11. <?php
  12. if(get_field('field_name'))
  13. {
  14. echo '<p>' . get_field('field_name') . '</p>';
  15. }
  16. ?>
  17.  
  18. Working with Array values
  19. <?php
  20. $values = get_field('field_name');
  21. if($values)
  22. {
  23. echo '<ul>';
  24. foreach($values as $value)
  25. {
  26. echo '<li>' . $value . '</li>';
  27. }
  28. echo '</ul>';
  29. }
  30. // always good to see exactly what you are working with
  31. var_dump($values);
  32. ?>
  33.  
  34. Working with Images – URL
  35. <img src="<?php the_field('image_test'); ?>" alt="" />
  36.  
  37. Working with Images – ID
  38. <?php $image = wp_get_attachment_image_src(get_field('image_test'), 'full'); ?>
  39. <img src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_field('image_test')) ?>" />
  40.  
  41. Working with the Repeater Field
  42. <?php if( have_rows('repeater_field_name') ): ?>
  43. <ul>
  44. <?php while( have_rows('repeater_field_name') ): the_row(); ?>
  45. <li>sub_field_1 = <?php the_sub_field('sub_field_1'); ?>, sub_field_2 = <?php the_sub_field('sub_field_2'); ?>, etc</li>
  46. <?php
  47. $sub_field_3 = get_sub_field('sub_field_3');
  48. // do something with $sub_field_3
  49. ?>
  50. <?php endwhile; ?>
  51. </ul>
  52. <?php endif; ?>
  53.  
  54. Randomly select a repeater field row
  55. <?php
  56. $rows = get_field('repeater_field_name');
  57. $row_count = count($rows);
  58. $i = rand(0, $row_count - 1);
  59. echo $rows[ $i ]['sub_field_name'];
  60. ?>
  61.  
  62. Getting values from another page
  63. <?php
  64. $other_page = 12;
  65. ?>
  66. <p><?php the_field('field_name', $other_page); ?></p>
  67. <?php
  68. // get variable
  69. $variable = get_field('field_name', $other_page);
  70. // repeater field: note that the_sub_field and get_sub_field don't need a post id parameter
  71. if( have_rows('repeater_field_name', $other_page) ): ?>
  72. <ul>
  73. <?php while( have_rows('repeater_field_name', $other_page) ): the_row(); ?>
  74. <li>sub_field_1 = <?php the_sub_field('sub_field_1'); ?>, sub_field_2 = <?php the_sub_field('sub_field_2'); ?>, etc</li>
  75. <?php
  76. $sub_field_3 = get_sub_field('sub_field_3');
  77. // do something with $sub_field_3
  78. ?>
  79. <?php endwhile; ?>
  80. </ul>
  81. <?php endif; ?>
  82.  
  83. Query posts with ACF values
  84. <?php
  85. $posts = get_posts(array(
  86. 'numberposts' => -1,
  87. 'post_type' => 'event',
  88. 'meta_key' => 'location',
  89. 'meta_value' => 'melbourne'
  90. ));
  91. if($posts)
  92. {
  93. echo '<ul>';
  94. foreach($posts as $post)
  95. {
  96. echo '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></li>';
  97. }
  98. echo '</ul>';
  99. }
  100. ?>
Add Comment
Please, Sign In to add comment