WordPress.org

Ready to get started?Download WordPress

Forums

TablePress
[resolved] Choose table to display in theme template using custom fields (4 posts)

  1. jenxi
    Member
    Posted 1 year ago #

    I would like to display a different table in the sidebar for each post. Is there a way to place the table code in the template and specify a custom field to choose the table to be shown?

    I know I can add the table for each post but I want to use the sidebar because it would be less likely to break the theme. I need to float the table to the right of the content.

    http://wordpress.org/extend/plugins/tablepress/

  2. TobiasBg
    Member
    Plugin Author

    Posted 1 year ago #

    Hi,

    thanks for your question.

    Yes, there are "Template Tag" functions that you can use anywhere in the theme. For more information on these, please take a look at the corresponding section in the documentation at http://tablepress.org/documentation/
    You could then combine that with some PHP code that retrieves the value of a custom field and inserts that as the table ID into the Template Tag function.

    Another idea would be to use a plugin like Widget Logic from http://wordpress.org/extend/plugins/widget-logic/ which allows you to show a different text widget on different posts/pages. You could then have different text widgets with a different table Shortcode each.

    Regards,
    Tobias

  3. jenxi
    Member
    Posted 1 year ago #

    Hi Tobias,

    Thanks for the quick response! The first option looks a little daunting with my limited knowledge of PHP. The widget looks like a promising solution but I read that anyone with access to it can possibly add malicious code. Since I would have multiple contributors who need to be able to add the table in this way, it is a likely vulnerability that I would rather not risk.

    The main obstacle is combining the table template tag with the custom function code.

    get_post_meta($post->ID, 'key', true);

    tablepress_print_table( array( 'id' => '1', 'use_datatables' => true, 'print_name' => false ) )

  4. TobiasBg
    Member
    Plugin Author

    Posted 1 year ago #

    Hi,

    yes, the first option would require some small PHP code in the sidebar area of the theme, while the second method would indeed open the possibility of entering malicious PHP code.

    However, for the first option, combining the custom field with the Template Tag function is not that difficult:

    $table_id = get_post_meta( $post->ID, 'sidebar_tablepress_table_id', true );
    if ( ! empty( $table_id ) )
      tablepress_print_table( array( 'id' => $table_id ) );

    With that, you would just have to add a custom field named sidebar_tablepress_table_id to the post.

    Regards,
    Tobias

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic