Thank you! Your feedback has been delivered
Thank you! Your feedback has been sent

WordPress | Custom category page loop

Hi there,

Right now, I am creating a custom category page for one specific category of a website in WordPress. This page "category-263.PHP" is running a query for showing all internal sub-categories related to that specific category (and also descriptions of that).

I found this query online, and I can use NAME or ID for sorting those articles, but category ID is different than category SLUG and we would like to sort these sub categories in the way we want.

So it would be great if we can use category SLUG for sorting them or any other way we can list subcategories.

This is the code:

<?php
global $wpdb;
if (is_category( )) { $cat2 = get_query_var('cat2'); $yourcat = get_category ($cat2); }
$categories = $wpdb->get_results("SELECT $wpdb->terms.term_id AS id, name, description from $wpdb->terms INNER JOIN $wpdb->term_taxonomy ON $wpdb->terms.term_id = $wpdb->term_taxonomy.term_id WHERE parent = '$cat' ORDER BY name ASC");
foreach($categories as $category) : ?>


<a href="<?php echo get_category_link($category->id);?>">

<h3><?php echo $category->name; ?></h3></a>



    <ul>
    <li><a href="<?php echo get_category_link($category->id);?>"><?php echo $category->description; ?></a>



    </li>
    </ul> 

<?php endforeach; ?>
User Gravatar

daanial

Posted Jun 7 2014 13:00 UTC

$25


  • Assigned To ServA1
  • Solved
  • php
    wordpress
  • 1057 Views

14 Replies


I don't know if I understand you correctly.

Replace

ORDER BY name ASC

with

ORDER BY slug ASC

This orders results by slug value.

User Gravatar

renekorss

Posted Jun 7 2014 13:24 UTC

Thanks, but slug won't work here. slug is not a defined value here. Maybe you can suggest any other loop as an alternative?

User Gravatar

daanial

Posted Jun 7 2014 14:12 UTC

 $term_slug = get_query_var( 'cat2' );
 $taxonomyName = get_query_var( 'category' );
 $current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
 $termchildren = get_term_children( $current_term->term_id, $taxonomyName );
 foreach ($termchildren as $child) {
       $term = get_term_by( 'id', $child, $taxonomyName );
       echo 'a href="' . get_term_link( $term->name, $taxonomyName ) . '">'$term->name . '</a>';
 }

what about something like this?

User Gravatar

chrismccoy

Posted Jun 7 2014 14:18 UTC

Great Chrismccoy, but I get a error while replacing this with my main code. Where should I replace it? Maybe i do something wrong...

User Gravatar

daanial

Posted Jun 7 2014 14:26 UTC

i would wrap it in a function in your functions.php then call that via your template

User Gravatar

chrismccoy

Posted Jun 7 2014 14:28 UTC

Thanks Chrismccoy, I've added it to functions.php but it makes an error and makes the whole site unaccecbile. I really apperciate it if you can take a look at it again.

User Gravatar

daanial

Posted Jun 7 2014 14:35 UTC

try turning WP_DEBUG to true in your wp-config.php and pass the error on.

User Gravatar

chrismccoy

Posted Jun 7 2014 18:33 UTC

I tried it and pity, all errors are completely off by the hosting (WPEngine). Sorry for taking your time anyway...

User Gravatar

daanial

Posted Jun 7 2014 18:52 UTC

Have you tried something like this

<?php

$catid = get_category_by_slug('*cat2*')->term_id;

$args = array(
    'type'                     => 'post',
    'child_of'                 => $catid,
    'parent'                   => '',
    'orderby'                  => 'slug',
    'order'                    => 'ASC',
    'hide_empty'               => 1,
    'hierarchical'             => 1,
    'exclude'                  => '',
    'include'                  => '',
    'number'                   => '',
    'taxonomy'                 => 'category',
    'pad_counts'               => false 

);

$categories = get_categories($args);
  foreach($categories as $category) { 
    echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
    echo '<p> Description:'. $category->description . '</p>';
    echo '<p> Post Count: '. $category->count . '</p>';  } 

?>

*child_of* will get all children and grandchildren of the category id you enter for that, whereas parent will only get the children (ie no grandchildren)

User Gravatar

ServA1

Posted Jun 7 2014 21:14 UTC

I've adjusted my previous code to fit your desired output and also tested it in the latest version of wordpress and it works fine :

<?php

$catid = get_category_by_slug('cat2')->term_id;

$args = array(
    'type'                     => 'post',
    'child_of'                 => $catid,
    'parent'                   => '',
    'orderby'                  => 'slug',
    'order'                    => 'ASC',
    'hide_empty'               => 0,
    'hierarchical'             => 1,
    'exclude'                  => '',
    'include'                  => '',
    'number'                   => '',
    'taxonomy'                 => 'category',
    'pad_counts'               => false
);

$categories = get_categories($args);

foreach($categories as $category) { 

    echo '<a href="' . get_category_link($category->id) . '"><h3>' . $category->name . '</h3></a>';

    echo '<ul><li><a href="' . get_category_link($category->id) . '">' . $category->description . '</a></li></ul>'; 

} 

?>
User Gravatar

ServA1

Posted Jun 7 2014 21:41 UTC

oops I forgot to update the category links with the right variable... just use *get_category_link($category)*

User Gravatar

ServA1

Posted Jun 7 2014 21:59 UTC

here is the complete category-263.php page for you, and also updated the category links with the correct variable :

<?php
/**
 * The template for displaying Category pages
 *
 * @link http://codex.wordpress.org/Template_Hierarchy
 *
 * @package WordPress
 * @subpackage Twenty_Fourteen
 * @since Twenty Fourteen 1.0
 */

get_header(); ?>

        <div id="content" class="site-content" role="main">

    <?php

    $catid = get_category_by_slug('a')->term_id;

    $args = array(
        'type'                     => 'post',
        'child_of'                 => $catid,
        'parent'                   => '',
        'orderby'                  => 'slug',
        'order'                    => 'ASC',
        'hide_empty'               => 0,
        'hierarchical'             => 1,
        'exclude'                  => '',
        'include'                  => '',
        'number'                   => '',
        'taxonomy'                 => 'category',
        'pad_counts'               => false 

    );

    $categories = get_categories($args);

    foreach($categories as $category) { 

        echo '<a href="' . get_category_link($category) . '"><h3>' . $category->name . '</h3></a>';

       echo '<ul><li><a href="' . get_category_link($category) . '">' . $category->description . '</a></li></ul>'; 

    } 

    ?>

        </div><!-- #content -->

<?php

get_footer();
User Gravatar

ServA1

Posted Jun 7 2014 22:09 UTC

Solution

This didn't solve your task? Get your own custom solution.

Many thanks SerVa1. Almost works! :)

User Gravatar

daanial

Posted Jun 9 2014 0:20 UTC

No worries daanial, thanks for accepting my answer. If you are having trouble tweaking that code to suit your needs just let me know and I'll be happy to give you hand

User Gravatar

ServA1

Posted Jun 9 2014 20:56 UTC

Add a reply

By posting a reply on CodersClan you agree to our Terms & Conditions