Skip to content

Instantly share code, notes, and snippets.

@megclaypool
Last active May 18, 2018 15:30
Show Gist options
  • Save megclaypool/bfe47c07a6e5563cd72e81a087226a18 to your computer and use it in GitHub Desktop.
Save megclaypool/bfe47c07a6e5563cd72e81a087226a18 to your computer and use it in GitHub Desktop.
Dump Fields in WP (non-Timber/Twig)

From ACF comes the following:

<?php 

$fields = get_fields();

if( $fields ): ?>
	<ul>
		<?php foreach( $fields as $name => $value ): ?>
			<li><b><?php echo $name; ?></b> <?php echo $value; ?></li>
		<?php endforeach; ?>
	</ul>
<?php endif; ?>

From CSS Tricks we get:

<h3>All Post Meta</h3>

<?php $getPostCustom=get_post_custom(); // Get all the data ?>

<?php
    foreach($getPostCustom as $name=>$value) {

        echo "<strong>".$name."</strong>"."  =>  ";

        foreach($value as $nameAr=>$valueAr) {
                echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                echo $nameAr."  =>  ";
                echo var_dump($valueAr);
        }

        echo "<br /><br />";

    }
?>

My Improvements on the CSS Tricks code

This outputs arrays as preformatted arrays so I can actually see and use their info!!!

<h3>All Post Meta</h3>

<?php $getPostCustom=get_post_custom(); // Get all the data ?>

<?php

$fields = get_fields();

if( $fields ): ?>
	<ul>
		<?php foreach( $fields as $name => $value ):
      if (gettype($value) !== 'array') {?>
			<li><b><?php echo $name; ?></b> <?php echo $value; ?></li>
		<?php } else { ?>
      <li><b><?php echo $name; ?></b> <pre><?php print_r($value); ?></pre></li>
    <?php } endforeach; ?>
	</ul>
<?php endif; ?>


<?php
    foreach($getPostCustom as $name=>$value) {

        echo "<strong>".$name."</strong>"."  =>  ";

        foreach($value as $nameAr=>$valueAr) {
                echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                echo $nameAr."  =>  ";
                echo var_dump($valueAr);
        }

        echo "<br /><br />";

    }
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment