Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Monster Shortcode Commented

demchakalex Mar 13th, 2018 55 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. <?php
  2.     function monster_shortcode( $atts ){
  3.         // $query may be an already defined variable, so prefix it with 'monster_'
  4.         $monster_query = new WP_Query( array(
  5.             'post_type' => 'monsters',
  6.             'name' => $atts['name'],
  7.         ) );
  8.  
  9.         if( $monster_query->have_posts() ){
  10.             while ( $monster_query->have_posts() ) : $monster_query->the_post();
  11.                 /* We do this so we can just add a new field here, if you need
  12.                  * $monster_weight at some point later, you can just add 'weight'
  13.                  * to this array, and it will do the `get_field( 'weight' )`
  14.                  * function call for you.
  15.                  */
  16.                 $fields = array(
  17.                     'size',
  18.                     'type',
  19.                     'alignment',
  20.                     'armor_class',
  21.                     'ac_type',
  22.                     'hit_points',
  23.                     'hit_die',
  24.                     'speed',
  25.                     'str',
  26.                     'dex',
  27.                     'con',
  28.                     'int',
  29.                     'wis',
  30.                     'cha',
  31.                     'saving_throws',
  32.                     'skills',
  33.                     'damage_adjustments',
  34.                     'senses',
  35.                     'languages',
  36.                     'cr',
  37.                     'actions',
  38.                     'reactions',
  39.                     'characteristics',
  40.                     'enviroments',
  41.                 );
  42.  
  43.                 /* Similar to the fields above, but since these
  44.                  * have alternative functions, we do them here as well
  45.                  */
  46.                 $stats = array(
  47.                     'str',
  48.                     'dex',
  49.                     'con',
  50.                     'int',
  51.                     'wis',
  52.                     'cha',
  53.                 );
  54.  
  55.                 /* Now we loop through each $fields item. This will
  56.                  * define each variable for us as the same field name.
  57.                  * Examples:
  58.                  * $monster_size = get_field( 'size' );
  59.                  * $monster_alignment = get_field( 'alignmnet' );
  60.                  *
  61.                  * If you add 'weight' to the $fields array above,
  62.                  * this loop will automatically define the variable
  63.                  * $monster_weight = get_field( 'weight' ) for you
  64.                  */
  65.                 foreach( $fields as $field ) ${'monster_'.$field}    = get_field( $field );
  66.  
  67.                 /* Similarly, since $stats (or $statb) has something
  68.                  * else to output, we loop through $stats in a similar fashion.
  69.                  * Inside `monster_stats()` we don't need to call `get_field( 'int' )`
  70.                  * again since it's already defined as a variable above, so we just
  71.                  * define it like so:
  72.                  * $monster_intb = monster_stats( $monster_int )
  73.                  */
  74.                 foreach( $stats as $stat )   ${'monster_'.$stat.'b'} = monster_stats( ${'monster_'.$stat} );
  75.                
  76.                 /* We can condense this into one declaration to make it easier to read.
  77.                  * Instead of concatenating a thousand strings, we can just concatenate
  78.                  * the title and description, and then use the string literals inline
  79.                  * since we're using the double-quoted syntax. Alternatively you can
  80.                  * use backticks, but that's not necessarily recommended.
  81.                  */
  82.                 $return = "<h1 class=\"entry-title\">". get_the_title() ."</h1>
  83.                <div class=\"monster-meta\">$monster_size $monster_type, $monster_alignment</div>
  84.                <ul class=\"monster-stat\">
  85.                    <li><label>Armor Class</label> $monster_armor_class ($monster_ac_type)</li>
  86.                    <li><label>Hit Points</label> $monster_hit_points ($monster_hit_die)</li>
  87.                    <li><label>Speed</label> $monster_speed</li>
  88.                </ul>
  89.                <ul class=\"monster-stat abilities\">
  90.                    <li><label>STR</label> $monster_str ($monster_strb)</li>
  91.                    <li><label>DEX</label> $monster_dex ($monster_dexb)</li>
  92.                    <li><label>CON</label> $monster_con ($monster_conb)</li>
  93.                    <li><label>INT</label> $monster_int ($monster_intb)</li>
  94.                    <li><label>WIS</label> $monster_wis ($monster_wisb)</li>
  95.                    <li><label>CHA</label> $monster_cha ($monster_chab)</li>
  96.                 </ul>
  97.                <ul class=\"monster-stat\">
  98.                    <li><label>Saving Throws</label> $monster_saving_throws</li>
  99.                    <li><label>Skills</label> $monster_skills</li>
  100.                    <li><label>Damage Adjustments</label> $monster_damage_adjustments</li>
  101.                    <li><label>Senses</label> $monster_senses</li>
  102.                    <li><label>Langauage</label> $monster_languages</li>
  103.                    <li><label>Challenge Rating</label> $monster_cr</li>
  104.                </ul>
  105.                <p>$monster_traits</p>
  106.                <h4 class=\"monster-label\">Actions</h4><p>$monster_actions</p>
  107.                <h4 class=\"monster-label\">Reactions</h4><p>$monster_reactions</p>
  108.                <h4 class=\"monster-label\">Characteristics</h4><p>$monster_characteristics</p>
  109.                <h4 class=\"monster-label\">Details</h4><p>". get_the_content() ."</p>
  110.                <p><label>Enviroments:</label> $monster_enviroments</p>";
  111.             endwhile;
  112.         }
  113.         return $return;
  114.         wp_reset_postdata();
  115.     }
  116.     add_shortcode( 'monster', 'monster_shortcode' );
  117. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top