Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function monster_shortcode( $atts ){
- // $query may be an already defined variable, so prefix it with 'monster_'
- $monster_query = new WP_Query( array(
- 'post_type' => 'monsters',
- 'name' => $atts['name'],
- ) );
- if( $monster_query->have_posts() ){
- while ( $monster_query->have_posts() ) : $monster_query->the_post();
- /* We do this so we can just add a new field here, if you need
- * $monster_weight at some point later, you can just add 'weight'
- * to this array, and it will do the `get_field( 'weight' )`
- * function call for you.
- */
- $fields = array(
- 'size',
- 'type',
- 'alignment',
- 'armor_class',
- 'ac_type',
- 'hit_points',
- 'hit_die',
- 'speed',
- 'str',
- 'dex',
- 'con',
- 'int',
- 'wis',
- 'cha',
- 'saving_throws',
- 'skills',
- 'damage_adjustments',
- 'senses',
- 'languages',
- 'cr',
- 'actions',
- 'reactions',
- 'characteristics',
- 'enviroments',
- );
- /* Similar to the fields above, but since these
- * have alternative functions, we do them here as well
- */
- $stats = array(
- 'str',
- 'dex',
- 'con',
- 'int',
- 'wis',
- 'cha',
- );
- /* Now we loop through each $fields item. This will
- * define each variable for us as the same field name.
- * Examples:
- * $monster_size = get_field( 'size' );
- * $monster_alignment = get_field( 'alignmnet' );
- *
- * If you add 'weight' to the $fields array above,
- * this loop will automatically define the variable
- * $monster_weight = get_field( 'weight' ) for you
- */
- foreach( $fields as $field ) ${'monster_'.$field} = get_field( $field );
- /* Similarly, since $stats (or $statb) has something
- * else to output, we loop through $stats in a similar fashion.
- * Inside `monster_stats()` we don't need to call `get_field( 'int' )`
- * again since it's already defined as a variable above, so we just
- * define it like so:
- * $monster_intb = monster_stats( $monster_int )
- */
- foreach( $stats as $stat ) ${'monster_'.$stat.'b'} = monster_stats( ${'monster_'.$stat} );
- /* We can condense this into one declaration to make it easier to read.
- * Instead of concatenating a thousand strings, we can just concatenate
- * the title and description, and then use the string literals inline
- * since we're using the double-quoted syntax. Alternatively you can
- * use backticks, but that's not necessarily recommended.
- */
- $return = "<h1 class=\"entry-title\">". get_the_title() ."</h1>
- <div class=\"monster-meta\">$monster_size $monster_type, $monster_alignment</div>
- <ul class=\"monster-stat\">
- <li><label>Armor Class</label> $monster_armor_class ($monster_ac_type)</li>
- <li><label>Hit Points</label> $monster_hit_points ($monster_hit_die)</li>
- <li><label>Speed</label> $monster_speed</li>
- </ul>
- <ul class=\"monster-stat abilities\">
- <li><label>STR</label> $monster_str ($monster_strb)</li>
- <li><label>DEX</label> $monster_dex ($monster_dexb)</li>
- <li><label>CON</label> $monster_con ($monster_conb)</li>
- <li><label>INT</label> $monster_int ($monster_intb)</li>
- <li><label>WIS</label> $monster_wis ($monster_wisb)</li>
- <li><label>CHA</label> $monster_cha ($monster_chab)</li>
- </ul>
- <ul class=\"monster-stat\">
- <li><label>Saving Throws</label> $monster_saving_throws</li>
- <li><label>Skills</label> $monster_skills</li>
- <li><label>Damage Adjustments</label> $monster_damage_adjustments</li>
- <li><label>Senses</label> $monster_senses</li>
- <li><label>Langauage</label> $monster_languages</li>
- <li><label>Challenge Rating</label> $monster_cr</li>
- </ul>
- <p>$monster_traits</p>
- <h4 class=\"monster-label\">Actions</h4><p>$monster_actions</p>
- <h4 class=\"monster-label\">Reactions</h4><p>$monster_reactions</p>
- <h4 class=\"monster-label\">Characteristics</h4><p>$monster_characteristics</p>
- <h4 class=\"monster-label\">Details</h4><p>". get_the_content() ."</p>
- <p><label>Enviroments:</label> $monster_enviroments</p>";
- endwhile;
- }
- return $return;
- wp_reset_postdata();
- }
- add_shortcode( 'monster', 'monster_shortcode' );
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement