Wordpress get_post_meta()数组数据无法通过主干js传递

时间:2018-11-09 13:04:25

标签: javascript php wordpress

我添加了一个自定义字段,以使用Metabox获取选定的ID。

    array(

                'name' => __('Select projects', 'x2-backend'),

                'id' => "{$prefix}similar_projects_list",

                'type' => 'select_advanced',

                'multiple' => true,

                'options' => $pages_clients_list,

            ),



$args_clients = array(
    'sort_order' => 'ASC',
    'sort_column' => 'post_title',
    'hierarchical' => 1,
    'exclude' => $post_id,
    'include' => '',
    'meta_key' => '',
    'meta_value' => '',
    'authors' => '',
    'child_of' => 0,
    'parent' => -1,
    'exclude_tree' => '',
    'number' => '',
    'offset' => 0,
    'post_type' => 'room',
    'post_status' => 'publish'
);

$pages_clients = get_pages($args_clients);
foreach ($pages_clients as $pages_client) {
  if ($pages_client->ID !`enter code here`= '') {
    $pages_clients_list[$pages_client->ID] = $pages_client->post_title;
    }
}

我的观点是使用骨干js。下面是我的代码。

<script type="text/template" id="villashome-item-tpl-similar-projects">
    <article class="promosgrid-el withborder with-buttons <?php echo ($sproject->room_bookonlinelink == '' && $sproject->room_enquiryform != 1 ? 'buttonNotActive' : ''); ?> with-grdient with-overlay">

        <h3 class="title">
            <a title="<%= md.name %>" href="<%= md.permalink %>"><%= md.name %></a>
        </h3>
        <h4 class="subtitle"><%= md.secondtext_title %></h4>
        <div class="img-cont">
            <div class="img-hover-overlay">
            <a title="<%= md.seo_title %>" href="<%= md.permalink %>">
                <img itemprop="image" alt="" src="<%= md.image_thumb_desktop %>" />
            </a>
            <a title="<%= md.seo_title %>" href="<%= md.permalink %>">
                <div class="img-hover-text"><?php echo ___('+ more info', 'x2-frontend', 'villa_collection_more_info'); ?></div>
            </a>
            </div>
        </div>

    </article>
</script>

loadmore按钮代码在这里

<?php 
 $IDs = get_post_meta( get_the_id() , 'ct_mb_similar_projects_list', false );
$total_rooms = x2_get_similar_projects($IDs);
// var_dump(count($total_rooms));
if ( count($total_rooms)>3 ) { ?>
        <div id="villas-load-more-div" class="spacer-el loadmore-wrp">
                <a id="villas-load-more" class="btns load-more btn-stroke scrollto-action loadmore-gallery-action" href="#"><?php echo ___('Load more X2 Clients', 'x2-frontend', 'gallery_loadmorephotos_btn'); ?></a>
        </div>
<?php } ?>

此按钮调用我的script.js文件中的以下代码

if($('.homepage-villas-similar-projects').length>0){
    // $.stellar('refresh');
    $('.homepage-villas-similar-projects').easyLoad2({
        'tpl_selector':'#villashome-item-tpl-similar-projects',
        'loadstep': 3,
        'getDataType':'getsimilarprojects',
        'offset': 3,
    });
}

通过此脚本,通过easyload2函数在我的plugin.js中调用getsimilarprojects函数。

