如何输出从CUSTOM_TYPE_POST中找到的匹配的元键?

时间:2014-04-02 作者:Steven Mcsorley

在WordPress中播放单人游戏。php页面,其中显示一个配置文件(来自许多其他配置文件),其中包含来自大约10个不同元字段的信息。

不使用标记或类别。。

我想做的是在页面底部,有一个相关的部分,但将其他配置文件(由相同的custom\\u type\\u post制作)与页面上的当前配置文件进行比较,如果10个元字段中的任何一个值与任何其他配置文件匹配,请在页面底部的滑块中显示它们。。。。。。。但是

显示匹配的元字段。

我相信我目前正在显示匹配的个人资料。。。但我试图找到一种方法来显示它们是如何匹配的,方法是显示元字段(几乎像标记云)

IE:配置文件1由-->体重、身高匹配,在另一个配置文件页面上,它可能由一个或两个其他字段匹配SIE:配置文件7由-->位置、身高、团队匹配

这是到目前为止我的代码。

                <!--- RELATED  PLAYERS -->
            <div class="container" style="margin-bottom:40px;">
            <div class="row">
            <div class="col-md-12">
            <h3 style="margin-top:0;padding-bottom:10px;"><span class="glyphicon glyphicon-random"></span> Similar Players</h3>
             <?php 

            $wp_query = new WP_Query(); 
            $args = array(

                \'post_type\' => \'player\',

                \'posts_per_page\' => \'-1\',

                \'category_name\' => \'\',

                \'orderby\' => \'rand\',

                \'meta_query\' => array(
                    \'relation\' => \'OR\',
                array(
                        \'key\' => \'player_position\',
                        \'value\' => $player_position,
                        \'compare\' => \'LIKE\'
                ),
                array(
                        \'key\' => \'player_height\',
                        \'value\' => $player_height,
                        \'compare\' => \'LIKE\'
                )
            )
                );

            $wp_query->query($args);?>

            <div id="carousel-example-generic" class="carousel slide">
            <!-- Wrapper for slides -->
            <div class="carousel-inner"> 
            <?php
            // The Loop

            //add a class to the first result
            $first = true;

            if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();?>


            <?php echo \'<div class="item \'.($first ? \'active\' : \'\').\'">\';?>
            <div class="col-md-2">
            <a href="<?php the_permalink()?>" title="<?php the_title_attribute(); ?>" rel="bookmark">
            <?php if ( has_post_thumbnail() ) {the_post_thumbnail();}?>
            </a>
            </div>
            <div class="col-md-10"> <!-- ///SILDE DATA OUTPUT/ -->

            <?php //////////////////// MATCHED META FIELDS HERE /////////////////////// ?>  
            <?php
            $player_name = get_post_meta($post->ID, \'player_name\', true);
                        if (!$player_name ==\'\')
                        {
                        echo \'<h5>Name: \'.\'<span style="color:grey;">\'.$player_name.\'</span></h5>\';
                        } 
                        ?>
            <?php
            $player_tech_char = get_post_meta($post->ID, \'player_tech_char\', true);
                        if (!$player_tech_char ==\'\')
                        {
                        echo $player_tech_char;
                        } 
            ?>
            </div> <!-- //////////////END SILDE DATA OUTPUT//////////////////////// -->
            </div>
            <?php
            if ($first) {
                $first = false;
                        }
            ?>


            <?php endwhile;endif;?>

            </div>



            <!-- Controls -->
            <!-- 
            <a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
            <span class="icon-prev"></span>
            </a>
            -->
            <a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
            <span class="icon-next"></span>
            </a>

            </div>
            </div>
            </div>

            <!-- END OF OTHER PLAYERS -->
简而言之

$matchedCustomGetPostMeta =  get_post_meta($post->ID, "MATCHED_KEY_FROM_WP_QUERY->have_posts");
更长版本和完整代码

函数创建自定义帖子类型和自定义字段

            <?php
            /**
            *Creates ADD Player post type 
            */
            add_action(\'init\', \'player_register_post_type\');

            function player_register_post_type() {
                register_post_type(\'player\', array(
                    \'labels\' => array(
                        \'name\' => \'Player\',
                        \'singular_name\' => \'Player\',
                        \'add_new\' => \'Add New Player Profile\',
                        \'edit_item\' => \'Edit Player Profile\',
                        \'new_item\' => \'New Player Profilee\',
                        \'view_item\' => \'View Player Profile\',
                        \'search_items\' => \'Search Player Profile\',
                        \'not_found\' => \'No Player Profiles found\',
                        \'not_found_in_trash\' => \'No Player Profiles found in Trash\'
                    ),
                    \'public\' => true,
                    \'show_ui\' => true,
                    \'rewrite\' => true,
                    \'capability_type\' => \'post\',
                    \'has_archive\' => true,
                    \'supports\' => array(
                        \'title\',
                        \'excerpt\',
                        \'comments\',
                        \'thumbnail\',
                        \'editor\',
                        \'author\',
                        \'page-attributes\'
                    ),
                    \'taxonomies\' => array(\'category\', \'post_tag\') // this is IMPORTANT
                ));
            }

            /**
             * Add player custom fields
             */
            function add_player_meta_boxes() {
              add_meta_box("player_contact_meta", "Player Details", "add_player_details_player_meta_box", "player", "normal", "low");
            }
            function add_player_details_player_meta_box()
            {

            global $post;
              $custom = get_post_custom( $post->ID );
             ?>
            <style>.width99 {width:99%;}</style>
            <p>
            <label>Player Current Team</label><br />
            <input type="text" name="player_current_team" value="<?= @$custom["player_current_team"][0] ?>" class="width99" />
            </p>
            <p>
            <label>Player Name</label><br />
            <input type="text" name="player_name" value="<?= @$custom["player_name"][0] ?>" class="width99" />
            </p>
            <p>
            <label>Player Position:</label><br />
            <input type="text" name="player_position" value="<?= @$custom["player_position"][0] ?>" class="width99" />
            </p>
            <p>
            <label>Player Age (enter Date of Birth from Date Picker)</label><br />
            <input type="text" id="player_dob" name="player_dob" value="<?= @$custom["player_dob"][0] ?>" class="width99" />
            <script type="text/javascript">
            jQuery(document).ready(function() {
            jQuery(\'#player_dob\').datepicker({
            dateFormat : \'dd-mm-yy\', defaultDate: -11799
            });
            });
            </script>
            </p>
            <p>
            <label>Player Height:</label><br />
            <input type="text" name="player_height" value="<?= @$custom["player_height"][0] ?>" class="width99" />
            </p>
            <p>
            <label>Player Weight:</label><br />
            <input type="text" name="player_weight" value="<?= @$custom["player_weight"][0] ?>" class="width99" />
            </p>
            <p>
            <label>Player Laterality:</label><br />
            <input type="text" name="player_lat" value="<?= @$custom["player_lat"][0] ?>" class="width99" />
            </p>
            <p>
            <label>Player Domain Legs:</label><br />
            <input type="text" name="player_dom_leg" value="<?= @$custom["player_dom_leg"][0] ?>" class="width99" />
            </p>
            <p>
            <label>Player Standing Number:</label><br />
            <input type="text" name="player_stand" value="<?= @$custom["player_stand"][0] ?>" class="width99" />
            </p>
            <p>
            <label>Player Body Mass Index:</label><br />
            <input type="text" name="player_bmi" value="<?= @$custom["player_bmi"][0] ?>" class="width99" />
            </p>
            <p>
            <label>Player Medical Warnings:</label><br />
            <input type="text" name="player_meds" value="<?= @$custom["player_meds"][0] ?>" class="width99" />
            </p>
            <p>
            <label>Player Nationality:</label><br />
            <input type="text" name="player_nat" value="<?= @$custom["player_nat"][0] ?>" class="width99" />
            </p>
            <p>
            <label>Player European passport:</label><br />
            <input type="text" name="player_eu" value="<?= @$custom["player_eu"][0] ?>" class="width99" />
            </p>
            <p>
            <label>Player Last Team:</label><br />
            <input type="text" name="player_last" value="<?= @$custom["player_last"][0] ?>" class="width99" />
            </p>
            <p>
            <label>Technical Characteristics</label><br />
            <textarea rows="5" name="player_tech_char" class="width99"><?= @$custom["player_tech_char"][0] ?></textarea>
            </p>
            <p>
            <label>Video Links</label><br />
            <textarea rows="5" name="player_vid_link" class="width99"><?= @$custom["player_vid_link"][0] ?></textarea>
            </p>
            <p>
            <label>MEDICALLY AND PSYCHOLOGICAL CHARACTERISTICS</label><br />
            <textarea rows="5" name="player_med_char" class="width99"><?= @$custom["player_med_char"][0] ?></textarea>
            </p>
            <?php
            }
            /**
            * Get Age from DOB ( datepicker)
             */
            function get_age( $birthdayIn )
            {
            $now      = new DateTime();
            $birthday = new DateTime($birthdayIn);
            $interval = $now->diff($birthday);
            $playerAge =  $interval->format(\'%y\'); // age
            return $playerAge;
            }

            // manipulate $_POST
            $birth_date1 = get_age( $_POST["player_dob"] );
            $_POST["player_dob"] = $birth_date1;

            /**
             * Save custom field data when creating/updating posts
             */
            function save_player_custom_fields(){
              global $post;

              if ( $post )
              {
              update_post_meta($post->ID, "player_current_team", @$_POST["player_current_team"]);
              update_post_meta($post->ID, "player_name", @$_POST["player_name"]);
              update_post_meta($post->ID, "player_position", @$_POST["player_position"]);
              update_post_meta($post->ID, "player_dob", @$_POST["player_dob"]);
              update_post_meta($post->ID, "player_height", @$_POST["player_height"]);
              update_post_meta($post->ID, "player_weight", @$_POST["player_weight"]);
              update_post_meta($post->ID, "player_lat", @$_POST["player_lat"]);
              update_post_meta($post->ID, "player_dom_leg", @$_POST["player_dom_leg"]);
              update_post_meta($post->ID, "player_stand", @$_POST["player_stand"]);
              update_post_meta($post->ID, "player_bmi", @$_POST["player_bmi"]);
              update_post_meta($post->ID, "player_meds", @$_POST["player_meds"]);
              update_post_meta($post->ID, "player_nat", @$_POST["player_nat"]);
              update_post_meta($post->ID, "player_eu", @$_POST["player_eu"]);
              update_post_meta($post->ID, "player_last", @$_POST["player_last"]);
              update_post_meta($post->ID, "player_tech_char", @$_POST["player_tech_char"]);
              update_post_meta($post->ID, "player_tech_char", @$_POST["player_tech_char"]);
              update_post_meta($post->ID, "player_vid_link", @$_POST["player_vid_link"]);
              update_post_meta($post->ID, "player_med_char", @$_POST["player_med_char"]);
              }
            }
            add_action( \'admin_init\', \'add_player_meta_boxes\' );
            add_action( \'save_post\', \'save_player_custom_fields\' );
            ?>
以及单个自定义帖子类型页面输出的缩短版本

                            <?php 
                            /// Get Meta Key of this Profile Page Currently on  
                            $key_1_value = get_post_meta( get_the_ID(), \'player_position\', true );
                            // check if the custom field has a value
                            if( ! empty( $key_1_value ) ) {
                              echo "<h4>This Page Key :" . $key_1_value."</h4>";
                            } 
                            ?>




                                <div>
                            <h3 style="margin-top:0;"><span class="glyphicon glyphicon-th-list"></span>  INFORMATION </h3>
                            <?php 
                            $player_name = get_post_meta($post->ID, \'player_name\', true);
                            if (!$player_name ==\'\')
                            {
                            echo \'<h4>Name: \'.\'<span style="color:grey;">\'.$player_name.\'</span></h4>\';
                            } 
                            ?>
                            <?php
                            $player_position = get_post_meta($post->ID, \'player_position\', true);
                            if (!$player_position ==\'\')
                            {
                            echo \'<h4>Position: \'.\'<span style="color:grey;">\'.$player_position.\'</span></h4>\';
                            } 
                            ?>
                            <?php
                            $player_dob = get_post_meta($post->ID, \'player_dob\', true);
                            if (!$player_dob ==\'\')
                            {
                            echo \'<h4>Age: \'.\'<span style="color:grey;">\'.$player_dob.\'</span></h4>\';
                            } 
                            ?>
                            <?php
                            $player_height = get_post_meta($post->ID, \'player_height\', true);
                            if (!$player_height ==\'\')
                            {
                            echo \'<h4>Height: \'.\'<span style="color:grey;">\'.$player_height.\'</span></h4>\';
                            } 
                            ?>
                            <?php
                            $player_weight = get_post_meta($post->ID, \'player_weight\', true);
                            if (!$player_weight ==\'\')
                            {
                            echo \'<h4>Weight: \'.\'<span style="color:grey;">\'.$player_weight.\'</span></h4>\';
                            } 
                            ?>
                            <?php
                            $player_lat = get_post_meta($post->ID, \'player_lat\', true);
                            if (!$player_lat ==\'\')
                            {
                            echo \'<h4>Laterality: \'.\'<span style="color:grey;">\'.$player_lat.\'</span></h4>\';
                            } 
                            ?>
                            <?php
                            $player_dom_leg = get_post_meta($post->ID, \'player_dom_leg\', true);
                            if (!$player_dom_leg ==\'\')
                            {
                            echo \'<h4>Domain Legs: \'.\'<span style="color:grey;">\'.$player_dom_leg.\'</span></h4>\';
                            } 
                            ?>
                            <?php
                            $player_stand = get_post_meta($post->ID, \'player_stand\', true);
                            if (!$player_stand ==\'\')
                            {
                            echo \'<h4>Standing Number: \'.\'<span style="color:grey;">\'.$player_stand.\'</span></h4>\';
                            } 
                            ?>
                            <?php
                            $player_bmi = get_post_meta($post->ID, \'player_bmi\', true);
                            if (!$player_bmi ==\'\')
                            {
                            echo \'<h4>Body Mass Index: \'.\'<span style="color:grey;">\'.$player_bmi.\'</span></h4>\';
                            } 
                            ?>
                            <?php
                            $player_meds = get_post_meta($post->ID, \'player_meds\', true);
                            if (!$player_meds ==\'\')
                            {
                            echo \'<h4>Medical Warnings: \'.\'<span style="color:grey;">\'.$player_meds.\'</span></h4>\';
                            } 
                            ?>
                            <?php
                            $player_nat = get_post_meta($post->ID, \'player_nat\', true);
                            if (!$player_nat ==\'\')
                            {
                            echo \'<h4>Nationality: \'.\'<span style="color:grey;">\'.$player_nat.\'</span></h4>\';
                            } 
                            ?>
                            <?php
                            $player_eu = get_post_meta($post->ID, \'player_eu\', true);
                            if (!$player_eu ==\'\')
                            {
                            echo \'<h4>European passport: \'.\'<span style="color:grey;">\'.$player_eu.\'</span></h4>\';
                            } 
                            ?>
                            <?php
                            $player_last = get_post_meta($post->ID, \'player_last\', true);
                            if (!$player_last ==\'\')
                            {
                            echo \'<h4>Last Team: \'.\'<span style="color:grey;">\'.$player_last.\'</span></h4>\';
                            } 
                            ?>
                            </div>
                            <?php endwhile; ?>          
                            <?php endif; ?>
                            <?php wp_reset_query(); ?>
                            </div>


                            <!-- //

                            //Slider with related profiles

                            -->
                                        <?php 

                                        $wp_query = new WP_Query(); 
                                        $args = array(

                                            \'post_type\' => \'player\',

                                            \'posts_per_page\' => \'-1\',

                                            \'category_name\' => \'\',

                                            \'orderby\' => \'rand\',

                                            \'meta_query\' => array(
                                                \'relation\' => \'OR\',
                                            array(
                                                    \'key\' => \'player_position\',
                                                    \'value\' => $player_position,
                                                    \'compare\' => \'LIKE\'
                                            ),
                                            array(
                                                    \'key\' => \'player_height\',
                                                    \'value\' => $player_height,
                                                    \'compare\' => \'LIKE\'
                                            )
                                        )
                                            );

                                        $wp_query->query($args);?>

                            <?php
                            // The Loop
                            $first = true;
                            if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();?>


                            <?php echo \'<div class="item \'.($first ? \'active\' : \'\').\'">\';?>
                            <div class="col-md-3">
                             <a href="<?php the_permalink()?>" title="<?php the_title_attribute(); ?>" rel="bookmark">
                            <?php if ( has_post_thumbnail() ) {the_post_thumbnail();}?>

                            </a>
                            </div>
                              <div class="col-md-3"> <!-- Slide data output -->

                                    <?php 

                            //// COMPARE THE PAGE META KEY WITH OTHER POSTS IF IT MATCHES THEN STYLE
                            $key_2_value = get_post_meta( get_the_ID(), \'player_position\', true );
                            // check if the custom field has a value
                            if(( ! empty( $key_2_value ) )  && ($key_2_value == $key_1_value)) {
                              //echo "<h4>Matched by :" . $key_1_value."</h4>";
                              $classH6_style =\'style="color:deeppink;font-weight:bold;"\';
                              $classSpan_style =\'style="color:dodgerblue;font-weight:bold;text-decoration:underline;"\';
                            } 
                            else
                            {
                                //echo "No Match For";
                                $classH6_style=\'\';
                                $classSpan_style = \'style="color:grey;"\';
                            }
                            ?>
                            <?php 




                            $player_name = get_post_meta($post->ID, \'player_name\', true);
                                        if (!$player_name ==\'\')
                                        {
                                        echo \'<h6>Name: \'.\'<span style="color:grey;">\'.$player_name.\'</span></h6>\';
                                        } 

                            $player_position = get_post_meta($post->ID, \'player_position\', true);
                                        if (!$player_position ==\'\')
                                        {
                                        echo \'<h6 \'.$classH6_style.\'>Position: \'.\'<span \'.$classSpan_style.\'>\'.$player_position.\'</span></h6>\';
                                        } 

                            $player_dob = get_post_meta($post->ID, \'player_dob\', true);
                                        if (!$player_dob ==\'\')
                                        {
                                        echo \'<h6>D.O.B: \'.\'<span style="color:grey;">\'.$player_dob.\'</span></h6>\';
                                        } 

                            $player_height = get_post_meta($post->ID, \'player_height\', true);
                                        if (!$player_height ==\'\')
                                        {
                                        echo \'<h6>Height: \'.\'<span style="color:grey;">\'.$player_height.\'</span></h6>\';
                                        } 
                            ?>


                             </div>
                            </div>
                            <?php
                            if ($first) {
                                        $first = false;
                                    }
                                    ?>


                            <?php endwhile;endif;?>

                            </div>
                            <?php get_footer(); ?>
这部分是我尝试使用多个键进行匹配的部分,如果在e上,则样式不同

                                    <?php 

                            //// COMPARE THE PAGE META KEY WITH OTHER POSTS IF IT MATCHES THEN STYLE
                            $key_2_value = get_post_meta( get_the_ID(), \'player_position\', true );
                            // check if the custom field has a value
                            if(( ! empty( $key_2_value ) )  && ($key_2_value == $key_1_value)) {
                              //echo "<h4>Matched by :" . $key_1_value."</h4>";
                              $classH6_style =\'style="color:deeppink;font-weight:bold;"\';
                              $classSpan_style =\'style="color:dodgerblue;font-weight:bold;text-decoration:underline;"\';
                            } 
                            else
                            {
                                //echo "No Match For";
                                $classH6_style=\'\';
                                $classSpan_style = \'style="color:grey;"\';
                            }
                            ?>

1 个回复
SO网友:kraftner

让我看看我是否正确理解了你的问题:

您拥有此页面所属玩家的元值。然后在循环中,您拥有每个匹配玩家的相应元值。您只需比较它们:

if($player_position == get_post_meta($post->ID, \'player_position\', true)){
    // This player is on the same position as the one this player page belongs to
}
如果你有很多元值,你应该避免写很多If语句,所以最好使用这个:

$meta_values_to_compare = array(\'player_position\',\'player_height\');

$matched_meta= array();
foreach ($meta_values_to_compare as $value) {
    if($$value == get_post_meta($post->ID, $value, true)){
         $matched_meta[]=$value;
    }
}
然后,您可以使用此数组,例如,在元素上设置类或您需要执行的任何操作。

结束

相关推荐