• Resolved floi13

    (@floi13)


    Playing around with VUEJS and the WordPress Rest API I can display my custom posttype on the page. The custom posttype also has a custom taxonomy, but I can’t seem to find a way to display that.
    I did read some stuff about adding /?_embed to the endpoint url and ‘show_in_rest => true’ to add support to the API and if I log the result I do get _embedded as an array.
    Only the name of the taxonomy isn’t there.

    Am I missing some steps to add the taxonomy of the custom post in the API result?

    DOM snippet

    <div id="workfeed" class="work" style="margin-top: 100px;">
    			<div v-for="(item,index) in work" class="row work__item" :data-index="index">
    				<div class="col work__item-image">
    					<img :src="item._embedded['wp:featuredmedia'][0].source_url" />
    				</div>
    
    				<div class="col work__item-content">
    					<h3>{{ item.title.rendered }}</h3>
    					{{ item.content.rendered }}
    					{{ item._embedded['wp:term'].rendered }}
    				</div>
    			</div>
    		</div>

    Snippet custom posttype

    $args = array(
    		'labels'		=> $labels,
    		'description'	=> 'All projects',
    		'public'		=> true,
    		'menu_icon'		=> 'dashicons-layout',
    		'supports'		=> array('title', 'editor', 'thumbnail'),
    		'has_archive'	=> true,
    		'show_in_rest'	=> true,
    		'rest_base'		=> 'work-api',
    		'rest_controller_class'	=> 'WP_REST_Posts_Controller'
    	);
    	
    	register_post_type('work', $args);
    }
    add_action('init', 'cptWork', 0);

    Snippet custom taxonomy

    register_taxonomy('types',array('work'), array(
        'hierarchical' 		=> true,
        'labels' 			=> $labels,
        'show_ui' 			=> true,
        'show_admin_column' => true,
        'query_var' 		=> true,
        'rewrite' 			=> array( 'slug' => 'type' ),
        'show_in_rest'		=> true,
        'rest_base'			=> 'work-api',
        'rest_controller_class'	=> 'WP_REST_Terms_Controller'
      ));
    }
    add_action( 'init', 'taxWork', 0 );
Viewing 4 replies - 1 through 4 (of 4 total)
  • Can you please try once by changing ‘rest_base’ => ‘work-api’, to something else say ‘rest_base’ => ‘types’,

    You can check once here

    Hope this helps.

    Thread Starter floi13

    (@floi13)

    Hi Anurag,

    This doesn’t seem to do anything, except limit the result to 10 items (total is 16). Maybe a screenshot of the api result helps: https://bit.ly/2UGE5iH. The ‘work-api’ returns the ID of the chosen taxonomy.
    As a plan B I could write some logic like ‘if ID is 6, taxonomy is abc’, but that’s not something I would like to do.

    • This reply was modified 5 years, 11 months ago by floi13.
    • This reply was modified 5 years, 11 months ago by floi13.
    • This reply was modified 5 years, 11 months ago by floi13.
    Moderator bcworkz

    (@bcworkz)

    The term ID of your taxonomy is all you can expect to get with an API posts request. If your client app needs more, like the term name, it needs to make a separate taxonomy request with the provided term ID.

    Or as in your plan B, hardcode the term lookup in the app. I think you realize this is a very poor alternative. One, you would be replicating data, never a good idea. Two, it doesn’t allow for the flexibility of adding terms without having to modify the client app. IMO better to make a second request.

    I suppose you could create a custom route/endpoint that feeds everything all at once. But you end up with a lot of redundant code just to save one extra request.

    Thread Starter floi13

    (@floi13)

    Hi bcworkz,

    You’re right; the plan b wasn’t something I wanted to do, it was more of an alternative.
    Hmz, a separate request you say.. perhaps I’ll drop the whole api approach and just display all wanted data through standard WordPress loop.

    Owell, you learn and you try (=

    • This reply was modified 5 years, 11 months ago by floi13.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Display post taxonomy REST API’ is closed to new replies.