• Resolved MediaDC

    (@mediadc)


    I have a question about how to use the templates for Query Wrangler that, if it’s not currently possible without a hack of some kind, will end up being a feature request.

    Scenario:
    I would like to employ the query templates to theme the look and function of the queries I’m creating. I might make a query called “Two Column Large Lead”, so the template’s file name in my theme would be “query-wrangler-two-column-large-lead.php”. For this query, I would want to filter my posts to only those matching the category of “Sports”. However, if I want to create another query, that uses a filter to show only posts matching the category of “Business”, the template it uses is based entirely upon the name I give the query. I would like to use the same template file for both queries, even though they are clearly different queries because they use different filters, and perhaps other differences in the configuration of the queries exist as well (post count, header/footer content, etc.). It’s possible for me to create two queries with the same name, and thus the same slug, so that they will both use the same template file, but if I do that, I have no way of knowing just by looking at the title of the query which is which. I would have to separately track the ID value of which query corresponds to which set of filters, in order to know which one I was using in widgets, pages, etc.

    Question:
    Is it possible for me to have two different queries, with two different names, that can both use the same template? I could not accomplish this by using merely the “query-wrapper.php” generic template, because I have several combinations of queries that need to use the same template, i.e “Sports” and “Business” queries use one template, “News” and “Politics” queries use another.

    Current “Solution”:
    The only way I can discern to make this work right now would be to get into the database and change the title of the query itself, but leave the slug as is. I believe this would effectively do the trick, but it doesn’t seem to be the best long term solution.

    Potential Solutions:

    1. If there is no other solution to this scenario using options that are already available in the plugin, it seems like one way to do this would be to add another option for the query that would allow the creator to choose from a list of available templates, that would be pulled from those automatically detected within the plugin’s default templates folder, as well as any that have been locally created in the theme directory.
    2. An additional option could be created for the query that would allow us to somehow categorize the query itself, and then automatically select a template that matched the name or ID of that category. In this solution, I might name the query “Business Articles”, and select a query category of “Two Column Large Lead”. The plugin would then look at the slug of the category and attempt to locate a corresponding template in my theme folder matching that slug, in the same way that it currently does using the slug of the query name.

    If another solution to this scenario exists that I’m not seeing, I’d love to know what it is, but even if there is one, I propose that one or both of the solutions I suggested above could be a very useful addition to the plugin in a future version.

    Thank you.

    https://wordpress.org/plugins/query-wrangler/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Jonathan Daggerhart

    (@daggerhart)

    Hi mediaDC,

    Both of those recommendations are good, when I have a bit more time I’ll consider how to best implement this as a UI feature. For now, I’ve updated the plugin to make this possible through a hook (1.5rc20).

    Template Wrangler allows for modification to the templating process at a couple of different points. What you want to do is to add new template suggestions during the pre_process hook.

    Here is a snippet of code that shows this as an example. This should work in a custom plugin, or in the theme’s function.php file.

    // add a custom function to the tw_pre_process_template filter
    add_filter('tw_pre_process_template', 'my_tw_pre_process_function');
    
    function my_tw_pre_process_function($template){
      // only alter the query_display_wrapper template suggestions
      if ($template['name'] == "query_display_wrapper"){
        // get the meta information of the qw_query
        $meta = $template['arguments']['options']['meta'];
    
        // only add the new suggestion to specific queries
        if ($meta['slug'] == "my-query-slug1" || $meta['slug'] == "my-query-slug2"){
          // make our new template suggestion first in line
          array_unshift($template['files'], 'query-wrapper--my-custom-wrapper-for-just-these-two-queries.php');
        }
      }
      return $template;
    }

    The name of the template file can be anything you want. And you can make the new suggestion to the templates based on anything as well. In this example I implemented the new suggestion based on the query slugs to match your question.

    Please let me know if you have any questions or trouble implementing your custom filter.

    Thanks,
    Jonathan

    Thread Starter MediaDC

    (@mediadc)

    Jonathan, this did the trick! I can now create queries with whatever names I want, and specify their templates in the theme’s functions.php file using further “if ($meta[‘slug’]…” statements.

    It seems that now that this is operating via apply_filters in the functions.php file, it might also be possible for me to change the widget’s template on the fly depending on what page I’m on. For example, if I was on the “Politics” page, I might want to change the template of the “Politics” QW widget in some way, i.e. changing the background color. It seems I could do this very easily through the functions.php file with an overriding if statement.

    I think this has given the template functionality of the plugin much more flexibility. I look forward to further releases that incorporate this more directly into the UI.

    Thank you!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Specify template for different queries’ is closed to new replies.