Eigen taxonomieën tonen in de WordPress-bericht-pdf
Een overzichtelijke en informatieve ervaring op je WordPress-site betekent vaak dat je gedetailleerde informatie geordend presenteert. Een manier om dat te verbeteren, is eigen taxonomieën in je WordPress-berichten te tonen, vooral als die berichten als pdf worden geëxporteerd. Eigen taxonomieën zijn een krachtig hulpmiddel om inhoud te categoriseren en te taggen op een manier die bij je site past, of het nu om producten, diensten of andere inhoud gaat.
In deze handleiding gaan we in op het tonen van eigen taxonomieën in geëxporteerde WordPress-bericht-pdf’s. Deze functie is vooral handig voor site-eigenaren die downloadbare inhoud aanbieden, zoals productcatalogi, informatieve artikelen of handleidingen, in een pdf die alle belangrijke categorieën en tags behoudt. We behandelen de nodige stappen, van het definiëren van je eigen taxonomieën tot het aanpassen van je pdf-uitvoer. Of je nu net met WordPress begint of een ervaren ontwikkelaar bent: met deze handleiding verbeter je de presentatie en bruikbaarheid van de inhoud van je site.
Je hebt onze plugin WP Posts to PDF nodig. Ga daarna naar het functions.php van je child theme en voeg de code hieronder toe. Om de code aan te passen, wijzig je de taxonomie-keys in de variabele $taxonomies.
// 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;
});