Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Liam Maclachlan
Liam Maclachlan
22,805 Points

Sharing a solution for unnecessary 'single-slug.php' pages in ACF, when using post type as a slider.

So, this got a bit more complicated that I thought it would.

Firstof all I was happy with

<?php 
    header( 'location:' . site_URL() );
?>

But I quickly got annoyed of being redirected to a page where the banner didn't reside. So I only ever had 2 categories per slider. One of them, where needed, was always going to be a vendor the other was either 'slider' or 'home'. Or if neither where a vednor then the homepage would need to be chosen.

Here is my solution

<?php
    function string_to_slug($varOld) {
        $varNew = trim( strtolower( $varOld ) );
        $varNew = preg_replace('/[^a-zA-Z0-9\s]/', '', $varNew);
        $varNew = str_replace(' ', '-', $varNew);
        return $varNew;
    }
    // builds category array
    $c = get_the_category();
    $d = string_to_slug( $c[0]->cat_name );
    $e = string_to_slug( $c[1]->cat_name );
    if ( $d == 'home' && $e != 'slider' ) {
        header( 'location: ' . site_URL() . '/vendor/' . $e );
        exit;

    } elseif ($d != 'home' && $e == 'slider') {
        header( 'location: ' . site_URL() . '/vendor/' . $d );
        exit;

    } else {
        header( 'location: ' . site_URL() );
    }
?>

What do you think? :)

1 Answer