WordPress如何给自定义文章类型实现问答功能?详细教程来了

火币

火币

火币是全球三大交易所之一,虚拟货币交易平台安全可靠,注册领取新人礼包!

很久以前就有位资深站长问我能不能做个问答插件,当时有思考过如何实现,但因种种原因没有做。近日看到某主题网站出了问答功能,虽然功能不是很强大(第一版,后续可能会增强),但展现还行。借此机会讲讲个人认为实现问答功能的两种实现办法。

图片[1]-WordPress如何给自定义文章类型实现问答功能?详细教程来了-玩转网 | 902D.Com

WordPress自定义文章类型实现问答效果
wordpress有一个可以自定义文章类型的功能,仔细想想,一个问答不也相当于一篇文章吗?有标题,有内容,有评论,有点赞。只是很多文章用到的内容没用上,显得十分浪费,对数据库也不够友好。但是胜在高度适配wordpress系统,实现代码较少,下面看看WordPress自定义文章类型实现问答效果的示例代码。

注册自定义文章类型

//https://www.902d.com/
//创建问答自定义文章类型
add_filter( 'template_include', 'include_question_template_function', 1 );
function include_question_template_function( $template_path ) {
if ( get_post_type() == 'question_answer' ) {
if ( is_single() ) {
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ( $theme_file = locate_template( array ( 'single-question_answer.php' ) ) ) {
$template_path = $theme_file;
} else {
// $template_path = plugin_dir_path( __FILE__ ) . '/single-movie_reviews.php';
}
}
}
return $template_path;
}
add_action( 'init', 'create_question_answer' );
function create_question_answer() {
register_post_type( 'question_answer',
array(
'labels' => array(
'name' => '问答',
'singular_name' => '问答',
'all_items'=>'所有问答',
'add_new' => '添加',
'add_new_item' => '新建问答',
'edit' => '编辑',
'edit_item' => '编辑问答',
'new_item' => '新问答',
'view' => '查看',
'view_item' => '查看问答',
'search_items' => '搜索问答',
'not_found' => '没找到问答',
'not_found_in_trash' => '回收站里没有问答',
'parent' => '问答父页面'
),

'public' => true,
'menu_position' => 15,
'supports' => array( 'title', 'editor', 'comments', 'custom-fields' ),
'taxonomies' => array( '' ),
'has_archive' => true,
'rewrite'=> array( 'slug' => 'question' ),
)
);
}

只需使用wordpress提供的register_post_type函数即可实现。将上面的代码放在你的functions.php文件中,到wordpress后台就能看见一个名为问答的一级菜单了,其形式如文章菜单。

新建一篇问答类型文章试试看,此时查看问答文章是不会有内容显示的,因为我们还没准备问答类型文章所使用的前端模板。随便复制一个网页,将其保存为HTML文件,再重命名为single-question_answer.php文件,前端访问时,wordpress就会自动查找single-+自定义文章类型名的模板文件。(同理,自定义类型文章列表也是这样,这里就不多讲了,自行百度吧。)

此时是否再访问可能会出现404未找到的现象,因此我们在代码中定义了此问答类型文章的访问路径,涉及到伪静态的,需要前往后台——设置——固定链接保存一下。

此时应该就能正确显示问答类型文章的页面了,就是你上面保存的那个HTML。只需对其进行一些动态替换即可完成内容的自动变换。

重写自定义文章类型文章的URL规则
如果我们希望问答类型文章的访问路径为:域名/question/postid.html,那么在functions.php文件中添加如下url重写规则。

//https://www.902d.com/
//重写自定义文章类型url规则
add_filter('post_type_link', 'custom_question_answer_link', 1, 3);
function custom_question_answer_link( $link, $post = 0 ){
if ( $post->post_type == 'question_answer' ){
return home_url( 'question/' . $post->ID .'.html' );
} else {
return $link;
}
}
add_action( 'init', 'custom_question_answer_rewrites_init' );
function custom_question_answer_rewrites_init(){
add_rewrite_rule(
'question/([0-9]+)?.html$',
'index.php?post_type=question_answer&p=$matches[1]',
'top' );
add_rewrite_rule(
'question/([0-9]+)?.html/comment-page-([0-9]{1,})$',
'index.php?post_type=question_answer&p=$matches[1]&cpage=$matches[2]',
'top'
);
}

再次保存一下动态链接试试效果吧。

创建单独数据表实现问答效果
这种效果是我认为最好的,因为可以高度自定义,不拘泥于wordpress,但代码量相对较大,对数据表的设计能力有一定要求,后期维护也比较复杂。建议最少能自己独立完成数据表的增删改查的站长使用,并且此种方式在wordpress后台管理不如第一种方式快捷方便。

虽然第二种比较复杂,但让我选,我仍然选第二种。最大的原因是预估本站数据会越来越大,wordpress的自定义文章类型实在太过臃肿,数据量大了后期网站速度影响较大。

© 版权声明
THE END
点赞9赞赏 分享