Funzioni & Plugin per WordPress Template

Aprile 11, 2011 2 Commenti

Articolo Precedente

Articolo Successivo

Ecco la quarta e ultima parte del tutorial su come sviluppare un template di WordPress, spiega funzioni utili e plugin utili.

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

Funzioni Utili

Ecco alcune funzioni di utilità generale, torneranno sicuramente utili:

wp_head(); // Attach header
wp_footer(); // Attach footer
get_sidebar(); // Attach sidebar
 
get_option('home'); // Returns home url
get_bloginfo('url');
get_bloginfo('template_url');
$GLOBALS[$lang_home] = split("\?", get_option('home'));  // $lang_home[0] is like bloginfo('url') but without url variables
 
get_cat_name($cat_id);
get_cat_ID($cat_name);
single_term_title("", false); // Returns the CURRENT category title
get_category_link($cat_id);
get_category_parents($cat_id, FALSE, ' - ', FALSE); // arguments: (category), (display link), (separator), (nice name)  
 
// get_page id from name
function get_page_id($page_name){
	global $wpdb;
	$page_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."'");
	return $page_name;
}
 
// print categories sorted by id
$subtitle = '';
$cats = get_the_category();
for($i=0;$i<count($cats);$i++){
	$subtitlearr[$cats[$i]->cat_ID]=$cats[$i]->cat_name;
}
ksort($subtitlearr);
$i=0;
foreach ($subtitlearr as $key => $value) {
	$i++;
	$subtitle .= $value;
	if($i!=(count($subtitlearr))){$subtitle .=" - ";}
}
 
// current cat also on posts
$var = get_the_category();
$cat = $var[0]->cat_ID;
$var=wp_list_categories('current_category='.$cat);
 
// get_the_content_with_formatting() for storing and editing the_content() inside a variable
function get_the_content_with_formatting ($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
	$content = get_the_content($more_link_text, $stripteaser, $more_file);
	$content = apply_filters('the_content', $content);
	$content = str_replace(']]>', ']]&gt;', $content);
	return $content;
}
 
// WPML functions
ICL_LANGUAGE_CODE // Actual language code
icl_object_id(3, 'category', false, 'en'); // arguments: (the ID of the post, page, tag or category), (type – ‘post’, ‘page’, ‘post_tag’ or ‘category’), (return_original_if_missing), (language code to return id)
 
// get page meta for WPML, useful if you are storing some general data into pages
$t_lang = icl_object_id('268',  'page', false);
$desc = get_post_meta($t_lang, 'desc-machines', true);
echo $desc;
 
// Translate redirect with WPML, useful to put inside index.php if you want redirection only in index
$browser_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
$ref = split("\?", get_bloginfo('url'));
$ref2 = split("\?", $_SERVER['HTTP_REFERER']);
if(is_home()){
	if (($browser_lang != ICL_LANGUAGE_CODE) && (!stristr($ref2[0], $ref[0])) ) {
		$languages = icl_get_languages('skip_missing=1');
		foreach($languages as $l){
			if ($browser_lang == $l['language_code']) { /* native_name */
				header('Location:'.$ref[0].'?lang='.$l['language_code']); exit;
			}
		}
	}
}
 
// Find ID of Top-Most Parent Post
if ($post->post_parent)	{
	$ancestors=get_post_ancestors($post->ID);
	$root=count($ancestors)-1;
	$parent = $ancestors[$root];
} else {
	$parent = $post->ID;
}
 
// Breadcrumbs
echo '<ul>';
if (!is_home()) {
	echo '<li><a href="';
	echo get_option('home');
	echo '">';
	echo 'Home';
	echo "</a></li>";
	if (is_category() || is_single()) {
		echo '<li>';
		the_category(' </li><li> ');
		if (is_single()) {
			echo "</li><li>";
			the_title();
			echo '</li>';
		}
	} elseif (is_page()) {
		echo '<li>';
		echo the_title();
		echo '</li>';
	}
}
elseif (is_tag()) {single_tag_title();}
elseif (is_day()) {echo"<li>Archive for "; the_time('F jS, Y'); echo'</li>';}
elseif (is_month()) {echo"<li>Archive for "; the_time('F, Y'); echo'</li>';}
elseif (is_year()) {echo"<li>Archive for "; the_time('Y'); echo'</li>';}
elseif (is_author()) {echo"<li>Author Archive"; echo'</li>';}
elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "<li>Blog Archives"; echo'</li>';}
elseif (is_search()) {echo"<li>Search Results"; echo'</li>';}
echo '</ul>';
Plugin Utili

Ecco alcuni dei più utili plugin per WordPress:

  • WPML Multilingual: Plugin per supportare multiple lingue nel sito.
  • Magic Fields: Plugin che arricchisce il gestionale di WordPress.
  • WP Minify: Questo plugin minimizza i CSS e i JS dei tema di WordPress, velocizza il caricamento delle pagine.
  • WP Super Cache: Un motore di caching molto veloce che produce file html statici, velocizza il caricamento delle pagine.
  • WP htaccess Control: Interfaccia per customizzare i permalinks e il file htaccess generato da WordPress.
Il Tutorial

Fine della qurta e ultima parte del Tutorial di WordPress Template.

Commenti

    C3b3341e2f5d057abb7d8fd562fd048c?s=40&amp;d=http%3A%2F%2F0.gravatar
    C20cbf43e2c329d40416d0a254334c81?s=40&amp;d=http%3A%2F%2F0.gravatar

Lascia un commento