0

What I want to do I need to build a "month-bar" for tribe-events Wordpress plugin. I copied "bar.php" to my theme folder and managed to show a list of the next 12 months there, on top of the search bar in list view.

What I did so far I added links to the month bar, and managed to add the month as a query to the current domain (&tribe-bar-date=$title) which gets me to the correct page. So far so good.

My problem Every further klick on my month-bar links, ads more querys to the end of the current domain (&tribe-bar-date=2018-12&tribe-bar-date=2019-07). What I need is a variable which contains the query (&tribe-bar-date=$title), and gets a kind of refresh on every page.

My Code so far:

<div><!--horizontal bar test-->
<?php
// find out the domain:
$domain = $_SERVER['HTTP_HOST'];
// find out the path to the current file:
$path = $_SERVER['SCRIPT_NAME'];
// find out the QueryString:
$queryString = $_SERVER['QUERY_STRING'];
// put it all together - different ways: 
//$url = "https://" . $domain . $path . "?" . $queryString;
// An alternative way is to use REQUEST_URI instead of both
// SCRIPT_NAME and QUERY_STRING, if you don't need them seperate:
$url = "http://" . $domain . $_SERVER['REQUEST_URI'];
//echo $url;
//END find out the domain

//Venue ID - just in case. 
    $post_id = get_the_ID(); 
    $v1 = '8995';
    $v2 = '1444';
    $venue = tribe_get_venue(); 
//END Venue ID - just in case. 

// Month bar starts here
echo "<p class=’text-center no-underline’ style=’margin-top: 20px;’>Month: ";
    for ( $i = 0; $i <= 12 ; $i++ )
            {
            $next = date('Y-m',strtotime("+$i months"));
            $m = date('M',strtotime("+$i months"));
            $title = date('Y-m',strtotime("+$i months"));
            //echo "<a href='$url&tribe_bar_date=$title'>$m</a>";            
            echo "<a href='$url&tribe-bar-date=$title'>$m</a>";  

            }
        echo "</p>";
        ?>

    <!-- end horizontal bar--></div>
s.Panse
  • 27
  • 6
  • You would need to remove the parameters before setting them again - `$_SERVER['REQUEST_URI']` gets current parameters as well. I guess you could use somthing like `$path=strtok($_SERVER["REQUEST_URI"],'?');` which will give you the path until the first `? ` if you have a `?` – Stender Nov 02 '18 at 09:25
  • So : `$url = "http://" . $domain . strtok($_SERVER["REQUEST_URI"],'&');` maybe? – Stender Nov 02 '18 at 09:26
  • Or.. you could do like this : https://stackoverflow.com/a/6969655/4244684 – Stender Nov 02 '18 at 09:29
  • or this `parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);` many possabilities – Stender Nov 02 '18 at 09:30
  • Hello @stender, thanks a lot! Ill give it a try. One problem I see coming here: part of the current URL is a "venue-parameter" which has to be preserved. I could put the venues in categories and change the url structure from parameters to folders (/category/venue/), but then I need to change the venue import process. – s.Panse Nov 02 '18 at 10:59
  • If you want to keep a specific parameter - you can do like this - I have created a playground for you – Stender Nov 02 '18 at 11:09
  • https://www.tehplayground.com/xfbsd86Nxm744hpa – Stender Nov 02 '18 at 11:09
  • further explained here : https://www.tehplayground.com/bnIDq3RjYatHStRY – Stender Nov 02 '18 at 11:21
  • I have to thank you again - you are crazy man :) In a good way. – s.Panse Nov 02 '18 at 13:26
  • Ill try that and give you feedback within the next days, oaky? – s.Panse Nov 02 '18 at 13:26
  • No worries mate, I going AFK for the weekend anyways – Stender Nov 02 '18 at 13:27
  • Hello @Stender! I played around a little with your code and after some try and error I finally and found an new approach. add_query_arg -> a WP function. 'add_query_arg( array( 'key1' => 'value1', 'key2' => 'value2', ), 'http://example.com' );' https://developer.wordpress.org/reference/functions/add_query_arg/ Would you help me finalizing my code when I post it here? All the best! – s.Panse Nov 08 '18 at 15:05
  • ' echo "

    Monat: "; for ( $i = 0; $i <= 12 ; $i++ ) { $next = date('Y-m',strtotime("+$i months")); $m = date('M',strtotime("+$i months")); $title = date('Y-m',strtotime("+$i months")); //echo "$m"; $query = $url; get_query_var('tribe-bar-date'); $new_query = add_query_arg( array( 'tribe-bar-date' => $title, ), $query ); echo " $m "; } echo "

    ";'
    – s.Panse Nov 14 '18 at 10:50
  • Hello @Stender! I posted my code above. Do you have an idea, if I need to use "esc url" here? I could not find a way of doing so. – s.Panse Nov 14 '18 at 10:53

0 Answers0