WordPress.org

Ready to get started?Download WordPress

Forums

Twenty Eleven
Adding custom Taxonomy to post meta (6 posts)

  1. Hugh
    Member
    Posted 1 year ago #

    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. (See it in a screenshot here: http://d.pr/i/EghK.)
    But:

    1. Not every post contains a term from one of my custom posts.
    2. 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:

    1. the use of variables like this: %3$s ; where or how they are defined?
    2. 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.
    3. What is the role of the printf( statement if there are several if-elseif statements?
    4. 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?
    5. 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_liststatement 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.

  2. linux4me2
    Member
    Posted 1 year ago #

    I don't have perfect answers for all your questions, but you've put so much effort into this and asking about it that I can't stand to see you go unanswered.

    1. I think those variables with the percent sign and number preceding them are placeholders. Take a look at the section "Messages with Placeholders"
    2. It looks like $utility_text is just a variable set on line 58 of your pastebin code.
    3. The printf function is a lot like echo in that it outputs text to the browser, but printf allows you to format the text. Have a look at the PHP manual here. What the code you have does is set the formatting string to the $utility_text using the placeholders in consecutive order, so %1$s would be $categories_list, %2$s would be $tag_list, etc.
    4. It's hard for me to answer this one, because it depends on when you want your taxonomies to display. It seems like you would always want them listed if they exist, wouldn't you? If that's the case, you could construct an independent conditional statements for them and add them in the format you desire if they aren't empty.
    5. I haven't seen underscores in PHP in a long, long time, and I have no idea why they are using them here. It used to be used to designate private functions, but the way you did it; i.e., without the underscore, is correct for current versions of PHP, I believe.
    6. I think you understand the get_the_category_list function correctly, it's their use of the underscore that's throwing you off. They're basically just using a comma and space for the separator.

    There are couple of things you might consider. First is that PHP has some really handy array functions. One use would be to get that "and" in there that you want. The get_the_category_list and others that return comma-separated strings can easily be used to create arrays using the PHP explode function, which you can then count using the count function, and iterate through using the foreach construct. Using a counter, you could add the " and" prior to finishing the string off with the last value.

    Since your experimental code is also redundant, you could even construct an array using the variable names for each group, then use a foreach loop to handle each pair. That would make your code much more efficient and easier to maintain.

  3. Hugh
    Member
    Posted 1 year ago #

    Thanks for the reply... it appears that the __() is being used for putting the translation in from the .po and .mo files. On of your links showed me that: http://codex.wordpress.org/Translating_WordPress

  4. Hugh
    Member
    Posted 1 year ago #

    It would seem that with as popular as custom meta and custom taxonomies are that there should be a plugin for adding custom meta to content.phpand content-single.php

  5. linux4me2
    Member
    Posted 1 year ago #

    Ah, good catch on the underscores. I missed that. Good to know.

    Here I thought you just wanted to roll your own. I'm surprised there aren't plugins that handle it too. When you're done with this, you could tackle writing one. : )

  6. Hugh
    Member
    Posted 1 year ago #

    Is there a hook I could use? cause I would rather do this via a plugin rather than through a child theme. It might be more code but there is the pro of transferability after a theme switch.

Topic Closed

This topic has been closed to new replies.

About this Theme

About this Topic