Creating a seamless and informative experience for users on your WordPress site often involves presenting detailed information in an organized manner. One way to enhance this experience is by showing custom taxonomies in your WordPress posts, especially when these posts are exported as PDFs. Custom taxonomies in WordPress are a powerful tool for categorizing and tagging content in a way that’s specific to your site’s needs, be it for products, services, or any other type of content.

In this guide, we will delve into the process of displaying custom taxonomies in exported WordPress posts PDF. This feature is particularly useful for site owners who need to provide downloadable content, like product catalogs, informative articles, or resource guides, in a PDF format that retains all the essential categorization and tagging data. We’ll cover the necessary steps, from defining your custom taxonomies to modifying your PDF output, ensuring a comprehensive and user-friendly approach. Whether you’re a WordPress beginner or an experienced developer, this guide will equip you with the knowledge to enhance your site’s content presentation and usability.

Please note you need our wp posts to pdf plugin. Then go into your functions.php of your child theme and add the below code. To adapt the code change the taxonomy keys in the $taxonomies variable.

// Add a filter to modify the post metadata keys.
add_filter('wordpress_print_posts_meta_keys', function($tmp) {

    global $post; // Access the global post object.

    // Define an array of taxonomy slugs to process.
    $taxonomies = array(
        'vehica_6659'
    );

    // Iterate through each taxonomy.
    foreach($taxonomies as $taxonomy) {

        // Get terms associated with the current post and taxonomy.
        $terms = wp_get_object_terms($post->ID, $taxonomy);

        // Check if any terms are associated.
        if(!empty($terms)){

            // Retrieve the full taxonomy object.
            $taxonomy = get_taxonomy($taxonomy);

            // Initialize an array to store term names.
            $termNames = array();
            foreach($terms as $term){
                // Skip if the term name is not set or is empty.
                if(!isset($term->name) || empty($term->name)) {
                    continue;
                }
                // Add the term name to the array.
                $termNames[] = $term->name;
            }

            // Add a new array to $tmp with taxonomy label and term names.
            $tmp[] = array(
                'key' =>  $taxonomy->label,
                'before' => $taxonomy->label . ': ',
                'value' => implode(',', $termNames), // Combine term names into a string.
            );
        }
    }

    // Return the modified array.
    return $tmp;

});

Leave a Reply

Your email address will not be published. Required fields are marked *