Created
October 13, 2015 18:45
-
-
Save thotbox/52bc00d4a8ba65a24bf7 to your computer and use it in GitHub Desktop.
PHP: Wordpress URL Segments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add to header or function file: | |
<?php | |
// URL Segments and Master ID | |
// | |
// Segment Variables: $GLOBALS['segment']['1'], $GLOBALS['segment']['2'], etc. | |
// Master ID Variable: $GLOBALS['master_id'] | |
global $segment; | |
global $master_id; | |
$segment = explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)); | |
$last_segment_array = array_slice($segment, -2, 1, true); | |
$last_segment = array_shift($last_segment_array); | |
$master_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$last_segment'"); | |
?> | |
Example Usage: | |
<?php if ($GLOBALS['segment']['1'] == 'test-1') : ?> | |
<!-- If True --> | |
<?php endif; ?> | |
<?php if ($GLOBALS['segment']['1'] == 'test-1' && $GLOBALS['segment']['2'] == 'test-2') : ?> | |
<!-- If True --> | |
<?php endif; ?> | |
<?php if ($GLOBALS['master_id'] == '10') : ?> | |
<!-- If True --> | |
<?php endif; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment