【问题标题】:Adding tag input in post form以帖子形式添加标签输入
【发布时间】:2015-10-28 21:18:54
【问题描述】:

所以,我的插件(wordpress)中有以下表单:

if(empty($_POST['post_id'])){

    $post = array(
        'post_content'   => 'Dummy Content', 
        'post_title'     => 'Dummy Title',          
        'post_status'    => 'inherit',      
        'post_type'      => 'post' 
    ); 
    $post_id = wp_insert_post( $post, true );

}else{
    $post_id = $_POST['post_id'];
}
$upload_dir = wp_upload_dir();
$files = $_FILES['files'];
foreach ($files['name'] as $key => $value) {            
    if ($files['name'][$key]) { 
        $filename = rand() . '' . $files['name'][$key];
        $file = array( 

            //Here goes other irrelevant code

        ); 
        $upload_overrides = array( 'test_form' => false );

        $image_path[]     = array(
                    'image_path' => $upload_dir['url'] . '/' . $filename,
                    'meta_key'   => 'upload_image',
                    'meta_value' => $upload_dir['url'] . '/' . $filename,
                    'post_id'    => $post_id
                );

        add_post_meta($post_id, 'upload_image', $upload_dir['url'] . '/' . $filename );
    } 
} 
echo json_encode($image_path);
exit;

上面的函数将图像添加为带有虚拟内容(例如标题/描述等)的帖子。

现在,如果我想更新内容,可以通过以下方式进行:

global $wpdb;
$tags = $_POST['rh_tags'];  
if(!empty($_POST['post_id'])){          
    $data = array(
            'post_title'   => $_POST['title'],
            'post_content' => $_POST['content'],
            'tags_input'     => $tags,
            'post_status'  => 'publish'

      );
      $where = array('ID' => $_POST['post_id']);
      $update =  $wpdb->update($wpdb->prefix . 'posts' , $data, $where, $format = null, $where_format = null );       
}else{      
    $post = array(
        'post_title'     => $_POST['title'], 
        'post_content'   => $_POST['content'],      
        'post_status'    => 'publish', 
        'tags_input'     => $tags,  
        'post_type'      => 'post' 
    ); 
    wp_insert_post($post);      
}
echo 'success';
exit;

这里我添加了'tags_input' => $tags,来添加一个帖子标签。

这是目前的情况:

1.未选择图像: 在输入字段(带有标题、描述和标签的简单输入字段的表单)中,如果我在未选择图像的情况下向标签输入添加内容,则使用标记。

2。已选择图片:如果选择了图片,则使用标签输入,它根本不会创建帖子。

我尝试在第一个函数中添加'tags_input' => $tags,,但这也不起作用。

谁能指出我需要在我的第一个函数中做出哪些改变?

【问题讨论】:

  • 我看不到您在第一个 sn-p 中声明 $image_path 的位置。
  • 我编辑在第一个函数中添加了$image_path
  • enable erros => ini_set('error_reporting', E_ALL);ini_set('display_errors', 1); 并运行脚本,检查是否有任何错误。

标签: php jquery wordpress tags


【解决方案1】:

如果这仍然是一个问题,建议您添加一些var_dump 语句。当后面跟exit; 语句时,这将立即显示对象中的数据。

我想查看每个 if/then 语句中的数据状态以及每个函数中的预期数据。

当然,创建/上传图像之前和之后的数据。然后我希望问题变得可见。

所以一般来说,我会执行以下操作来调试问题。

在不同的地方放置以下调试代码,您还可以在 var_dump 函数中使用逗号分隔符添加不同的关键变量:

var_dump($_POST); 
exit;

您还应该考虑使用更好的变量名,因为代码 调试起来比它需要的更令人困惑。变量名是 即使在单个 if/then 循环中也不一致,例如在 您提到的第二个函数是 $data,然后是 $post。

【讨论】:

    猜你喜欢
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 2014-01-31
    相关资源
    最近更新 更多