wordpress 自定义循环 按指定 array(ID数组)排序

实际应用:
$p_id_cookie = $_COOKIE['p_id_cookie'];
if(isset($p_id_cookie)){
	$arr = json_decode($p_id_cookie);
}
//print_r(array_reverse($arr));
$args = array(
	'post_type'	=>	'post',
	'post__in'	=>	array_reverse($arr),
	'orderby'=>'post__in'
);
$the_query = new WP_Query($args);

if ( $the_query->have_posts() ) : 

// Start the Loop.
while ( $the_query->have_posts() ) : $the_query->the_post();

/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );

endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();

else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );

endif;
wp_reset_postdata();

以下代码 wordpress 自定义循环 按指定 array(ID数组)排序 

$args = array(
'post_type' => 'post',
'post__in' => array(3,5,2),
'orderby'=>'post__in'
);
$the_query = new WP_Query($args);

他会按ID 3 5 2 排序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WordPress 提供了自定义接口的功能,让开发者可以创建自己的 REST API 端点。下面是一个简单的示例,展示了如何在 WordPress 中创建一个自定义的 REST API 端点: 1. 首先,在你的主题文件夹中创建一个名为 `functions.php` 的文件。 2. 在 `functions.php` 文件中添加以下代码: ```php function custom_api_route() { register_rest_route( 'custom/v1', '/your-endpoint', array( 'methods' => 'GET', 'callback' => 'custom_api_callback', ) ); } add_action( 'rest_api_init', 'custom_api_route' ); function custom_api_callback( $request ) { // 在这里编写你的自定义接口逻辑 // 返回数据 return array( 'message' => 'Hello, World!' ); } ``` 在这个示例中,我们使用了 `register_rest_route` 函数来注册一个自定义的 REST API 端点。第一个参数是命名空间,用于将你的端点与其他端点区分开来。第二个参数是你的自定义端点的路径,你可以在这里指定你想要的路径。第三个参数是一个数组,用于指定支持的 HTTP 方法和回调函数。 3. 保存并上传 `functions.php` 文件到你的主题文件夹。 4. 现在你可以通过访问 `/wp-json/custom/v1/your-endpoint` 来访问你创建的自定义接口。 当你访问该接口时,`custom_api_callback` 函数会被调用,你可以在这个函数中编写你的自定义接口逻辑。在示例中,我们简单地返回了一条消息。 这只是一个简单的示例,你可以根据你的需求添加更多的逻辑和功能。你可以在自定义接口的回调函数中访问数据库、获取数据等等。希望这个示例能帮助到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值