Php 自定义邮政类型相关产品
我在我的Wordpress网站上定制了一个帖子类型“product”。产品的详细信息页面是single-product.php,它完美地显示了产品的所有信息 所有产品将按以下结构分类:Php 自定义邮政类型相关产品,php,wordpress,custom-post-type,Php,Wordpress,Custom Post Type,我在我的Wordpress网站上定制了一个帖子类型“product”。产品的详细信息页面是single-product.php,它完美地显示了产品的所有信息 所有产品将按以下结构分类: 脚趾控制 埃列克特罗尼斯隆酒店 旺德莱泽 软件 过度生产 斯卢泰尔卡顿 克鲁伊赞 我的网站上有两个测试产品。这两种产品都属于“Electronische sloten”类别。这是“控制”的子类别 我想在single-product.php上显示相关产品。此相关产品不能是当前产品本身,必须在同一父类别下。
<?php
$related = get_posts( array(
'post_type' => 'product',
'category__in' => wp_get_post_categories($post->ID),
'numberposts' => 5,
'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php }
wp_reset_postdata();
?>
当我看产品A时,我会在相关产品下看到产品B,但当我看产品B页面时,我看不到产品A。它们的类别完全相同
提前感谢。尚未测试此功能,但您可以尝试
$related = get_posts( array(
'post_type' => 'product',
'tax_query' => array( array(
'taxonomy' => $taxonomy_name,
'field' => 'term_id',
'terms' => wp_get_post_terms($post->ID, $taxonomy_name, array('fields' => 'ids'))
) ),
'numberposts' => 5,
'exclude' => array($post->ID)
) );
请使用下面的代码,我认为它会工作
$related = new WP_Query(
array(
'category__in' => wp_get_post_categories( $post->ID ),
'posts_per_page' => 5,
'post__not_in' => array( $post->ID )
)
);
if( $related->have_posts() ) {
while( $related->have_posts() ) {
$related->the_post(); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php }
wp_reset_postdata();
} ?>
$related=newwp\u查询(
排列(
'category\uu in'=>wp\u get\u post\u categories($post->ID),
“每页帖子数”=>5,
'post\u not\u in'=>数组($post->ID)
)
);
如果($related->have_posts()){
而($related->have_posts()){
$related->the_post();?>
谢谢你的回复。但它不起作用。你的代码给出了其他帖子。我有两个产品,子类别为“Elektronische sloten”,来自家长“Toegangs Controls”,子类别为“Sleutelkaarten”,来自家长“Overige producten”。当我在single-product.php页面时,它仍然显示来自另一个父类别的产品。我只想看到来自同一父类别的产品。已经测试过,并且正常工作。使用var_dump(wp_get_post_categories($post->ID))要输出和检查您收集的类别ID,请将它们与您应该接收的类别ID进行比较。还要检查$post->ID变量以匹配当前产品。您是否有类别或它是否是自定义分类法?这是自定义分类法在这种情况下,您必须使用wp_get_post_terms()而不是wp_get_post_categories()-更新了答案谢谢!但我收到一个语法错误,意外的“=”,预期的“'),以及$taxonomy\u name从哪里获得值?