我正在尝试将我的附件id页重定向到正确的帖子id页(woocommerce单页)。
我得到了这个URL,附件id=17381。如何检索使用此附件ID的产品/帖子ID?我已经搜索了几个小时,尝试了不同的方法,但我找不到解决方案。
https://www.ejstruplys.dk/no/?attachment_id=17381
我希望将其重定向到此页面:
https://www.ejstruplys.dk/no/produkt/block-candles-rustic-surface-3/正在执行var_dump($post)
给了我这个,但我不知道这个附件\\u id->产品id位于哪里:
OBJECT = WP_Post::__set_state(array(
\'ID\' => 17381,
\'post_author\' => \'1\',
\'post_date\' => \'2015-08-24 19:32:00\',
\'post_date_gmt\' => \'2015-08-24 19:32:00\',
\'post_content\' => \'\',
\'post_title\' => \'Orange. Bloklys. Rustikk overflate\',
\'post_excerpt\' => \'\',
\'post_status\' => \'inherit\',
\'comment_status\' => \'open\',
\'ping_status\' => \'closed\',
\'post_password\' => \'\',
\'post_name\' => \'bloklys_rustik_overflade_55_dia_orange-jpg-2\',
\'to_ping\' => \'\',
\'pinged\' => \'\',
\'post_modified\' => \'2015-08-24 19:32:00\',
\'post_modified_gmt\' => \'2015-08-24 19:32:00\',
\'post_content_filtered\' => \'\',
\'post_parent\' => 0,
\'guid\' => \'https://www.ejstruplys.dk/wp-content/uploads/2015/08/bloklys_rustik_overflade_55_dia_orange.jpg\',
\'menu_order\' => 0,
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image/jpeg\',
\'comment_count\' => \'0\',
\'filter\' => \'raw\',
\'post_title_ml\' => \'[:en]Orange. Block candles. Rustic surface[:no]Orange. Bloklys. Rustikk overflate[:sv]Orange. Blokljus. Rustik yta[:de]Orange. Stumpenkerzen. Rustikale Oberfläche[:da]Orange. Bloklys. Rustik overflade[:]\',
));
如何从附件ID中检索帖子ID?
Edit:我试过这个:Get post id by attachment id?
但它只是回来了null
.
SO网友:Unicco
根据之前的回答,我成功地做到了这一点。感谢回复,指出问题是基于错误的附件(不存在post\\u父ID)。
如果有人在为同样的问题而挣扎,我最终会得到以下解决方案:
该方法挂钩于Wordpress\'template_redirect
, 检查特定页面是否有parent\\u post,并使用两种不同的方法检索正确的产品post ID。它适用于所有“~/?attachment\\u ID”和“~/variation\\u Product”**。
/*
* Redirect Attachment Pages
*/
add_action( \'template_redirect\', \'wpse27119_template_redirect\' );
function wpse27119_template_redirect() {
global $post;
/* Assign the specific post ID to local variable */
$post_id = get_the_ID();
/* If page has no attachment - no need to continue */
if (!is_attachment() ) return false;
if (empty($post)) $post = get_queried_object();
/* Instantiating the product object based on currenct post ID */
$_product = new WC_Product_Variation($post_id);
/* Check if specific post got an post_parent ~ proper image-attachments and product_variation */
if ($post->post_parent) {
/* Retrieve the permalink ~ from the current product-object */
$link = get_permalink($_product->post->ID);
/* Redirect the user */
wp_redirect( $link, \'301\' );
exit();
} else {
/* Retrieve all product-pages */
$args = array(
\'post_type\' => \'product\',
\'posts_per_page\' => -1
);
/* Initialize WP_Query, and retrieve all product-pages */
$loop = new WP_Query($args);
/* Check if array contains any posts */
if ($loop->have_posts()): while ($loop->have_posts()): $loop->the_post();
global $product;
/* Iterate through all children in product */
foreach ($product->get_children() as $child_id) {
/* Retrieve all children of the specific product */
$variation = $product->get_child($child_id);
/* Check if variation is an instance of product */
if ($variation instanceof WC_Product_Variation) {
/* Retrieve all image_ids from variation */
$variation_image_id = $variation->get_image_id();
/* Compare variation image id with the current post id ~ attachment id */
if ($variation_image_id == $post_id) {
/* Retrieve the correct product - using the post id */
$_product = new WC_Product_Variation($post_id);
/* Retrieve the permalink ~ from the current product-object */
$link = get_permalink($_product->post->ID);
/* Redirect the user */
wp_redirect($link, \'301\');
exit();
}
}
}
endwhile; endif; wp_reset_postdata();
}
}
我试图将变体参数解析为重定向方法。如果我找到解决方案,我会更新这篇文章。