WordPress统计每个父自定义帖子类型的子帖子,并在用于Google Maps jQuery插件的json数组中使用

时间:2013-11-07 作者:jonhodson1984

因此,我已经找了几天了,但在任何地方都找不到解决这个问题的方法。这是一个客户相当定制的请求。。。我正在使用jQuery谷歌地图插件为管理员使用带有纬度和长坐标的自定义元框创建的每个帖子(区域)添加标记。

每个标记都有一个与之关联的工具提示。在工具提示中,我得到了一些文本、用于创建标记本身的每个帖子的标题以及帖子(区域)的永久链接。

但我想显示每个帖子(区域)的所有子帖子(交易)的数量。

我对(区域)帖子本身进行了处理,但无法让它们显示在我的地图工具提示中。

这是我到目前为止所拥有的代码,我认为我离解决方案还很远,但我的php技能仍处于初级水平,因此非常感谢您的帮助。

<script type="text/javascript">
    $(document).ready(function(){
        var myMarkers = {"markers": [
            <?php $items3 = new wp_Query( array(
                \'post_type\' => \'area\',
                \'post_status\' => \'publish\',
                \'posts_per_page\' => -1,
                \'orderby\' => \'date\',
                \'order\' => \'desc\',
            ) );
            $post_id = $post->ID; //or somehow get the current post ID.
            $parent_id = get_post_ancestors($post_id);
            $parent = get_post_type( $parent_id );
            $args = array(
                \'post_type\' => \'deal\',
                \'post_status\' => \'published\',
                \'post_parent\' => $parent,
                \'numberposts\' => -1
            );
            $this_page_id=$wp_query->post->ID;
            $num = count( get_posts( $args ) );
            while ( $items3->have_posts() ) : $items3->the_post(); ?>
                        {"latitude": "<?php echo get_post_meta($post->ID, \'_arealat\', true); ?>", "longitude": "<?php echo get_post_meta($post->ID, \'_arealong\', true); ?>", "icon": "/wp-content/themes/localdoubleglazingdeals/assets/img/design/logo-icon.png", "baloon_text": \'We have <span><?php echo $num ?></span> deals in <strong><?php the_title(); ?></strong><br/><a href="<?php the_permalink(); ?>">View deals</a>\'},
            <?php endwhile; ?>
            ]
                };
    $("#map").mapmarker({
        zoom    : 7,
        center  : \'Liverpool\',
        markers : myMarkers
    });
});

这是。。。

<?php echo $num ?>
我被卡住的部分。

1 个回复
最合适的回答,由SO网友:gmazzap 整理而成

首先,您应该始终将php与javascript分开。一般来说,将它们混合在一起是一种不好的做法,特别是对于WordPress,它还迫使您以内联方式添加javascript,而不是使用WordPress的正确方式:使用wp_enqueue_script.当您需要将变量从PHP传递到javascript时,WordPress的正确方法是使用wp_localize_script.

因此,您的javascript代码应该如下所示:

jQuery(document).ready(function($){
  var myMarkers = {"markers": [ my_map_data.markers ] };
  $("#map").mapmarker({
    zoom : 7,
    center : \'Liverpool\',
    markers : myMarkers
  });
});
可读,不是吗?还要注意我是如何使用jQuery无冲突包装器的,这是因为我希望您正确地将jQuery排队(即使用wp_enqueue_script 加载WP embedded jQuery版本),如果是,WordPress将在无冲突模式下使用jQuery,因此要使其正常工作,您应该使用no confict wrappers.

现在您已经有了上面的代码,应该将其保存在一个文件中,并将其命名为my map。js并保存在您的主题(子)文件夹中(如果您正在开发插件,则保存在插件文件夹中)。

假设您将该文件保存在主题的“js”子文件夹中。现在,您必须包含在正确的模板中。我不知道你在哪里打印地图,我假设这里有一个页面模板,名为\'page-map.php\' 你想在那里包括你的地图,所以你必须在那里包括你的javascript。

在您的functions.php 您应该添加:

add_action(\'wp_enqueue_scripts\', \'add_map_scripts\');
function add_map_scripts() {
   if ( is_page_template(\'page-map.php\') ) {
      $jsurl = get_template_directory_uri() . \'/js/my-map.js\';
      wp_enqueue_script(\'my-map\', $jsurl, array(\'jquery\') );
      wp_localize_script(\'my-map\', \'my_map_data\', get_map_data() );
   }
}
这是在WordPress中添加脚本的正确方法。如果你看起来像广告wp_enqueue_script 我链接的文档,你可以看到我已经为函数添加了第三个参数,它是依赖项数组,无论你在数组中添加了什么脚本,如果还没有包含在页面中,都会被添加,之后还会包含你的脚本。我已经将jQuery包含在依赖关系数组中,这样我确信jQuery将被添加到页面中:如果您手动将jQuery添加到页面中。。。将其移除。你说你使用的插件有自己的js脚本,你应该看看这个脚本的句柄(它是wp_enqueue_script) 并将其添加到依赖项数组中,这样您就可以确保您的脚本是在插件脚本之后添加的。

现在,如果您看到我发布的javascript代码,您会注意到标记数组取自my_map_data.markers 变量但是这个变量是在哪里定义的呢?

答案在wp_localize_script 我在上面的代码中调用了wp_enqueue_script. 如果您看到wp_localize_script 我在上面链接的文档中,您可以看到我正在向脚本传递一个对象,该脚本名为\'my_map_data\' 该对象的内容是get_map_data 作用这个函数还不存在,所以让我们编写它(应该在functions.php):

function get_map_data() {
  // assure we are in right page
  if ( ! is_page_template(\'page-map.php\' ) return;
  $areas = new WP_Query( array(
    \'post_type\' => \'area\', \'post_status\' => \'publish\', \'posts_per_page\' => -1,
    \'orderby\' => \'date\', \'order\' => \'desc\'
  ) );
  if ( ! $areas->have_posts() ) return; // no areas, nothing to do    
  $markers = array();
  while ( $areas->have_posts() ) : $areas->the_post();
    global $post; // current area post object
    $deals_args = array(
      \'post_type\' => \'deal\', \'post_status\' => \'publish\', \'numberposts\' => -1
      \'post_parent\' => $post->post_parent
    );
    $d_num = count( get_posts($deals_args) );
    $ballon_f = \'We have <span>%d</span> deals in <strong>%s</strong>\';
    $ballon_f .= \'<br/><a href="%s">View deals</a>\';
    $marker = array(
      \'latidude\' => get_post_meta( get_the_ID(), \'_arealat\', true),
      \'longitude\' => get_post_meta( get_the_ID(), \'_arealong\', true),
      \'icon\': get_template_directory_uri() . \'/assets/img/design/logo-icon.png\',
      \'baloon_text\' => sprintf($ballon_f, $d_num, get_the_title(), get_permalink() );
    );
    $markers[] = (object) $marker; // collect all markes in the $markes array
  endwhile;
  wp_reset_postdata(); // reset the post data after the custom query
  // return the array of markes to wp_localize_script that will json_encode it and
  // pass it to javascipt
  return array( \'markers\' => $markers );
}
现在,我们的函数将数组返回到wp_localize_script 该函数将此数组转换为JSON格式,并将其作为全局对象(名为“my\\u map\\u data”)传递给javascript。在javascript中使用

{"markers": [ my_map_data.markers ] };
它将包含由创建的对象数组get_map_data 作用

结束

相关推荐

定制wpdb查询-如何按另一个表meta_count对POST进行排序

下面的自定义wpdb查询代码运行良好,但我需要排序依据stt2_meta 桌子meta_count,则, <?php $querystr = \" SELECT ID, post_title FROM $wpdb->posts WHERE post_type = \'post\' AND post_status = \'publish\'