Contenuti WordPress Template

Aprile 09, 2011 Nessuno Commento

Articolo Precedente

Articolo Successivo

Ecco la seconda parte del tutorial su come sviluppare un template di WordPress, spiega il loop, campi personalizzati e le categorie.

Per accedere alle altre parti del tutorial vedi il post Tutorial di WordPress Template.

Il Loop

Ci sono differenti metodi per prendere i dati dei post, di default sono accessibili all’interno del “loop”, che è il codice all’interno del file di template che analizza i dati dati da WordPress di default. Ha questa forma:

if (have_posts()) :
  while (have_posts()) : the_post();
  // Inside The Loop is here
  endwhile;
else :
// Non trovato
endif;

All’interno del “loop” si può accedere ai dati dei post, i post sono filtrati automaticamente a seconda del strong>file di template su cui si sta lavorando.

Tutti i dati sono accessibili all’interno del “loop” usando particolari funzioni chiamate template tags, eccone alcune:

the_ID();
the_author(); get_the_author();
the_author_link(); get_the_author_link();
the_title(); get_the_title($post->ID);
the_permalink(); get_permalink($post->ID);
the_time('F jS, Y');
the_content(); the_excerpt();
the_tags('Tags: ', ', ', '<br />');
the_category(', '); get_the_category();
comments_popup_link('No Comments', '1 Comment', '% Comments');

O all’interno dell’oggetto $post:

$post->post_author;
$post->post_date; $post->post_date_gmt;
$post->post_content;
$post->post_title;
$post->post_excerpt;
$post->post_name;
$post->post_modified; $post->post_modified_gmt;
$post->post_parent;
$post->guid;
$post->menu_order;
$post->post_type;
$post->comment_count;
Campi Personalizzati

Sempre all’interno del loop, puoi accedere ai dati dei campi personalizzati di un post, con questo codice:

// Dump out all custom fields as a list
the_meta();
 
// Display value of one specific custom field
$meta = get_post_meta($post->ID, 'CustomFieldName', true);
if ($meta) {}
 
// Display multiple values of same custom field name
$metas = get_post_meta($post->ID, 'CustomFieldName', false); sort($metas);
foreach($metas as $meta) {
	echo '<li>'.$meta.'</li>';
}

Ma se stai usando i Magic Fields si può accedere ai dati dei campi personalizzati con questo codice (qui per maggiori informazioni):

get('field_name');
Le Categorie

Ci sono alcune funzioni di base per lavorare con le categorie.

La prima è get_the_category che viene usata per accedere alle categorie dei post nella pagina corrente:

// get_the_category
$category=get_the_category($post->ID); // Optional argument the post ID
echo $category[0]->cat_ID;
echo $category[0]->cat_name;
 
// You can also loop trought the results
foreach((get_the_category()) as $category) {
  echo $category->cat_ID;
  echo $category->cat_name;
  echo $category->category_nicename; // slug
  echo $category->category_description;
  echo $category->category_parent; // 0 = no parents
  echo $category->category_count;
}

La seconda funzione è get_categories che si usa quando si deve eseguire una query più complessa.

// get_categories
$args = array(
    'type' => 'post',
    'child_of' => 0, // Display all categories that are descendants of category ID
    'parent' => 0, // Display all categories that are 1 depth descendants of category ID
    'orderby' => 'name', // id, name, slug, count, group
    'order' => 'ASC', // asc or desc
    'hide_empty' => 1, // 1 or 0
    'hierarchical' => 1,
    'exclude' => '', // List of categories by ID, in ascending order
    'include' => '', // List of categories by ID, in ascending order
    'number' => 10, // The number of categories to return
    'taxonomy' => 'category' // Taxonomy to return. category or taxonomy
    'pad_counts' => false
);
$categories = get_categories( $args );
foreach($categories as $category) {
  echo $category->cat_ID;
  echo $category->cat_name;
  echo $category->category_nicename; // slug
  echo $category->category_description;
  echo $category->category_parent; // 0 = no parents
  echo $category->category_count;
}

La terza funzione è wp_list_categories che mostra TUTTE le categorie come link, molto utile se si deve costruire il menu del sito.

$args = array(
    'show_option_all'    => '',
    'orderby'            => 'id', // id, name, slug, count, group
    'order'              => 'ASC', // asc or desc
    'show_last_update'   => 0, // 1 or 0
    'style'              => 'list', // list or none
    'show_count'         => 0, // 1 or 0
    'hide_empty'         => 1, // 1 or 0
    'use_desc_for_title' => 0, // 1 or 0
    'child_of'           => 0, // Display all categories that are descendants of category ID
    'exclude'            => '', // List of categories by ID, in ascending order
    'exclude_tree'       => '', // List of categories by ID, in ascending order
    'include'            => '', // List of categories by ID, in ascending order
    'hierarchical'       => 1, // Display sub-categories as inner list items. 1 or 0
    'title_li'           => __( '' ), // Set the title and style of the outer list item
    'number'             => NULL, // The number of categories to return
    'echo'               => 0, // Show the result (1) or keep it in a variable (0)
    'depth'              => 1, // 0: All Categories and child Categories, 1: Show only top level Categories, n
    'current_category'   => 0, // Allows you to force the "current-cat" 
    'pad_counts'         => 0,
    'taxonomy'           => 'category' // Taxonomy to return. category or taxonomy
);
$categories = wp_list_categories( $args );
$categories = str_replace('View all posts filed under ', '', $categories); // Useful if you have to change something
echo $categories;
Il Tutorial

Fine della seconda parte del Tutorial di WordPress Template.

La prossima parte del tutorial coprirà custom queries, il WP_Query, il get_posts e il query_posts.

Commenti

Lascia un commento