WordPress 的公共帖子表单

发布于 2024-10-14 23:38:16 字数 156 浏览 2 评论 0原文

您好,我正在寻找一种非常简单快速(尽管没有插件)的方法来在页面模板内添加表单,该模板将允许登录用户从 WP-Admin 外部发布。所以基本上该帖子将位于登录的作者之下。

我一直在网上寻找一些教程等,但没有太多运气,许多人似乎想选择插件等。

任何人都可以帮忙吗?谢谢。

Hi I'm looking for a very simple quick (no-plugins though) way to add a form inside a page template that will allow a logged-in user to post from outside WP-Admin. So basically the post will be under the logged-in author.

I have been looking around the web for some tutorials etc but haven't had too much luck and many seem to want to opt for plugins etc.

Can anyone help thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘东忘西忘不掉你 2024-10-21 23:38:16

您可以使用wp_insert_post()函数。

看看这个。例如,您可以将其粘贴到 category.php 中,然后访问类别页面并检查它。但请确保将顶部代码放在 get_header() 函数上方。

 <?php 
    if(isset($_POST['new_post']) == '1') {
        $post_title = $_POST['post_title'];
        $post_category = $_POST['cat'];
        $post_content = $_POST['post_content'];

        $new_post = array(
              'ID' => '',
              'post_author' => $user->ID, 
              'post_category' => array($post_category),
              'post_content' => $post_content, 
              'post_title' => $post_title,
              'post_status' => 'publish'
            );

        $post_id = wp_insert_post($new_post);

        // This will redirect you to the newly created post
        $post = get_post($post_id);
        wp_redirect($post->guid);
    }      
    ?>

--

<!-- this form shows only if user is logged in -->
 <?php if ( is_user_logged_in() ) { ?>

<form method="post" action=""> 
    <input type="text" name="post_title" size="45" id="input-title"/>
    <?php wp_dropdown_categories('orderby=name&hide_empty=0&exclude=1&hierarchical=1'); ?>
    <textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea> 
    <input type="hidden" name="new_post" value="1"/> 
    <input class="subput round" type="submit" name="submit" value="Post"/>
</form>

<?php } ?>

有关更多信息,您应该看看这里 带有表单的 wp_insert_post

好运气

You can use the wp_insert_post() function.

take a look at this. you can paste it in category.php for example and visit a category page and check it. but make sure to put the top code above the get_header() function.

 <?php 
    if(isset($_POST['new_post']) == '1') {
        $post_title = $_POST['post_title'];
        $post_category = $_POST['cat'];
        $post_content = $_POST['post_content'];

        $new_post = array(
              'ID' => '',
              'post_author' => $user->ID, 
              'post_category' => array($post_category),
              'post_content' => $post_content, 
              'post_title' => $post_title,
              'post_status' => 'publish'
            );

        $post_id = wp_insert_post($new_post);

        // This will redirect you to the newly created post
        $post = get_post($post_id);
        wp_redirect($post->guid);
    }      
    ?>

--

<!-- this form shows only if user is logged in -->
 <?php if ( is_user_logged_in() ) { ?>

<form method="post" action=""> 
    <input type="text" name="post_title" size="45" id="input-title"/>
    <?php wp_dropdown_categories('orderby=name&hide_empty=0&exclude=1&hierarchical=1'); ?>
    <textarea rows="5" name="post_content" cols="66" id="text-desc"></textarea> 
    <input type="hidden" name="new_post" value="1"/> 
    <input class="subput round" type="submit" name="submit" value="Post"/>
</form>

<?php } ?>

for more info you should take a look here wp_insert_post with a form

good luck

╄→承喏 2024-10-21 23:38:16

如果您不介意更改主题,您可能想尝试 P2: http://wordpress.org/扩展/主题/p2
这里还有一个实际操作的视频:http://p2theme.com/

P2 允许人们直接从主页,这似乎是您正在寻找的内容。

If you don't mind changing your theme, you might want to try P2: http://wordpress.org/extend/themes/p2
There's also a video of it in action here: http://p2theme.com/

P2 allows people to post straight from the home page, which seems like what your looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文