如何在自定义帖子类型中获取帖子类别

时间:2018-08-03 00:46:14

标签: php wordpress custom-post-type

我有一个名为“ football_teams”的自定义帖子类型,其中有20个帖子。

public void codeReset(final String email, final String type)
{
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL2,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response)
                {
                    try
                    {
                        JSONObject jsonObject = new JSONObject(response);
                        String success = jsonObject.getString("success");
                        String message = jsonObject.getString("message");
                        if(success.equals("1"))
                        {
                            AlertDialogBox("Success", message);
                            cVerifyProgressBar.setVisibility(View.GONE);
                        }
                        else
                        {
                            AlertDialogBox("Failed", message);
                            cVerifyProgressBar.setVisibility(View.GONE);
                        }
                        prompt = success;

                    }
                    catch (JSONException e)
                    {
                        e.printStackTrace();
                        AlertDialogBox("Error", e.toString());
                        cVerifyProgressBar.setVisibility(View.GONE);
                    }

                    final String result=response.toString();
                    Log.d("response", "result : "+result);
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error)
                {
                    AlertDialogBox("Error", error.toString());
                    cVerifyProgressBar.setVisibility(View.GONE);
                }
            })
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError
        {
            Map<String, String> params = new HashMap<>();
            params.put("Email",email);
            params.put("actionType",type);

            int count = 0;
            count++;
            Log.d("count", "count: "+ count);
            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
} 

如何获取CPT中所有这些帖子的类别。

我尝试过// WP_Query arguments $args = array( 'post_type' => 'football_teams', 'posts_per_page' => '-1' ); // The Query $query = new WP_Query( $args ); ,但它显示了所有帖子的所有类别。

我也尝试过$cats = get_categories();,但是没有用。

任何帮助将不胜感激,我是Web开发的新手。

1 个答案:

答案 0 :(得分:3)

您可以简单地使用get_the_terms()函数来获取术语(类别)名称。 您可以使用下面的代码。 注意:$taxonomy是您的自定义分类法。如果您未使用自定义分类法,则为category

<?php

$post_type = 'your post type';
$taxonomy = 'your taxonomy';

$args = [
    'post_type' => $post_type,
    'posts_per_page' => -1
];

$query = new WP_Query($args);

if( $query->have_posts() ) {

    while( $query->have_posts() ) {
        $query->the_post();

        $terms = get_the_terms($post->ID, $taxonomy);
        $categories = [];

        if( $terms ) {          
            foreach ($terms as $category) {
                $categories[] = $category->name;
            }       
        }

        $categories = implode(', ', $categories);

        echo get_the_title() .'| '. $categories .' <br>';
    }
}


Sample output:
my post 1 | category1, category2, category3
my post 2 | category2, category4, category5