WordPress中如何添加投稿功能

其他教程   发布日期:2023年07月29日   浏览次数:205

这篇文章主要讲解了“WordPress中如何添加投稿功能”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“WordPress中如何添加投稿功能”吧!

一、添加投稿表单

1、首先在当前主题的目录下新建一个php文件,命名为tougao-page.php,然后将page.php中的所有代码复制到tougao-page.php中;

2、删除tougao-page.php开头的所有注释,即 /* 与 */ ,以及它们之间的所有内容;

3、搜索:the_content,可以查找到类似代码<?php the_content(); ?>,将其替换成代码一

如果你在tougao-page.php中找不到

the_content
,那么你可以查找:
get_template_part
,可找到类似代码:<?php get_template_part( 'content', 'page' ); ?>,将content-page.php中的所有代码替换这部分代码即可。再用下面的代码替换<?php the_content(); ?>

代码一:

<?php the_content(); ?>

<!-- 关于表单样式,请自行调整-->
<form class="ludou-tougao" method="post" action="<?php echo $_SERVER["REQUEST_URI"]; $current_user = wp_get_current_user(); ?>">
	<div style="text-align: left; padding-top: 10px;">
		<label for="tougao_authorname">昵称:*</label>
		<input type="text" size="40" value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_login; ?>" id="tougao_authorname" name="tougao_authorname" />
	</div>

	<div style="text-align: left; padding-top: 10px;">
		<label for="tougao_authoremail">E-Mail:*</label>
		<input type="text" size="40" value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_email; ?>" id="tougao_authoremail" name="tougao_authoremail" />
	</div>
					
	<div style="text-align: left; padding-top: 10px;">
		<label for="tougao_authorblog">您的博客:</label>
		<input type="text" size="40" value="<?php if ( 0 != $current_user->ID ) echo $current_user->user_url; ?>" id="tougao_authorblog" name="tougao_authorblog" />
	</div>

	<div style="text-align: left; padding-top: 10px;">
		<label for="tougao_title">文章标题:*</label>
		<input type="text" size="40" value="" id="tougao_title" name="tougao_title" />
	</div>

	<div style="text-align: left; padding-top: 10px;">
		<label for="tougaocategorg">分类:*</label>
		<?php wp_dropdown_categories('hide_empty=0&id=tougaocategorg&show_count=1&hierarchical=1'); ?>
	</div>
					
	<div style="text-align: left; padding-top: 10px;">
		<label style="vertical-align:top" for="tougao_content">文章内容:*</label>
		<textarea rows="15" cols="55" id="tougao_content" name="tougao_content"></textarea>
	</div>
					
	<br clear="all">
	<div style="text-align: center; padding-top: 10px;">
		<input type="hidden" value="send" name="tougao_form" />
		<input type="submit" value="提交" />
		<input type="reset" value="重填" />
	</div>
</form>

二、添加表单处理代码

在tougao-page.php开头处中,将第一个 <?php 改成代码二:

<?php
/**
 * Template Name: tougao
 * 作者:露兜
 * 博客:https://www.ludou.org/
 * 
 * 更新记录
 *  2010年09月09日 :
 *  首个版本发布
 *  
 *  2011年03月17日 :
 *  修正时间戳函数,使用wp函数current_time('timestamp')替代time()
 *  
 *  2011年04月12日 :
 *  修改了wp_die函数调用,使用合适的页面title
 *  
 *  2013年01月30日 :
 *  错误提示,增加点此返回链接
 *  
 *  2013年07月24日 :
 *  去除了post type的限制;已登录用户投稿不用填写昵称、email和博客地址
 *  
 *  2015年03月08日 :
 *  使用date_i18n('U')代替current_time('timestamp')
 */
    
if( isset($_POST['tougao_form']) && $_POST['tougao_form'] == 'send') {
    global $wpdb;
    $current_url = 'http://你的投稿页面地址';   // 注意修改此处的链接地址
    $last_post = $wpdb->get_var("SELECT `post_date` FROM `$wpdb->posts` ORDER BY `post_date` DESC LIMIT 1");
    // 博客当前最新文章发布时间与要投稿的文章至少间隔120秒。
    // 可自行修改时间间隔,修改下面代码中的120即可
    // 相比Cookie来验证两次投稿的时间差,读数据库的方式更加安全
    if ( (date_i18n('U') - strtotime($last_post)) < 120 ) {
        wp_die('您投稿也太勤快了吧,先歇会儿!<a href="https://www.19jp.com">

最后以UTF-8编码保存tougao-page.php,否则中文可能会乱码。然后进入WordPress管理后台

以上就是WordPress中如何添加投稿功能的详细内容,更多关于WordPress中如何添加投稿功能的资料请关注九品源码其它相关文章!