1

I am using Shortcode in Wordpress also using Ninja Tables. The code is pulling from a Google Spreadsheet managed by a non-developer admin person. Generally easy. I am looking to embed this in a template rather installing the shortcode manually on all posts.

Following is the shortcode snippit I am using:

[ninja_tables id="446" search=0 filter="1100" filter_column="Filter4" columns="name,address,city,website,facebook"]

I am looking for a way that the shortcode can use a variable in place of the filter value of "1100" so that it automatically fills that with the post ID of the current post. This eliminates the need to hand key the short code on the post. I would then manually add the post ID into the spreadsheet which is far easier to maintain.

I have found references to PHP programming but am not a PHP developer and not trying to get so deep into development solutions. Hoping for a simpler solution with possible variables that can read page details into the shortcode.

New contributor
Rodney Biddle is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

2 Answers 2

2

Add this to your functions.php file:

function ninja_table_by_post_id() {
    $post_id = get_the_ID();

    $shortcode = '[ninja_tables id="446" search=0 filter="' . $post_id . '" filter_column="Filter4" columns="name,address,city,website,facebook"]';

    return do_shortcode($shortcode);
}
add_shortcode('current_post_table', 'ninja_table_by_post_id');

This code creates a new shortcode called [current_post_table] that when inserted into any post/page it'll render a table filtered out by the current post ID.

Usage:

<?php echo do_shortcode('[current_post_table]'); ?>
New contributor
edward avalos is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
2
  • 1
    Edward, welcome to Stack Overflow! Keep in mind though that you need to use English on this site (use an online translator if you must.) I took the liberty to translate your anwser to English but please keep that in mind next time / Edward, ¡bienvenido a Stack Overflow! Ten en cuenta que en este sitio web es requerido usar el idioma inglés. En esta oportunidad me tomé la libertad de traducir tu respuesta pero tenlo en cuenta para próximas oportunidades. Commented Apr 25 at 16:40
  • See Can I ask a question in a language other than English? for more details. Commented Apr 25 at 16:41
0

If you want to put this in a template (a .php file on the server, e.g. page.php), you can use this PHP snippet:

<?php
     $page_id = get_queried_object_id();
     echo do_shortcode('[ninja_tables id="446" search=0 filter="' . $page_id . '" filter_column="Filter4" columns="name,address,city,website,facebook"]');
?>

You can pass the $page_id into the shortcode within a do_shortcode function.

This would be placed in a PHP template or even potentially a global PHP/Code snippet if you're using some sort of page builder.

If you want an All-in-One solution that uses a new shortcode that can just be placed anywhere, Edward Avalos's answer would work great

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.