easyload2函数在我的plugin.js上

    $.fn.easyLoad2 = function (options) {
        var el = $(this);
        var settings = $.extend({
            'tpl_selector': '#blogpost-item-tpl',
            'loadstep': 4,
            'loadbtn': '.load-more',
            'getDataType': 'getposts',
            'offset': 0
        }, options);
        var el_ = $(this);
        var jsonurl_ = directory_uri;
        var Model = Backbone.Model.extend();
        var Collection = Backbone.Collection.extend({
          model: Model,
          url: jsonurl_
       });
    var View = Backbone.View.extend({
        el: el_,
        initialize: function () {
            var self = this;
            _.each(this.model, function (v, i, l) {
                this.template = _.template($(settings.tpl_selector).html(), {md: v});
                var d = $(this.template);
                self.$el.append(d);
            }); $.stellar('refresh');
        }
    });
    var func = {
        countData: (settings.offset > 0 ? settings.offset : 0),
        myData: '',
        step: settings.loadstep,
        loadaction: true,
        init: function () {
            var self = this;
            self.scrollTriggers();
            if (settings.offset == 0) {
                self.data();
            }
            $('body').on('loadmoredata', function () {
                self.data();
            });
        },
        scrollTriggers: function () {
            if (this.loadaction === false) {
                return;
            }
            if ($(settings.loadbtn).length === 0) {
                $(window).on("scroll", function () {
                    if (this.loadaction === true) {
                        var scrollHeight = $(document).height();
                        var scrollPosition = $(window).height() + $(window).scrollTop();
                        if ((scrollHeight - scrollPosition) / scrollHeight === 0) {
                            $('body').trigger('loadmoredata');
                        }
                    }
                });
            } else {
                $(settings.loadbtn).on('click', function (e) {
                    e.preventDefault();
                    $('body').trigger('loadmoredata');
                });
            }
        },
        setCountData: function () {
            this.countData += this.myData.length;
        },
        data: function () {
            var self = this;
            var myCollection = new Collection();
            myCollection.url = jsonurl_ + '?' + settings.getDataType + '=' + self.countData + '&items=' + self.step;
// console.log(myCollection.url);
            myCollection.fetch({success: function () {
                    self.myData = myCollection.toJSON();
                    self.setCountData();
                    self.tpl();
                }});
        },
        tpl: function () {
            var self = this;
            if (self.myData.length !== self.step) {
                self.step = self.myData.length;
            }
            if (self.myData.length === 0) {
                self.loadaction = false;
                return;
            }
            if (self.myData[0].count === void 0) {
            } else {
                if (self.countData >= self.myData[0].count) {
                    $(settings.loadbtn).hide();
                }
            }
            var myView = new View({model: self.myData});
            var scrolltoElement = el_.find('li').eq(self.countData - self.step);
            if (self.countData - self.step > 0) {
                setTimeout(function () {
                    $('body').stop(true, true).scrollTo(scrolltoElement, 700, {axis: 'y', offset: {top: 0}, onAfter: function () {
                        }});
                }, 1000);
            }
        }
    };
    func.init();
};

我的getsimilarprojects()在这里

if ( isset( $_REQUEST['getsimilarprojects'] ) ) {
    add_action( 'wp_loaded', 'getsimilarprojects');
}
function getsimilarprojects() {
    header('Content-type: application/json');
    $offset = $_REQUEST['getsimilarprojects'];
    $items = $_REQUEST['items'];
    $IDs = get_post_meta( get_the_id() , 'ct_mb_similar_projects_list', false );
    $args_rooms_ = array(
        'posts_per_page' => -1,
        'post_type' => 'room',
        'posts_per_page' => ($items == 0)? -1: $items,
        'offset' => ($offset == 0) ? 0: $offset,
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'post__in' => $IDs,
    );
    $rooms_query_ = new WP_Query($args_rooms_);
    $ar = array();
    $i = 0;
    while ($rooms_query_->have_posts()) {
        $rooms_query_->the_post();
        $x2postmeta_room = setX2Globals(get_the_ID());
        $ar[$i]['id'] = get_the_ID();
        $ar[$i]['name'] = get_the_title();
        $ar[$i]['permalink'] = get_permalink();
        $ar[$i]['homepageactive'] = (  $x2postmeta_room->room_homepage_dropdown == 1 ? 1 : 0 );
        $ar[$i]['order'] = $x2postmeta_room->room_meta_order;
        $ar[$i]['price'] = $x2postmeta_room->room_price;
        $ar[$i]['feature_title'] = ( $x2postmeta_room->room_feature_title != '' ? $x2postmeta_room->room_feature_title : get_the_title() );
        $ar[$i]['feature_status'] = ( $x2postmeta_room->room_feature_status == 1 ? true : false );
        $ar[$i]['image_thumb_desktop'] = getFeaturedImage(get_post_thumbnail_id(), 'x2_thumb_374x270', 'x2_thumb_374x270');
        $ar[$i]['seo_title_btn'] = $x2postmeta_room->room_seo_title;
        $ar[$i]['count'] = $rooms_query_->found_posts;
        $i++;
    }
    wp_reset_query();
    echo json_encode($ar);
    die();
}

问题是我无法获得'post__in'的$ IDs值。 在本节中不获取get_the_id();值。为什么? &我该如何解决?

0 个答案:

没有答案