Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    You can format an URL like so:
    example.com/?category_name=service-learning,sl

    Obviously not the “pretty” style of permalink. It would be possible to have a pretty permalink with two categories, but you’d need a custom rewrite rule to transform the pretty style to the plain style. The transformation is not visible to end users, it happens internally.

    The other problem is the archive page’s title will only show the first category name in the request, not both. If you need both to appear in the title then the related template code would need modification to get both category slugs from the related WP_Query query var. With the slugs, get the related term objects in order to get the proper category names for display use.

    Thread Starter aktivbuerger

    (@aktivbuerger)

    Thanks a lot, @bcworkz!
    This almost solves my problem, but can you transform this into shortcut / php-code?
    We use a widget with [archivrubriken]
    and this code in the functions.php:

    add_shortcode('archivrubriken', 'wp_archivecategories');
    function wp_archivecategories()
    $cat_ids = array('11', '210', '716', '13', '14', '17', '18', '722', '715', '720');
    $arcats = array();
    $content2='<ul>';
    //11, 210, 716, 13, 14, 17, 18, 722, 715, 720
    foreach ($cat_ids as $cat_id) {
    //array_push($arcats, get_category($cat_id));
    $arcat = get_category($cat_id);
    $arcat_link = get_category_link($cat_id);
    $content2 .= "<li class='archive_links'><a href=".$arcat_link.">" .$arcat->cat_name. "</a></li>";
    }
    $content2 .= '</ul>';
    return $content2;
    }

    Here I want to combine cat-id 13 with cat-id 727 and cat-id 14 with cat-id 728.

    Kind regards,
    Regina.

    Moderator bcworkz

    (@bcworkz)

    Create a conditional statement for where you assign an URL to $arcat_link. If $cat_id is either 13 or 14, assign to $arcat_link an URL formatted similar to my example. You can get the name for term IDs 13 or 14 from $arcat->name. You can do a similar get_category() call for term IDs 727 and 728.

    If $cat_id is not 13 or 14, do the usual get_category_link() call.

    You’ll want either another if/else conditional or a switch/case structure nested within the if $cat_id is either 13 or 14 section to decide whether to append the term name of either ID 727 or 728.

    All the necessary logic could instead all be contained within a single switch/case structure. No if/else logic is really needed. Use whichever approach you’re most familiar with.

    Thread Starter aktivbuerger

    (@aktivbuerger)

    Thanks you,?@bcworkz!

    I didn’t write the code and I’m not a PHP specialist, but I’ll try to follow your steps.

    Kind regards,
    Regina.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘combine two categories for one archive page?’ is closed to new replies.