I have created several custom taxonomies. I have even gotten them to display in the post single page (generally following this tutorial) using my child theme.
But:
- Not every post contains a term from one of my custom taxonomies.
- And some posts contain more than one term from one of my custom taxonomies. So I would like to have "and" inserted before the last term of the list if the list is greater than 1 term. eg. term1, term2, and term3
So, I need to do some magic with an if{ elseif{
kind of stuff but I am not sure exactly what the syntax should be or how to get it there.
Currently I have:
<?php
// We need to tell WordPress to look for our custom taxonomy lists
$hp3_topic_lang_text = "";
// Then we need to tell it which one we want in particular and store it for use in our code.
$hp3_top_lang_list = get_the_term_list( $term->ID, 'hp3_top_lang', 'and contains information about ', ', ', '' );
if ( '' != $hp3_top_lang_list ) {
$hp3_topic_lang_text .= "$hp3_top_lang_list\n";
}
// Now we need to tell WordPress exactly how we want to display the tag
if ( '' != $hp3_topic_lang_text ) {
?>
<!--// This just let's WordPress know that there's an else argument. It will remove the 'In this post' if no artists have been tagged. --<
<?
}
?>
<?php
// We need to tell WordPress to look for our custom taxonomy lists
$hp3_content_lang_text = "";
// Then we need to tell it which one we want in particular and store it for use in our code.
$hp3_cont_lang_list = get_the_term_list( $term->ID, 'hp3_cont_lang', '<!-- <strong>This entry has content written in:</strong> --> ', ', ', '' );
if ( '' != $hp3_cont_lang_list ) {
$hp3_content_lang_text .= "$hp3_cont_lang_list\n";
}
// Now we need to tell WordPress exactly how we want to display the tag
if ( '' != $hp3_content_lang_text ) {
?>
<div class="entry-utility">
This entry is written in <?php echo $hp3_content_lang_text; echo $hp3_topic_lang_text; ?>.
</div>
<!--// This just let's WordPress know that there's an else argument. It will remove the 'In this post' if no artists have been tagged. -->
<?
}
?>
But this code I have used above seems really inefficient because it is basically two times the same thing instead of 1 time of one statement with two clauses... but maybe I need two statements I don't know...
When I look at the standard code which comes in the twentyeleven theme and it has some pretty cool magic but I am not sure I understand it. I have pasted the above code and the standard code from twentyeleven in a pastebin here and is the same code found here in the codex.
- There are several things I am not understanding about the code in the Twenty Eleven theme:
- the use of variables like this:
%3$s
; where or how they are defined? - How `$utility_text` is defined. - in my examples I have had to define my $hp3_topic_lang_text, but I do not see where this is getting its definition.
- What is the role of the `printf(` statement if there are several if-elseif statements?
- Since there are three statements, if - elseif - esleif, which cases do I need to account for in my additional statements? (assuming that I wanted to add more elseif statements to add my custom taxonomies.)
It looks to me like the code in the theme reads; " if tag list is not empty, then set to clause #1, but if it is empty but category list is not empty then set to clause #2, but alas if both tag list and category list are empty then set to clause #3. This makes me ask 2 more questions:
- What happens if there are tags but no categories, what is placed in the category variable's slot?
- how many more else if statement do I need to add if of my two new taxonomies either one is optional and they can co-occur?
- Lastly, can someone help me understand this phrase:
$categories_list = get_the_category_list( __( ', ', 'twentyeleven' ) );
especially__(
part, why is this different than what I am doing above here:
$hp3_cont_lang_list = get_the_term_list( $term->ID, 'hp3_cont_lang', '<!-- <strong>This entry has content written in:</strong> --> ', ', ', '' );
?The way I currently understand the `$categories_list`statement to read is: set the variable categories_list to the value of get_the_category_list(..... and that is where I lose it.
Thanks in advance.
http://wordpress.org/extend/themes/twentyeleven/