WordPress.org

Ready to get started?Download WordPress

Forums

WP-Table Reloaded
[resolved] include category variable in Template Tag Funcion (9 posts)

  1. espo74
    Member
    Posted 2 years ago #

    Thank you so much for this work--I have had no problem getting everything to work as it should as well as adding in the necessary Extensions. The Documentation and FAQs have been of great help and this plugin is beyond excellent—proving to be better and better the more I dig into it.

    I am, though, trying something that is pushing me outside my PHP/WP skill set and hope that this is a simple answer from those with more talent.

    Scenario: I have a list of product numbers that correspond to a category tag. SJ1301, SJ1301A, SJ1301B, SJ1302, etc., etc. I have this bit of code currently which populates bits of text and pulls images based on that category

    $model_number_terms = wp_get_object_terms($post->ID, 'model_number');
    if(!empty($model_number_terms)){
      if(!is_wp_error( $model_number_terms )){
        foreach($model_number_terms as $term){
    echo ' <img src="folder/'.$term->name.'.png" alt="'.$term->name.'" / >
    ';
        }
      }
    }

    (in this case '.term->name.' returns SJ1301 )

    This works great and I have quite a bit of information building out on the pages with little effort on the data entry side--have it running some shortcodes, building links to documents, bunches of things for these pages...

    Where I am getting stuck is that I have a Specifications table which lists ALL of my products and I would like to filter this table based on the category/product name.

    When manually entered into the template this works perfectly:

    wp_table_reloaded_print_table( "id=4&use_tablesorter=true&print_name=false&filter=SJ1301||SJ1301A||SJ1301B" );

    What I would like is something that works similarly to how the images are pulling and builds the filter based on the Category:
    (again, using the example of '.term->name.' returning SJ1301)

    wp_table_reloaded_print_table( "id=4&use_tablesorter=true&print_name=false&filter=$term->name" );

    and, eventually, so that I could try to play the wild card for the product variations

    wp_table_reloaded_print_table( "id=4&use_tablesorter=true&print_name=false&filter=$term->name||$term->nameA||$term->nameB" );

    (displaying the rows for SJ1301, SJ1301A, SJ1301B)

    Again...my skills are not so great and I have been running in circles trying to accomplish this and, I am sure, looking for solutions in ways that just aren't correct. I am just not sure how to make this work...

    Thanks to anyone who has any advice!

    http://wordpress.org/extend/plugins/wp-table-reloaded/

  2. TobiasBg
    Member
    Plugin Author

    Posted 2 years ago #

    Hi,

    the way that you describe sounds reasonable, so I'm not sure what the problem is. You'll of course need paste those PHP calls into a page template. Or is that the problem (i.e. that you want to use this in a Shortcode)? What happens if you use your code?

    Regards,
    Tobias

  3. espo74
    Member
    Posted 2 years ago #

    Thanks for the reply!

    A shortcode would be ideal--very elegant--but I am fine with just using it in the template (which is what I am doing with the above code) that serves my purpose right now.

    The code seems like it should work...and logically it makes sense but this must be where I am not connecting a few PHP fundamentals correctly.

    I cannot figure out how to properly replace the echo string with the wp_table_reloaded code:

    <?php
    $model_number_terms = wp_get_object_terms($post->ID, 'model_number');
    if(!empty($model_number_terms)){
      if(!is_wp_error( $model_number_terms )){
        foreach($model_number_terms as $term){
    echo ' <img src="folder/'.$term->name.'.png" alt="'.$term->name.'" / >
    ';
        }
      }
    }
    
    ?>

    That gives me the image SJ1301.png

    <?php
    $model_number_terms = wp_get_object_terms($post->ID, 'model_number');
    if(!empty($model_number_terms)){
      if(!is_wp_error( $model_number_terms )){
        foreach($model_number_terms as $term){
    echo ' wp_table_reloaded_print_table( "id=4&use_tablesorter=true&print_name=false&filter=$term->name||KM1301SAH" );
    ';
        }
      }
    }
    
    ?>

    That, as one would assume, just returns the entire string as text.

    I have tried a myriad of other variations without the echo or trying to replace it but have had no success--again, going back to my inexperience with PHP/WP coding. I am sure it is something I am overlooking, I am just not sure what or where....

  4. espo74
    Member
    Posted 2 years ago #

    A little more research and a previous post here led me to using echo do_shortcode and I found that it works.

    $model_number_terms = wp_get_object_terms($post->ID, 'model_number');
    if(!empty($model_number_terms)){
      if(!is_wp_error( $model_number_terms )){
        foreach($model_number_terms as $term){
    
    echo do_shortcode( '[table id=1 filter='.$term->name.'A||'.$term->name.'B]' );
        }
    
      }
    }

    And then--I get an error if my table has a footer row assigned to it "no data found" although it displays the correct filtered table row...But I can live with that and work around it.

    WooHoo!

  5. TobiasBg
    Member
    Plugin Author

    Posted 2 years ago #

    Hi,

    thanks for the further explanations. Yes, do_shortcode() is then indeed the better solution. Although, actually you would just need to remove the echo (and single quotation marks) in the first try. The wp_table_reloaded_print_table() function will already print the table, so no extra echo is no necessary.

    One ore thing, you might want to do (in case you haven't already): Uncheck the "Cache Table Output" checkbox on your table's "Edit" screen.

    About that "No data found" error: I'm not sure where this comes from. Can you please post the link to the page with the table where this is visible?

    Regards,
    Tobias

  6. espo74
    Member
    Posted 2 years ago #

    Tobias!

    Thanks a ton. the wp_table_reloaded_print_table() actually isn't working with: $term->name when followed by anything. I believe that is what is causing that "No data found" error.

    However, with do_shortcode() the '.$term->name.'A||'.$term->name.'B works just perfectly. Although the footer row does not display at all (header displays just fine). Again, not an issue I can work around it.

    Question: is do_shortcode more efficient than wp_table_reloaded_print_table?

  7. TobiasBg
    Member
    Plugin Author

    Posted 2 years ago #

    Hi,

    the problem with wp_table_reloaded_print_table() likely is just an issue with string concatenation.
    Just use do_shortcode(), as that should be better readable.

    And don't worry about the performance, the functions are equally fast.

    Best wishes,
    Tobias

  8. espo74
    Member
    Posted 2 years ago #

    Right On Tobias...thanks for the help--I am heading over to that nice and friendly Paypal donation button.

  9. TobiasBg
    Member
    Plugin Author

    Posted 2 years ago #

    Hi,

    sure, no problem, you are very welcome!
    And also, thank you very much for the donation, it is much appreciated!

    Best wishes,
    Tobias

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic