如何在unctions.php中获取当前帖子ID

时间:2014-10-28 作者:Kedar B

这是我的密码functions.php 文件

function getcity(){
    global $wpdb;
    if($_POST[\'state\']) {
        $id=$_POST[\'state\'];
        $postid=$_POST[\'post_id\'];
        $district = esc_html(get_post_meta($postid , \'district \', true));
        $result=$wpdb->get_results("SELECT * FROM districts WHERE state_id=\'$id\'");
        foreach($result as $row) {
            $district_name=$row->district_name;
            $district_id= $row->district_id;
            ?><option value="<?php echo $district_id; ?>" <?php if($district == $district_id){echo \'selected="selected"\';} ?>>
                <?php echo $district_name; ?>
            </option><?php
        }
    }
}
add_action("wp_ajax_nopriv_getcity", "getcity");
add_action("wp_ajax_getcity", "getcity");
这是我的ajax请求代码

$(\'#state\').on(\'change\', function() {

  var state  = $(\'#state\').val();
   var post_id = $(\'#post_id\').val();

  $.ajax
  ({
    type : "POST",
    url  : "http://plotsup.com/plotsup_plot/wp-admin/admin-ajax.php",
    data :{\'action\' : \'getcity\', \'state\' : state, \'post_id\': post_id},
    success: function(html) {

      $("#district").removeAttr("disabled");
      $("#district").html(html);
    }

  });
 });
这是模板文件中的“我的代码”

 <select name="state"  id="state"  class="select-submit2" >
          <option  value="" >Select state</option>

                <?php 

                $property_state = get_post_meta($edit_id, \'state\', true);
                $result=$wpdb->get_results("select * from states");
                 foreach($result as $row) {
                  $state_id=$row->state_id;
                  $state_name=$row->state_name;     
                 ?>
  <option value="<?php echo $state_id; ?>" <?php if($property_state == $state_id){ echo
 \'selected="selected"\';} ?>><?php echo $state_name; ?></option>
<?php  }
          ?>


            </select>    
   <input type="hidden" name="post_id" value="<?php echo $edit_id;?>" id="post_id"/>
我想要此函数中的当前帖子ID。我怎么才能得到呢?

2 个回复
SO网友:Bordoni

这里您面临一个问题,因为如果此方法是AJAX请求,除非您将其传递给给定ID的JavaScript,否则您无法访问post的ID。

我所说的例子:

var args = {
    action: \'getcity\',
    state: \'RJ\',
    post_id: $(\'#post_id\').val()
};
$.post( ajaxurl, args, function( data ) {
    // the success of the AJAX
});
那你只需要找回它$_POST[\'post_id\') 在您的WordPress操作上。

SO网友:Subharanjan

尝试从post值而不是全局$post获取post\\u id。

function getcity(){
    global $wpdb;
    if($_POST[\'state\']) {
        $id=$_POST[\'state\'];
        $postid = $_POST[\'post_id\'];
        $district = esc_html(get_post_meta($postid , \'district \', true));
        $result=$wpdb->get_results("SELECT * FROM districts WHERE state_id=\'$id\'");
        foreach($result as $row) {
            $district_name=$row->district_name;
            $district_id= $row->district_id;
            ?><option value="<?php echo $district_id; ?>" <?php if($district == $district_id){echo \'selected="selected"\';} ?>>
                <?php echo $district_name; ?>
            </option><?php
        }
    }
}
add_action("wp_ajax_nopriv_getcity", "getcity");
add_action("wp_ajax_getcity", "getcity");
<小时>
<div class="container">
    <select name="state" id="state" class="select-submit2">
        <option value="">Select state</option>
        <?php
        $property_state = get_post_meta( $edit_id, \'state\', true );
        $result         = $wpdb->get_results( "select * from states" );
        foreach ( $result as $row ) {
            $state_id   = $row->state_id;
            $state_name = $row->state_name;
            ?>
            <option value="<?php echo $state_id; ?>" <?php if ( $property_state == $state_id ) {
                echo
                \'selected="selected"\';
            } ?>><?php echo $state_name; ?></option>
        <?php
        }
        ?>
    </select>
    <input type="hidden" name="post_id" value="<?php echo $edit_id; ?>" class="post_id" />
</div>



$(\'#state\').on(\'change\', function() {

  var state  = $(\'#state\').val();
  var post_id = $(this).siblings(\'.post_id\').val();

  $.ajax
  ({
    type : "POST",
    url  : "http://plotsup.com/plotsup_plot/wp-admin/admin-ajax.php",
    data :{\'action\' : \'getcity\', \'state\' : state, \'post_id\': post_id},
    success: function(html) {

      $("#district").removeAttr("disabled");
      $("#district").html(html);
    }

  });
 });

结束

相关推荐

允许用户“EDIT_OTHERS_POSTS”只保存,不能发布

我想添加几个编辑器can edit_others_posts, 但我不希望他们能够publish 仅限其他帖子save 单击“提交以供审阅”按钮,即可发布帖子。我该怎么做?编辑:详细解释这一点。目前,我们不允许用户编辑其他帖子only 保存帖子。如果edit_others_post 为用户启用,则他们可以publish 邮报。我的目标工作流程:编辑只能编辑其他挂起的帖子(solved here). </编辑可以保存挂起的帖子but 不发布它。因此,他们可以使用“提交审核”按钮(当帖子处于挂起模式时,