从前台编辑帖子。保存POST_TAG,但不会将其分开

时间:2012-11-09 作者:wellseo

我正在为Wordpress构建CRM主题。所以我在前端发布、编辑和删除帖子。我想我正在寻找一种解决所有问题的好方法。但有一个失败。在索引处有一个“编辑”和“删除”按钮。如果我点击“编辑”,我将进入一个表单。我可以在那里更改post\\u标签。更改将被保存,但单个单词不会分开。输入字段中的所有单词都是一个巨大的标记-我可以在侧栏的小部件中看到它。但我用分隔符。。。如果您有2分钟:

在这里(index.php中),我可以删除一篇帖子:

if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] ) {
   set_query_var( \'postid1\', $_POST[\'postid\'] );     /*  set the "postid" value from the delete button of the post we choose to delete into "postid1" */
   wp_delete_post( get_query_var( \'postid1\'), true );  /* delete the post we choosed */
};
get_header(); ?>
在索引的循环中,您可以找到以下代码段。第一个表单用于编辑,第二个表单用于删除:

<form class="edit" action="<?php echo esc_url( home_url( \'/\' ) ); ?>editieren-formular/" method="post">
    <input type="hidden" name="postid" value="<?php the_ID(); ?>" /> <?php /* get the post ID into "postid" and later pass it to "editieren-formular.php" */ ?>
    <input type="submit" value="Edit" />
</form>
<form class="delete" action="" method="post">
    <input type="hidden" name="postid" value="<?php the_ID(); ?>" /> <?php /* get the post ID into "postid" and later delete the post */ ?>
    <input type="submit"  value="Delete" />
</form>
一切都很好。现在是编辑公式。php是一个站点的页面模板,称为“Editiere Formular”。“editieren formular”是鼻涕虫。

<?php
/*
Template Name: Editieren Formular
*/

if( \'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) &&  $_POST[\'action\'] == "edit_post" && isset($_POST[\'postid\'])) {
    $post_to_edit = array();
    $post_to_edit = get_post($_POST[\'postid\']);

    /* these are the fields that we are editing in the form below. */
    $title = $_POST[\'title\'];
    $description = $_POST[\'description\'];
    $vorname = $_POST[\'vorname\'];
    $name = $_POST[\'name\'];

    /* this code will save the title and description into the post_to_edit array */
    $post_to_edit->post_title = $title;
    $post_to_edit->post_content = $description;

    /* honestly i can\'t really remember why i added this code but it is a must */
    $pid = wp_update_post($post_to_edit);

    /* save taxonomies: post ID, form name, taxonomy name, if it appends(true) or rewrite(false) */
    wp_set_post_terms($pid, array($_POST[\'cat\']),\'firmen\',false);
    wp_set_post_terms($pid, array($_POST[\'post_tags\']),\'post_tag\',false);

    //UPDATE CUSTOM FIELDS WITH THE NEW INFO
    //CHANGE TO YOUR CUSTOM FIELDS AND ADD AS MANY AS YOU NEED

    update_post_meta($pid, \'vorname\', $vorname);
    update_post_meta($pid, \'name\', $name);

    //REDIRECT USER WHERE EVER YOU WANT AFTER DONE EDITING
    wp_redirect( \'http://crm-wordpress-theme.wellseo.de\' );

...

} // END THE IF STATEMENT THAT STARTED THE WHOLE FORM


get_header(); 

$post_to_edit = get_post($_POST[\'postid\']); 
$terms = get_the_terms($post_to_edit->ID, \'firmen\'); 
$tags = strip_tags( get_the_term_list( $post_to_edit->ID, \'post_tag\', \'\', \', \', \'\' ) ); 

<?php $term_name = strip_tags( get_the_term_list( $post_to_edit->ID, \'category\', \'\', \', \', \'\' ) ); ?> <!-- get the category name of this post -->
<?php $term_obj = get_term_by(\'name\', $term_name, \'category\'); ?> <!-- get the current term object -->
<?php $term_id = $term_obj->term_id ;?> <!-- get this post\'s term id -->
<?php $args = array(
    \'selected\' => $term_id,
    \'name\' => \'cat\',
    \'class\' => \'postform\',
    \'tab_index\' => 10,
    \'depth\' => 2,
    \'hierarchical\' => 1,
    \'hide_empty\' => false );  /* array for wp_dropdown_category to display with the current post category selected by default */ ?>

<div id="content" role="main">

<form id="edit_post" name="edit_post" method="post" action="" enctype="multipart/form-data">
    <fieldset name="title">
        <label for="title">Title:</label><br />
        <input type="text" id="title" value="<?php echo $post_to_edit->post_title; ?>" tabindex="5" name="title" />
    </fieldset>

    <fieldset id="category">
        <label for="cat" ><?php wp_dropdown_categories( $args ); ?></label>
    </fieldset>

    <fieldset class="content">
        <label for="description">Discount Description:</label><br />
        <textarea id="description"  tabindex="15" name="description"><?php echo $post_to_edit->post_content; ?></textarea>
    </fieldset>

<!-- BELOW ARE THE CUSTOM FIELDS -->

    <fieldset class="vorname">
        <label for="vorname">Vorname:</label><br />
        <input type="text" value="<?php echo get_post_meta($post_to_edit->ID,\'vorname\', true); ?>" id="vorname" tabindex="20" name="vorname" />
    </fieldset>

    <fieldset class="name">
        <label for="name">Name:</label><br />
        <input type="text" value="<?php echo get_post_meta($post_to_edit->ID,\'name\', true); ?>" id="name" tabindex="20" name="name" />
    </fieldset>

    <fieldset class="tags">
        <label for="post_tags">Additional Keywords (comma separated):</label><br />
        <input type="text" value="<?php echo $tags; ?>" tabindex="35" name="post_tags" id="post_tags" />
    </fieldset>

    <fieldset class="submit">
        <input type="submit" value="Speichern" tabindex="40" id="submit" name="submit" />
    </fieldset>
    <input type="hidden" name="postid" value="<?php echo $post_to_edit->ID; ?>" />
    <input type="hidden" name="action" value="edit_post" />
    <input type="hidden" name="change_cat" value="" />
    <?php // wp_nonce_field( \'new-post\' ); ?>
</form>
</div><!-- #content -->

<?php get_sidebar(); ?>
<?php get_sidebar(\'two\'); ?>
<?php get_footer(); ?>

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

好的,我自己找到了一个解决方案。过去,我使用

wp_set_post_terms($pid, array($_POST[\'post_tags\']),\'post_tag\',false);
现在,我将post\\u标记保存为

wp_set_post_tags($pid, $_POST[\'post_tags\']);
这是可行的。这种方法与我从前端发布新帖子的方法相同。只改变了这个,其他什么都没有。

SO网友:Mario Peshev

当您尝试插入时$_POST[\'post_tags\'] 它实际上是保存一个长字符串。您所谓的“分离”只是从所有标记中创建一个字符串,其中包含一些分隔符:

$tags = strip_tags( get_the_term_list( $post_to_edit->ID, \'post_tag\', \'\', \', \', \'\' ) ); 
但最终的结果是一个简单的字符串,因此在调用之前需要拆分该字符串wp_set_post_terms - 将其分解为一个数组,然后将得到一个带有标记的数组,而不是逗号分隔的字符串。

结束

相关推荐

如何更改文件EDIT-TAGS.php生成的类别中显示的列表项数量

在@$$中,只看到类别中的20个项目一直是一件痛苦的事。如果我有30个页面,它会被分为两页,我讨厌这样。如果WordPress喜欢的话。所以问题是,列出所有类别,无论数字是多少。。。如果我有200个,把它们都列出来,不要分页。我检查编辑标签。php,但找不到类似的“show\\u item=20”问题是,如何制作编辑标签。php show 9999 item(或unlimited)有一个插件可以自定义它,而无需破解WP核心文件。我认为这里正在发生奇迹,但将20改为999不会改变任何事情add_screen