^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助,如果有代码出错,请联系站长解决
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 腾讯云3年2核2G新品轻量限时特惠只需408元

WordPress问答中心插件DW Question & Answer自定义链接和调用问题列表

一款简单的免费 WordPress 问答中心插件 DW Question & Answer,你值得拥有!

预览地址:https://dedewp.com/dwqa-questions/

2 核 2G 限时特惠 396 元/3 年    宝塔建站 10850 大礼包

后台搜索安装插件后会发现插件会自动新建两个页面,你可以自行修改页面标题。

下面分享下如何设置问答的固定连接为 id.html 格式,只需将下面的代码添加到当前主题的 functions.php :

/**
 * 自定义问答页面的固定链接结构为 id.html
 * https://www.wpdaxue.com/custom-post-type-permalink-code.html
 */
add_filter('post_type_link', 'custom_qa_link', 1, 3);
function custom_qa_link( $link, $post = 0 ){
	if ( $post->post_type == 'dwqa-question' ){
		return home_url( 'question/' . $post->ID .'.html' );
	} else {
		return $link;
	}
}
add_action( 'init', 'custom_qa_rewrites_init' );
function custom_qa_rewrites_init(){
	add_rewrite_rule(
		'question/([0-9]+)?.html$',
		'index.php?post_type=dwqa-question&p=$matches[1]',
		'top' );
}

添加后,如果访问问答页面出现 404 错误,请访问 WP 后台 – 设置 – 固定链接,保存一遍这里的设置即可。如果还不行,很可能是你的主机不支持伪静态,请联系你的主机商。

如何调用问答列表,比如最热问答?最新问答?随机问答?

1、随机问答列表

/**
* 调用最新问题列表
*/
<?php
$instance = wp_parse_args( $instance, array(
'title' => __('Latest Questions','dwqa'),
'number' => 10  //调用的多少条
) );
$args = array(
'posts_per_page' => $instance['number'],
'order' => 'DESC',
'orderby' => 'post_date',
'post_type' => 'dwqa-question',
'suppress_filters' => false
);
$questions = new WP_Query( $args );
while( $questions->have_posts() ) { $questions->the_post(); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title();?>"><?php the_title();?></a></li>
<?php } wp_reset_query();?>

2、最热问答列表

/**
* 调用热门问题列表
*/
<?php
$instance = wp_parse_args( $instance, array(
'title' => __('Popular Questions','dwqa'),
'number' => 10  //调用的多少条
) );
$args = array(
'posts_per_page' => $instance['number'],
'order' => 'DESC',
'orderby' => 'meta_value_num',
'meta_key' => '_dwqa_views',
'post_type' => 'dwqa-question',
'suppress_filters' => false
);
$questions = new WP_Query( $args );
while( $questions->have_posts() ) { $questions->the_post(); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title();?>"><?php the_title();?></a></li>
<?php } wp_reset_query();?>

3、最新问答列表

/**
* 调用最新问题列表
*/
<?php
$instance = wp_parse_args( $instance, array(
'title' => __('Latest Questions','dwqa'),
'number' => 6  //调用的多少条
) );
$args = array(
'posts_per_page' => $instance['number'],
'order' => 'DESC',
'orderby' => 'post_date',
'post_type' => 'dwqa-question',
'suppress_filters' => false
);
$questions = new WP_Query( $args );
while( $questions->have_posts() ) { $questions->the_post(); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title();?>"><?php the_title();?></a></li>
<?php } wp_reset_query();?>

一般来说该插件适用于任何主题,如果需要修改成陌小雨现在在用的样式,留言联系哦!

赠人玫瑰,手有余香。