项目概况
项目组需要在后台能够自己选择文章,配置首页文章推荐。
解决方案
新开发一个首页推荐配置的插件,管理员登录WordPress后台后,能够选择需要推荐的文章,提交保存时,以此给文章增加一个自定义属性的值,比如post_weight。然后根据自定义属性修改首页的查询,展示配置的推荐文章。
具体实现
1、创建插件文件夹
目录:/wp-content/plugins
本人创建的目录名称是:recommend
2、创建recommend.php文件,编写插件代码
说明:本人开发的插件比较简单,功能单一,所以插件代码都写在一个文件里面,如果功能比较复杂,可以自行进行功能拆分。
<?php
/*
Plugin Name: Recommend Plugin
Plugin URI: https://developer.wordpress.org/plugins/the-basics/
Description: Recommend Plugin Header Comment
Version: 20231127
Author: Niemx
Author URI: https://developer.wordpress.org/
Text Domain: wporg
Domain Path: /languages
License: GPL2
Recommend Plugin is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
any later version.
Recommend Plugin is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with {Plugin Name}. If not, see {License URI}.
*/
function recommend_activate() {
}
register_activation_hook( __FILE__, 'recommend_activate' );
function recommend_deactivate() {
//--------------Clear the credentials once deactivated-------------------
// delete_option('zmail_integ_client_id');
}
register_deactivation_hook( __FILE__, 'recommend_deactivate' );
function recommend_integ_settings() {
add_menu_page (
'Welcome to Recommend Plugin',
'Recommend',
apply_filters( 'recommend_capability', 'read' ),
// '',
'recommend-integ-settings',
'recommend_home_integ_settings_callback' ,
'dashicons-admin-generic'
);
}
function recommend_home_integ_settings_callback(){
global $current_user;
$bool_can = false;//权限控制
if( $current_user->roles[0] == 'editor' || $current_user->roles[0] == 'administrator') {
$bool_can = true;
}
if( $bool_can ) {
if(isset($_POST['home_recommend_submit']) && !empty($_POST)){
//原始数据全部置为0
$args = array(
'posts_per_page' => 10,
'post_type' => array('post'),
'post_status' => array('publish'),
'ignore_sticky_posts ' => false,
// 'orderby' => 'date',
'order' => 'DESC',
'meta_key' => 'post_weight',
'orderby' => 'meta_value_num',
);
$post_pre_list = new WP_Query($args);
while ($post_pre_list->have_posts()) : $post_pre_list->the_post();
update_metadata( 'post',get_the_ID(), 'post_weight', 0 );
endwhile;
for($num = 1,$weight=100;$num<=10;$num++,$weight--){
if($_POST['recommend_key_'.$num] > 0){
update_metadata( 'post',$_POST['recommend_key_'.$num], 'post_weight', $weight );
}
}
echo '<div class="updated"><p><strong>'.esc_html__('Save Successfully').'</strong></p></div>'."\n";
}
}
$args_new = array(
'posts_per_page' => 10,
'post_type' => array('post'),
'post_status' => array('publish'),
'ignore_sticky_posts ' => false,
'order' => 'DESC',
'meta_key' => 'post_weight',
'orderby' => 'meta_value_num',
);
$post_list = new WP_Query($args_new);
?>
<head>
<meta charset="UTF-8">
<title>Recommended configuration for homepage articles</title>
</head>
<body>
<h1 style="margin-top:20px;margin-left: 20px;">Recommended configuration for homepage articles</h1>
<form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
<?php add_thickbox(); ?>
<div style="margin-top:20px;margin-left: 20px;">
<?php $key=1; ?>
<?php while ($post_list->have_posts()) : $post_list->the_post(); ?>
<table>
<?php
$post_weight = get_metadata( 'post', get_the_ID(), 'post_weight', true );
if($post_weight>0){ ?>
<tr>
<td width="180px;">Recommended articles(<?php echo $key;?>)</td>
<td>
<input type="text" value="<?php the_title(); ?>" name="recommend_<?php echo $key;?>" id="recommend_<?php echo $key;?>" style="width: 800px">
<?php
if( $bool_can ) {
?>
<a class="thickbox" title="Article List" href="/wp-admin/post-list.php?TB_iframe=true&width=1000&height=680&table_id=<?php echo $key;?>"><button class="btn_select">Select</button></a>
<?php } ?>
<input type="hidden" value="<?php the_ID(); ?>" name="recommend_key_<?php echo $key;?>" id="recommend_key_<?php echo $key;?>">
</td>
</tr>
<?php }else{?>
<tr>
<td width="180px;">
Recommended articles(<?php echo $key;?>)
</td>
<td>
<input type="text" value="" name="recommend_<?php echo $key;?>" id="recommend_<?php echo $key;?>" style="width: 800px">
<?php
if( $bool_can ) {
?>
<a class="thickbox" title="Article List" href="/wp-admin/post-list.php?TB_iframe=true&width=1000&height=680&table_id=<?php echo $key;?>"><button class="btn_select">Select</button></a>
<?php } ?>
<input type="hidden" value="" name="recommend_key_<?php echo $key;?>" id="recommend_key_<?php echo $key;?>">
</td>
</tr>
<?php }
$key++;
endwhile;
wp_reset_postdata();
?>
</table>
</div>
<?php
if( $bool_can ) {
?>
<div class="form__row form__row-btn" style="margin-left: 20px;"> <input type="submit" class = "btn" name="home_recommend_submit" id="home_recommend_submit" value="<?php _e('Save');?>">
<?php } ?>
</div>
</form>
<script type="text/javascript">
function parent_check(_input_id,_value,_title){
var _input = document.getElementById("recommend_"+_input_id);
var _input_hidden = document.getElementById("recommend_key_"+_input_id);
_input_hidden.setAttribute("value", _value);
_input.setAttribute("value", _title);
tb_remove();
}
</script>
<style>
.btn_select {
display: inline-block;
padding: 5px 10px;
background-color: #4c84ff;
color: #fff;
font-size: 15px;
border-radius: 3px;
border:0px;
cursor: pointer;
transition: all 0.3s ease
}
</style>
</body>
<?php
}
add_action('admin_menu','recommend_integ_settings');
插件上传之后,需要登录WordPress后台启用插件。
3、配置页面效果
4、修改首页查询
查询代码
$arg = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC',
'posts_per_page' => 10,
'meta_key' => 'post_weight',
'orderby' => 'meta_value_num',
);
$queryd = new \WP_Query( $arg );
使用查询的结果集
while ($queryd->have_posts()) : $queryd->the_post();