Support

Account

Home Forums General Issues Portfolio Taxonomy: Names of Selcted Categories Under Custom Field

Solving

Portfolio Taxonomy: Names of Selcted Categories Under Custom Field

  • I just know this question has been asked, so many pardons for not finding the answer already.

    I am brand new with no coding skills so I started by choosing the taxonomy of my theme’s portfolio (taxonomy: portfolio-category) and the checkbox option for field type.

    My logic was to eventually associate the category/tag names with the urls in that taxonomy since I don’t code and it would seem better if I could call up a category/tag archive function that has already been made.

    However when I use

    $values = get_field(‘media’);
    if( count($values)){
    foreach($values as $k=>$value){
    if($k) echo ‘, ‘;
    echo $value;
    }
    }

    I get what looks like ids

    208, 210, 214.

    If I use echo $value->name, I get the commas so clearly it is running through something but the execution type is not appropriate in this instance of echo, possibly because I’m using a taxonomy rather than a custom field type (sorry, I’m new so my vocabulary is probably a little convoluted).

    A variable dump yields the result

    strings(5)”media”

    5 being the total number of options available to choose, so I can tell the field is being read differently under taxonomy, but I’ve already tried sub_field. There doesn’t seem to be any?

    So, is there anything else I can try? Thanks in advance.

  • To clarify now that I understand more:

    I am using my theme’s existing custom taxonomy portfolio-category, but I don’t know how to perform a var_dump of that field (assuming it’s a field and not a subfield) outside of the loop. I want to eventually be able to use each category I assign via checkbox a link, and to use my portfolio’s existing archive view to view the posts under that category.

    In my theme:
    I have one category in my theme’s ‘portfolio-category’ named ‘media’ (slug is also media) and five children assigned under that category:
    Acrylic (slug: acrylic-media)
    Textured Acrylic (slug: textured-acrylic-media)
    Digital (slug: digital-media)
    Prismacolor (slug: prismacolor)

    In ACF
    Field label: Media
    field name (slug?): media
    field type: Taxonomy
    Taxonomy: portfolio_category
    Field Type: Checkbox
    Term is ID, not object.
    Null is currently not allowed

    Since I’m using a taxonomy, using anything like [‘label’] would be out of the question (right?), which is why any of the sub-field snippits I tried did not work, and that a value of 5 from the var_dump was because there is 1 field and 4 sub-fields in my theme’s taxonomy assigned the field ‘media’. So if this method is too complex, then I could resort to another option and attempt to create my own archive function in the (probably distant) future…

    If checkbox were chosen instead of taxonomy, I could assign each category a label and a variable in the choices fields in ACF

    So something like

    acrylic: https://www.mysite.com/acrylic
    prismacolor: https://www.mysite.com/prismacolor
    etc.

    would work if I wanted to set a value to an ‘href’. But…

    I would have to call each subfield by name to retrieve the values from that field, right? Or would there be any way to assign a x-variable type value so I can just add what I wanted to ACF as I add more subfields? A pain, but not quite as much writing code for every subfield in addition to adding to ACF and therefore having two areas to edit.

  • Pardon me for giving this another bump, but the more I read, the more it looks like purchasing the pro version might be the better solution…the linking option looks as if it might solve my problem?

    Could someone with little to no coding skills make use of it? Again, pardon my ignorance.

  • I’ve read all of your comments, but I may not understand what you’re looking for.

    I’m really not sure what you mean by “the linking option” in the pro version.

    You are using a taxonomy field and you want to output links to the terms that have been selected?

    If this is true then how you would code this depends on what you have the Return Value of the field set to. You would most likely be better off returning a term object.

    
    $terms = get_field('media');
    if ($terms) {
      foreach ($terms as $term) {
        ?>
          <a href="<?php echo get_term_link($term); ?>"><?php echo $term->name; ?></a>
        <?php 
      }  // end foreach
    } // end if
    
  • Thanks, John.

    Sorry…I’m a bit cloudy about understanding exactly what ACF Pro does. I was confusing the flexible content field with something that would allow me to add a link to each choice, but further reading is leading me down a rabbit hole, so maybe it’s not the answer.

    Yes, that code works for links. So would I also use something like

    <a href="<?php echo get_term_link($term); ?> . echo $field['label'] . </a>

    to show the link as the name of the category? It’s not working so something isn’t right in my syntax. I’m a little confused as to when to use single quotes to indicate switching to html or single out items not native to php, and when to use the dots. Thanks again. You’ve helped tremendously already.

  • As far as coding goes, some people might consider me a little anal when it comes to code, for example, if I’m outputting HTML then I always close out of PHP before doing so unless there is some specific reason not to. I don’t like using echo to output HTML because that makes the HTML difficult to read. There are many ways that coding can be done.

    If you want to show the field label, then you’ll also need to get the field object, but I don’t think that’s what you really want to do. That would mean you’d have the same label for every term link. From your first few post it looks to me that you want to show the term name for each link and not the ACF field label.

  • Oh, interesting approach regarding the closing out of php. I like it. Kind of like formal writing avoids contractions. Probably a good approach for me too since I’m just learning the intricacies of php but am fine with most html.

    Name instead of label… So that’s how taxonomy’s handle things differently then? I was using a snippit for handling custom fields. I tried using a snippit my own theme uses as well…
    <?php echo get_the_term_list( $post->ID, 'portfolio_skills', '', '<br />', '' ); ?>
    (this time closing the tags rather than mess with all the single quotes…especially since I’m not worried about spaces and line breaks right now)

    However, since ‘portfolio_skills’ (I used ‘media’ instead) is my theme’s own custom field and probably not a taxonomy per se (albeit the values are user generated), maybe $terms->ID would be better $terms->NAME or will I break the theme again?

    Maybe a better question is to ask what would be a good resource for learning and practicing php? I’m like the person learning a new language asking for a “tall refreshing glass of water”. I know that I’ll get a glass of water without understanding that “refreshing” is superfluous and “tall” is too specific for my needs.

    Thanks again for all your efforts. I know it’s a labor of love teaching without pay.

  • I finally had to give up and use the Toolset Types plugin, which has some of the ACF function and inserts some of the custom field code into the wordpress itself. I can see two problems with this:

    1. It’s doing the dirty work for me. I still don’t know where it is putting the code or how it works.

    2. If I make a mistake or change my name about which field name to use, it could leave a trail of unused fields and code. I don’t know if that’s what it does, but I have no way of verifying either because I don’t know where it is inserting code into the theme.

    But after using it, I could use this snippit

     <?php if (get_the_term_list($post->ID, 'media-type', '', '<br />', '')): ?>
      <div>
      <h4><?php esc_html_e('Media:'); ?></h4>
      <div>
      <?php
      echo get_the_term_list($post->ID, 'media-type', '', ',&nbsp;', '');
    ?>
                                   </div>
                                </div>
    <?php endif; ?> 

    because now I have another custom category type built into my wordpress (right along side the existing “category” and “tag” options in wordpress).

    I also found out that the spaces (”) in places like this
    echo get_the_term_list($post->ID, 'media-type', '', ',&nbsp;', '');

    actually serve as a substitute for the

    if( count($values)){
        foreach($values as $k=>$value){
            if($k) echo ',  ';
           echo $value;
        }
    }

    part of the code and that it isn’t a good idea to use “if (count($values))” if the value is ever empty, but some of the problems associated with using anything with “empty()” was way over my head. Maybe someone can come along later and explain how someone could have an archive link (that part was solved by john) associated with the category name in a theme’s custom portfolio taxonomy.

Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Portfolio Taxonomy: Names of Selcted Categories Under Custom Field’ is closed to new replies.