You recently provided me with some PHP code that could be used to provide the post/portfolio header title in the subject field of an e-mail link. See below that was added to the functions.php file. What I also need is to add to that subject field the Parent Category name of that post/portfolio.
Can you please help
<?php
function custom_shortcode_func() {
global $post;
ob_start();
?>
<a href="mailto: (Email address hidden if logged out) ?subject=Purchase Enquiry on <?php echo get_the_title($post->ID);?> Painting">Purchase Enquiry</a>
<?php
$output = ob_get_clean();
return $output;
}
add_shortcode('my_shortcode', 'custom_shortcode_func');
?>
Can someone please respond as this is getting urgent
Hi,
Please try using the code as following
function custom_shortcode_func() {
global $post;
$category = get_the_category();
$category_parent_id = $category[0]->category_parent;
if ( $category_parent_id != 0 ) {
$category_parent = get_term( $category_parent_id, 'category' );
$parent_cat_slug = $category_parent->slug;
} else {
$parent_cat_slug = $category[0]->slug;
}
ob_start();
?>
<a href="mailto: (Email address hidden if logged out) ?subject=Purchase Enquiry on <?php echo get_the_title($post->ID);?> Painting from <?php echo $parent_cat_slug; ?> category">Purchase Enquiry</a>
<?php
$output = ob_get_clean();
return $output;
}
add_shortcode('my_shortcode', 'custom_shortcode_func');
Best regards,
Yigit