1

I am creating a section on my site that will display the last five categories created. How could I do that?

UPDATE: The code below is what I want, just like to know how to apply in order, if the latest published categories.

<?php
$cat = get_query_var('cat');
$categories=get_categories('child_of='.$cat);
if ($categories) { ?>

<div class="subcat-archive">
<?php
$cat = get_query_var('cat');
$categories=get_categories('child_of='.$cat);
if ($categories) {
foreach($categories as $term) {
echo $title . '<a href="' . get_category_link( $term->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a> | '; }
}
?>
</div>
<?php
}
else {
?>
<?php
}
?>
2

the id of the categories is autoincrement so you can sort by id to find the last one :

$args = array(
    "type" => "post",
    "orderby" => "id",
    "order" => "DESC",
    "number" => "5",
    "taxonomy" => "category",
    "hide_empty" => FALSE, // TRUE or FALSE depending what you want
);

$categories = get_categories($args);
| improve this answer | |
  • This will work, but it is not fool proof as the auto increment ID function can be changed ;-) – Pieter Goosen Jul 28 '15 at 6:28
  • True, @PieterGoosen, but probably the best you can get without custom tracking of the insert order. But then again, everything can be messed with :) – s_ha_dum Jul 29 '15 at 13:49
  • @s_ha_dum for sure. I think there is still an unsolved question regarding implementing a system where the date is saved when a term is created. This will be the most reliable way if you really really need a very reliable way. Otherwise, the method in this answer will most certainly do – Pieter Goosen Jul 29 '15 at 13:57
  • 1
    I agree @PieterGoosen. I am trying to decide if I want to tackle that :) – s_ha_dum Jul 29 '15 at 14:39
1

use this code for access categories

$args = new  WP_Query( array(
  'type'     = 'post',
  'taxonomy' = 'taxonomy_name',
  'order'    = 'ASC',
  'orderby'  = 'id'
));
//this function are used for access all categories
$query = get_categories($args);
| improve this answer | |
  • 1
    How is this even relevant to what has been asked. File an edit and explain why. Also, where are you getting the arguments from which you are using – Pieter Goosen Jul 28 '15 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.