Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将相关帖子添加到单帖子wordpress模板_Php_Wordpress_Related Content - Fatal编程技术网

Php 将相关帖子添加到单帖子wordpress模板

Php 将相关帖子添加到单帖子wordpress模板,php,wordpress,related-content,Php,Wordpress,Related Content,我试图在一个贴子页面中展示一个包含相关产品的模块 我创建了一个名为“产品”的cpt和一个名为“类别”的分类法 我想做的是,在一个贴子页面中显示相同类别的其他产品 到目前为止,我使用wp_get_recent_post函数成功地添加了其他帖子,但我当然获得了所有帖子 如何将类传递给查询 这是我的代码: <?php $args = array( 'numberposts' => '4', 'orderby' => 'rand',

我试图在一个贴子页面中展示一个包含相关产品的模块

我创建了一个名为“产品”的cpt和一个名为“类别”的分类法

我想做的是,在一个贴子页面中显示相同类别的其他产品

到目前为止,我使用wp_get_recent_post函数成功地添加了其他帖子,但我当然获得了所有帖子

如何将类传递给查询

这是我的代码:

<?php
$args = array(
            'numberposts' => '4',
            'orderby' => 'rand',
            'post_type' => 'product',
            'post_status' => 'publish'
             );
            $recent_posts = wp_get_recent_posts( $args );
            foreach( $recent_posts as $recent ){
                echo '<div class="col-md-3"><a href="' . get_permalink($recent["ID"]) . '">'. get_the_post_thumbnail($recent["ID"], 'thumbnail' ) . $recent["post_title"].'</a> </div> ';
            }
?>

多谢各位

编辑

我这样解决:

            $terms = get_the_terms( $post->ID , 'category' );
                if ( $terms != null ){
                foreach( $terms as $term );
                }

            $args = array(
            'post_type' => 'product',
            'post__not_in' => array($post->ID),
            'tax_query' => array(
                array(
                'taxonomy' => 'category',
                'field' => 'slug',
                'terms' => $term->slug))
                );

            $recent_posts = wp_get_recent_posts( $args );
            foreach( $recent_posts as $recent ){
                echo '<div class="col-md-3"><a href="' . get_permalink($recent["ID"]) . '">'. get_the_post_thumbnail($recent["ID"], 'thumbnail' ) . $recent["post_title"].'</a> </div> ';
                }
$terms=获取术语($post->ID,'category');
如果($terms!=null){
foreach($terms作为$term);
}
$args=数组(
“post_类型”=>“产品”,
'post\u not\u in'=>数组($post->ID),
“tax_query”=>数组(
排列(
“分类法”=>“类别”,
'字段'=>'段塞',
“术语”=>$term->slug)
);
$recent_posts=wp_get_recent_posts($args);
foreach(最近发布的文章为$recent){
回声';
}
使用
获取帖子()
():

$related=get_posts($args);
foreach($post相关){
设置_postdata($post);
回声';
}
wp_reset_postdata();

顺便说一句,要查询一组随机的帖子,你应该稍微修改一下你的
$args
'order'=>'rand','orderby'=>'none'
谢谢你的回复,很遗憾,这不是适合我的解决方案。使用您的代码,我可以获得所有帖子,就像使用wp_get_recent_posts函数一样。当我正在查看一篇文章,而这篇文章属于“第一类”类别时,我想显示同一类别的另一篇文章;你可以使用
$cats=get_the_the_terms(get_the_ID(),'category')获取当前帖子的类别$cats=wp_list_pull($cats,'term_id')
并将以下内容添加到get_posts的查询参数中:
'category'=>$cats
再次感谢您。通过你的暗示,我找到了解决办法。
$related = get_posts( $args );
foreach( $related as $post ){
    setup_postdata( $post );
    echo '<div class="col-md-3"><a href="' . get_permalink() . '">'. get_the_post_thumbnail( get_the_ID(), 'thumbnail' ) . get_the_title() . '</a></div>';
}
wp_reset_postdata();