【问题标题】:Get value from Objects in an Array Wordpress从数组 Wordpress 中的对象中获取值
【发布时间】:2014-12-29 11:44:00
【问题描述】:

我正在尝试向 Wordpress 网站添加投票功能,可用的插件不符合我的要求。

我有这个数组,其中包含另一个数组,其中包含 wp_post 对象(这些是投票的用户)我需要做的是将 current_user_ID 与所有 进行比较数组对象中的 post_author ids。我需要这样做,以便可以向已经投票的用户显示不同的内容。只知道如何从数组中的对象中获取 post_author 值 并将其与 current_user_id 进行比较会大有帮助。希望大家能帮帮我,先谢谢了。

这就是我获取数组的方式:

<?php $my_post_meta = get_post_meta($post->ID, 'supporters', false);
            echo print_r($my_post_meta); ?>

这是显示的数组:

 Array ([0] => Array
        (
        [0] => WP_Post Object
            (
                [ID] => 750
                [post_author] => 46
            )

        [1] => WP_Post Object
            (
                [ID] => 749
                [post_author] => 47

            )

        [2] => WP_Post Object
            (
                [ID] => 748
                [post_author] => 1

            )
          ))1            

【问题讨论】:

    标签: php arrays wordpress object


    【解决方案1】:

    试试这个:

     <?php 
    
        $my_post_meta = get_post_meta($post->ID, 'supporters', false);
        $current_user_ID = get_current_user_id();
        foreach( $my_post_meta[0] as $post_object ) {
            if( $post_object->ID == $current_user_ID ) {
                // The current user has voted
            }
        }
    
       ?>
    

    【讨论】:

      猜你喜欢
      • 2016-01-13
      • 2018-06-28
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      • 2017-10-06
      • 2016-09-06
      • 1970-01-01
      相关资源
      最近更新 更多