WordPress功能还是比较强大的,WordPress可以进行自定义文章类型功能,可以有内容,有评论,有点赞,只是因为很多文章用到的内容用不上导致浪费,对数据库不是很友好,那么WordPress如何自定义实现问答效果?
//https://www.daimadog.com/7093.html
//创建问答自定义文章类型
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后台就能看见一个名为问答的一级菜单了,其形式如文章菜单。
此时是否再访问可能会出现404未找到的现象,因此我们在代码中定义了此问答类型文章的访问路径,涉及到伪静态的,需要前往后台——设置——固定链接保存一下。
此时应该就能正确显示问答类型文章的页面了,就是你上面保存的那个HTML。只需对其进行一些动态替换即可完成内容的自动变换。
重写自定义文章类型文章的URL规则
如果我们希望问答类型文章的访问路径为:域名/question/postid.html,那么在functions.php文件中添加如下url重写规则。
//https://www.daimadog.com/7093.html
//重写自定义文章类型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的自定义文章类型实在太过臃肿,数据量大了后期网站速度影响较大。