• Resolved netschmiede24

    (@netschmiede24)


    Hi again,

    I have a pod for a business directory where people are listed by name, their photo, company, address etc. – and also there are categories for the different industries.

    Now I have a template for an alphabetical list for these people where you can look at all the names and photos and click on them to see their details. I also have a filter for the industries, so that you can filter and only see the names of people in these industries.

    Now there are some people in this directory who own more than one company – hence they have more than one entry. It was a bit strange to see their photos repeated – so I added some more fields to the pod to indicate whether this is an additional entry, or if this is the main entry. Also a relationship field which can select the other profiles from the same name, so on the detail page you can see which other companies this person has. Then I modified the pod template, so it will only ignore the entries with have a “yes” in the field “additional entry”. This works fine, it now lists only the main company, so every profile picture is only there once and you can find the other companies by clicking on the profile of the main entry.

    However I have now a problem with the filtering…. when I filter for industries, the additional entries will obviously not show. They would however show if I used the previous template, which ignores the “additional entry” field.

    Is there any clever way I could change the template, whenever somebody clicks on the “Select” button to filter for an industry? So, if you first clicked on the page with the alphabetical listing, you see the template without the duplicates – and as soon as you filter, you see the other template which shows all entries regardless whether they are main entries or duplicates.

    Hope I have explained well enough…

    Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @netschmiede24

    You could use a filter for this, however, I’m not sure what you use for filtering. Do you use our built-in filter options or something else?

    In any case, depending on what you use to create your view, there are filters available.
    For example, if you use a shortcode then pods_shortcode allows you to modify the params send with the shortcode. You could change the template base on the GET parameter of a filter.
    https://github.com/pods-framework/pods/blob/main/includes/general.php#L1282

    Cheers, Jory

    Thread Starter netschmiede24

    (@netschmiede24)

    I don’t think I understand where I can change the template base…

    On the page in question I used the built-in filter option in a pods item list block. But I just rebuilt it with a shortcode – however I do not understand what to put in the shortcode to make the template change once the filter gets applied?

    The shortcode as I have it now is:
    [pods name=”podname” template=”namelist-without-doubles” limit=”-1″ orderby=”lastname” filters=”industry”][/pods]

    I would want that as soon as the filter is clicked the part template=”namelist-without-doubles” changes to “template=”namelist”

    On another note – I noticed that if I don’t set a “limit=”-1″ parameter, then it would only list 12 items. Why is this?

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @netschmiede24

    On another note – I noticed that if I don’t set a “limit=”-1″ parameter, then it would only list 12 items. Why is this?

    This is a default limitation.

    however I do not understand what to put in the shortcode to make the template change once the filter gets applied?

    Do cannot do this in a shortcode. You’ll have to get comfortable with PHP and create a filter to change the shortcode params you’ve passed based on the current selected filter (get variable).
    Such a feature will require PHP knowledge.

    Quick example:

    add_filter( 'pods_shortcode', 'my_pods_shortcode_filter' );
    // Your function to filter the shortcode params.
    function my_pods_shortcode_filter( $params ) {
        // Check if a filter is active.
        if ( ! empty( $_GET[ 'YOUR_FILTER_NAME' ] ) ) {
            // Change the template name.
            $params['template'] = 'NEW TEMPLATE NAME';
        }
        // Return the modified arguments.
        return $params;
    }

    Cheers, Jory

    Thread Starter netschmiede24

    (@netschmiede24)

    OK – yes, I know, my PHP knowledge is rather basic… but I love to learn, and the best way to learn is by solving a concrete problem. (I am very good at frontend design though…)

    So – I added this filter (I have a plugin “Snippets”, where I can add additional code). I altered the filtername, so my code is now as follows:

    add_filter( 'pods_shortcode', 'my_pods_shortcode_filter' );
    // Your function to filter the shortcode params.
    function my_pods_shortcode_filter( $params ) {
        // Check if a filter is active.
        if ( ! empty( $_GET[ 'my_pods_shortcode_filter' ] ) ) {
            // Change the template name.
            $params['template'] = 'namelist';
        }
        // Return the modified arguments.
        return $params;
    }

    Now do I have to add this filter to the shortcode? Like this?
    [pods name=”podname” template=”namelist-without-doubles” limit=”-1″ orderby=”lastname” filters=”industry, my_pods_shortcode_filter”][/pods]

    I tried it – but when I use the filter, I still get the same template. I must be doing something wrong…?

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @netschmiede24

    Nope, my_pods_shortcode_filter is the function name, not the actual filter.
    I believe that would be filter_industry if I’m not mistaken.

    So you can remove my_pods_shortcode_filter from the shortcode and change the GET check in that function to filter_industry.

    Cheers, Jory

    Thread Starter netschmiede24

    (@netschmiede24)

    Ahhhh… yes, it works exactly as I wanted it to! Thank you so much! (And I tried “industry” in the GET before – but didn’t know I had to add “filter_” before that…. ).

    Your help is much appreciated! Thank you!

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @netschmiede24

    Good to hear it worked as you intended and you’re welcome! The best way to say thanks is to leave a 5 star review at https://ww.wp.xz.cn/plugins/pods/ and (if you’re feeling especially generous) become a Friend of Pods at https://friends.pods.io/

    Cheers, Jory

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Switching template when filtering’ is closed to new replies.