WP 查询按 $_GET 获取和 ID 并将其更改为 CPT 链接



>我正在尝试进行查询以获取id并返回cpt自定义链接。这是我的代码,但无法结束它:

<?php
require_once('wp-load.php');
$args = array(
'post_type' =>  'catalogos',
'post_status' => 'publish',
'id' => $_GET['id'],
);
$query= new WP_Query($args);
$link = get_field('link');
if(isset($_GET['id'])){
wp_redirect(home_url()"/".$link."/");
}

?>
http://yoursite.com/?id=your_page_id eg:2
$args = array(
'post_type' =>  'catalogos',
'post_status' => 'publish',
'id' => $_GET['id'],
);
$query= new WP_Query($args);
if ( $query->have_posts() ) {
while ( $query->have_posts() ){
$query->the_post();
$post_id = get_the_ID();
$link = get_field('link');
if($link){
wp_redirect(home_url()"/".$link."/?id=".$post_id);
}
}
}
wp_reset_postdata();

确保是否有"id"参数 https://codex.wordpress.org/Class_Reference/WP_Query

最新更新