Created
May 28, 2019 15:20
-
-
Save Garconis/88a9ae92898feab597db19dc47e34636 to your computer and use it in GitHub Desktop.
WordPress | Create a "Go Back" link based on what category the Project belongs to
This file contains 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
<?php | |
add_shortcode( 'fs-project-go-back', 'fs_product_go_back_shortcode' ); | |
function fs_product_go_back_shortcode( $atts ) { | |
// begin output buffering | |
ob_start(); | |
// grab the terms of the product's attribute | |
$project_cats = get_the_terms($post->ID, 'project_category'); | |
//$count = count($project_cats); | |
echo '<div class="fs-project-go-back">'; | |
// if in a Project with any category | |
if ( ! empty( $project_cats ) && ! is_wp_error( $project_cats ) ){ | |
/* instead of a foreach (incase it has multiple categories), this only grabs the first one... */ | |
reset($project_cats); // make sure you are on first occ | |
$project_cat = $project_cats[key($project_cats)]; | |
/* | |
foreach ( $project_cats as $project_cat ) { | |
$project_cat_name = $project_cat->name; | |
$project_cat_slug = $project_cat->slug; | |
} | |
*/ | |
$project_cat_name = $project_cat->name; | |
$project_cat_slug = $project_cat->slug; | |
if ($project_cat_slug == 'association-non-profit') { | |
$url = '/projects/associations-nonprofit/'; | |
} | |
elseif ($project_cat_slug == 'corporate') { | |
$url = '/projects/corporate/'; | |
} | |
elseif ($project_cat_slug == 'law-firm') { | |
$url = '/projects/law/'; | |
} | |
elseif ($project_cat_slug == 'renovations-capital-improvements') { | |
$url = '/projects/renovations-capital-improvements/'; | |
} | |
else { | |
$url = '/projects/all/'; | |
} | |
echo '<a href="'. $url .'"><img src="/wp-content/uploads/go-back-icon-02.png" alt="Go Back icon" title="Go Back"></a>'; | |
} | |
// else not in a Project CPT | |
else { | |
echo '<a href="/projects/"><img src="/wp-content/uploads/go-back-icon-02.png" alt="Go Back icon" title="Go Back"></a>'; | |
} | |
echo '</div>'; | |
// end output buffering, grab the buffer contents, and empty the buffer | |
return ob_get_clean(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment