WordPress.org

Ready to get started?Download WordPress

Forums

Slugs PHP Categories and substr functions that I don't understand. (1 post)

  1. dmiranda9872
    Member
    Posted 1 year ago #

    Hi Folks,

    I'm attempting to create a new category template in someone else's code.
    I'm unfamiliar with how the original programmer is using slugs and categories in wordpress.
    Could someone please explain how this code opens a "tour" in a specific single template:

    
    <?php // check to see if this is a tour menu page ?>
    <?php if(is_tour(the_slug(false,'solo'))){
    	include('single-tour-template.php');
    } else { ?>
    <?php get_header(); ?>
    

    Its pretty clear that the is_tour function is doing something to decide that the tour page is the current page and it decides to use the "single-tour-template.php" I just can't figure out how to add a new template to this function without breaking all the other tours on the site. I'd be adding in say: 'single-tour-template-new.php'.

    Here is the custom Function : is_tour

    
    function the_slug($echo = true, $slugtype = 'solo') {
    
    	$preslug = $_SERVER['REQUEST_URI'];
    	$fullSlug = substr($preslug,1,strlen($preslug));
    	substr_count($preslug,"/",1) > 0 ? $slug_length = strpos($preslug,"/",1) : $slug_length = strlen($preslug);
    	$rootSlug = substr($preslug,1,$slug_length-1);
    	$slug = substr($preslug, strrpos($preslug,"/")+1); 
    
    	switch ($slugtype) {
    		case 'solo':
    			$out = $slug;
    			break;
    		case 'root':
    			$out = $rootSlug;
    			break;
    		case 'full':
    			$out = $fullSlug;
    			break;
    	}
    
    	if ( $echo ){
    		echo $out;
    	} else {
    		return $out;
    	}
    }
    
    

    as well as this I believe:

    
    function is_tour($slug){
    	global $wpdb;
    	$is_tour = false;
    	$post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$slug'");
    	$cats = get_the_category($post_id);
    	foreach($cats as $cat){
    		if($cat->slug == 'tour-itinerary'){
    			$is_tour = true;
    			break;
    		}
    	}
    
    	return $is_tour;
    }
    

    I tried a conditional statement that would make a server request which would pull in the current URL and if it was on the specific page I put in the code then it would use the new template. It appears that the PHP code only reads the IS_tour function and doesn't get to the elseif part of the code.

    
    <?php if(is_tour(the_slug(false,'solo'))){
    	include('single-tour-template.php');
    } elseif ($_SERVER['REQUEST_URI'] == '/destinations/quebec-city/3-day-french-trips-chartered-by-motorcoach-springsummer'){
    	include('single-tour-template-new.php');
    }
    	else { ?>
    

    Is there someway I can do both? An if statement that will look at both and choose the URL option instead of the 'solo' option?

    If anyone could help me with even a basic understand of what the is_tour function is I'd greatly appreciate it!

    I've been stuck on this for a couple days now.

    Link to the Destination page with the tours at the bottom:
    http://visitcanada.com/destinations/quebec-city
    Look at the blue links at the bottom the tour I'm changing is the first one down.

    Link to tour I'm building a new template for:
    http://visitcanada.com/destinations/quebec-city/3-day-french-trips-chartered-by-motorcoach-springsummer

    Please let me know if you need anymore details.

    Thank you again,
    D

Topic Closed

This topic has been closed to new replies.

About this Topic