【问题标题】:Add tags to when create post in Wordpress Front End在 Wordpress 前端创建帖子时添加标签
【发布时间】:2014-01-10 16:44:33
【问题描述】:

我目前在一个需要从前端创建帖子的网站工作。 这是我从 wordpress 网站前端创建自定义帖子的代码。

<?php
   $postTitleError = '';

    if (isset($_POST['submitted']))
     {
      if(trim($_POST['postTitle']) === '')
       {
          $postTitleError = 'Please enter a title.';
          $hasError = true;
       }

    $post_information = array(
                'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
                'post_content' => $_POST['postContent'],
                'post_type' => 'product',
                'post_status' => 'publish'
                        );
    wp_insert_post( $post_information );    
  }
?>

这是我的 HTML 表单:

<form action="" id="primaryPostForm" method="POST">
    <!-- Title -->
    <input class="addTitle" type="text" name="postTitle" id="postTitle" class="required" />
    <!-- Description -->
    <input class="addDescription required" name="postContent" id="postContent" />
    <!-- Submit button -->
    <button type="submit"><?php _e('Add Product', 'framework') ?></button>
</form>

我在页面上显示了我的所有标签,但我不知道如何将它们分配到帖子中。 目前我可以使用我的代码创建帖子,但我只需要在我的帖子中添加标签。

有什么想法吗?

【问题讨论】:

    标签: php html wordpress tags custom-fields


    【解决方案1】:

    您是否尝试将以下内容添加到您的 $post_information = array():

    'tags_input' => 数组('tag,tag1,tag2');

    所以你的代码应该是这样的:

    $post_information = array( 
          'post_title' => wp_strip_all_tags( $_POST['postTitle'] ),
          'post_content' => $_POST['postContent'],
          'post_type' => 'product',
          'post_status' => 'publish',
          'tags_input' => array('tag,tag1,tag2')
    );
    

    这假设您已在自定义帖子类型中启用标签。

    参考:

    • http://www.templatemonster.com/help/wordpress-how-to-enable-and-output-post-tags-for-custom-post-types.html
    • http://stackoverflow.com/questions/10434686/wordpress-wp-insert-post-not-inserting-tags
    • http://stackoverflow.com/questions/12267422/update-and-add-tags-post-on-save-post-using-wp-update-post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-14
      • 1970-01-01
      • 2014-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-23
      • 1970-01-01
      相关资源
      最近更新 更多