Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.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 按类别显示带有当前标记的帖子_Php_Wordpress - Fatal编程技术网

Php 按类别显示带有当前标记的帖子

Php 按类别显示带有当前标记的帖子,php,wordpress,Php,Wordpress,我正在创建一个与当前帖子具有相同标签的帖子列表(这在单个帖子模板中)。我需要显示那些按类别组织的帖子,类别名称作为标题出现一次。因此,最终结果将是: 类别1 POST1 POST2 POST3 类别2 POST4 POST5 邮政6 等等。。。与当前帖子具有相同标签的所有帖子,按类别组织,类别名称显示一次。(具有此标记的所有帖子将只属于一个类别。当前帖子将仅具有一个标记。)由于模板上存在其他循环,因此需要使用WP_查询。我的PHP技能很低——我花了几天时间寻找一个解决方案,并找到了一些有用的东西

我正在创建一个与当前帖子具有相同标签的帖子列表(这在单个帖子模板中)。我需要显示那些按类别组织的帖子,类别名称作为标题出现一次。因此,最终结果将是:

类别1
POST1
POST2
POST3
类别2
POST4
POST5
邮政6


等等。。。与当前帖子具有相同标签的所有帖子,按类别组织,类别名称显示一次。(具有此标记的所有帖子将只属于一个类别。当前帖子将仅具有一个标记。)由于模板上存在其他循环,因此需要使用WP_查询。我的PHP技能很低——我花了几天时间寻找一个解决方案,并找到了一些有用的东西,但我没能解决这个问题。非常感谢您的帮助。

您需要获取当前的帖子标签以及与这些标签相关的所有帖子。这可以通过以下方式实现:

  global $post;

  $tags = wp_get_post_tags(get_the_ID());

  if ($tags) {

                $tag_list = wp_get_object_terms(get_the_ID(),'post_tag',array('fields' => 'ids'));

                $post_args = array(
                    'orderby' => 'post_date',
                    'order' => 'DESC',
                    'post_type' => 'post',
                    'post_status' => 'publish',
                    'posts_per_page' => 5,          
                    'post__not_in' => array( $post->ID ),
                    'tag__in' => $tag_list
                );

      $get_recent_posts = get_posts($post_args);

   }//if ends - tags present
    $post_arr = array();

    foreach($get_recent_posts as $recent_post)
    {
        $post_id = $recent_post->ID;

        $category_id_list = wp_get_post_categories($post_id);

        $current_category_id = current($category_id_list);

        if(array_key_exists($current_category_id,$post_arr))
        {

            array_push($post_arr[$current_category_id],$post_id);
        }
        else
        {
            $post_arr[$current_category_id] = array($post_id);
        }
    }
这将获取5个最近的帖子,不包括当前帖子

我们可以在上面的代码块中包含以下代码,按类别对帖子进行排序,如下所示:

  global $post;

  $tags = wp_get_post_tags(get_the_ID());

  if ($tags) {

                $tag_list = wp_get_object_terms(get_the_ID(),'post_tag',array('fields' => 'ids'));

                $post_args = array(
                    'orderby' => 'post_date',
                    'order' => 'DESC',
                    'post_type' => 'post',
                    'post_status' => 'publish',
                    'posts_per_page' => 5,          
                    'post__not_in' => array( $post->ID ),
                    'tag__in' => $tag_list
                );

      $get_recent_posts = get_posts($post_args);

   }//if ends - tags present
    $post_arr = array();

    foreach($get_recent_posts as $recent_post)
    {
        $post_id = $recent_post->ID;

        $category_id_list = wp_get_post_categories($post_id);

        $current_category_id = current($category_id_list);

        if(array_key_exists($current_category_id,$post_arr))
        {

            array_push($post_arr[$current_category_id],$post_id);
        }
        else
        {
            $post_arr[$current_category_id] = array($post_id);
        }
    }
这将产生如下输出:

 Array
 (
   [69] => Array
    (
        [0] => 135
    )

    [70] => Array
    (
        [0] => 140
        [1] => 8
    )

  )
比如,Category ID作为“key”&所有其他关联的post ID作为其元素