WordPress.org

Ready to get started?Download WordPress

Forums

Dynamic Drop Down Menu (9 posts)

  1. swabby2000
    Member
    Posted 2 months ago #

    Hello,

    I am using the below referenced code in order to do a "Drop Down Menu" on my website. I use a plugin called Event Espresso. Right now what it does is returns all my events by title, but I want to change it so that it references just the name of the city the event is in. Can someone give me a hint on where I would want to change what is displayed to the end user on the </select> portion that makes the drop down box?

    <h2>Search Classes by Location</h2>
    [insert_php]#wp_dropdown_pages("child_of=510");[/insert_php]

    <select id="page_id" name="page_id">
    [insert_php]
    $pages = get_pages("child_of=510");
    #$pages = get_pages();
    echo("<option value=''>Please Select...</option>");
    foreach ( $pages as $page ) {
    $title = $page->post_title;
    if (strpos($title, "Black Belt Training") != False) {
    $option = '<option value="' . $page->ID . '">';

    #$loc = strpos($title,"-");
    #$title = substr($title,0,$loc);
    #$title = trim($title);
    #$title = str_replace(" ", ", ", $title);

    $option .= $title;

    $option .= '</option>';
    echo $option;
    }
    }
    [/insert_php]
    </select>

    <script type="text/javascript">
    var dropdown = document.getElementById("page_id");
    function onCatChange()
    {
    if ( dropdown.options[dropdown.selectedIndex].value > 0 )
    {
    location.href = "[insert_php]echo get_option('home');[/insert_php]/?page_id="+dropdown.options[dropdown.selectedIndex].value;
    }
    }
    dropdown.onchange = onCatChange;
    </script>

  2. bcworkz
    Member
    Posted 2 months ago #

    The line where the title that the user sees is assigned with:
    $option .= $title;

    I don't know where you would get the city from, but let's say it was in $city. You could then just change $title to $city. Have you considered the case when there is more than one event in the same city? How would the user discern which event is which if they both have the same identical city label?

  3. swabby2000
    Member
    Posted 2 months ago #

    bcworkz - Thank you so much for your reply. This is very helpful. Good idea regarding more than one in the same city, we only have one event in each city which makes it simple.

    Question regarding getting the city... could I somehow put a field on my "pages" and just put city in there so that I could use a variable like $city to find them?

    What if the city is available in my plugin, how would I reference that variable? Is it something they have to have available in the plugin?

  4. swabby2000
    Member
    Posted 2 months ago #

    Is there a list of these "$title... etc" variables somewhere?

  5. bcworkz
    Member
    Posted 2 months ago #

    If you plugin does not already have a facility for saving city data, you could add a custom field where it could be saved with the event. There is normally a custom field box on the post edit form, though you may need to enable it in the screen options. Plugins can disable it though, so you may not be able to find it.

    One way or another you can enable the fields with some hacking, but the specifics would depend on how the plugin works, which I know nothing about. Once enabled, the data is stored in postmeta. You can get it out of the table and use it in the drop down with get_post_meta().

    If you can't simply figure this out, your next option is to inquire at the plugin support site where folks are familiar with the plugin. If that fails, some hacking may be in order.

  6. swabby2000
    Member
    Posted 2 months ago #

    bcworkz- Thanks again for your response. I created a custom field called "city" but i cannot get the command to work.

    Am I doing it correctly?

    I changed this line from
    $option .= $title;
    to

    $option .= get_post_meta( get_the_ID(), 'city', true );

  7. bcworkz
    Member
    Posted 2 months ago #

    That would be correct if you were running a standard WP Loop that calls the_post(), but you are not, so get_the_ID() does not work as expected. It's not necessary to run a standard loop, but you need to realize that doing so means some template tags meant for use in the Loop will not work correctly.

    In this particular case you can get the proper ID as $page->ID, so the entire code line should be like this:
    $option .= get_post_meta( $page->ID, 'city', true );

  8. swabby2000
    Member
    Posted 2 months ago #

    One last question. I was able to get my code working correctly however for some reason the last post I have with the city criteria, it keeps showing up in an ongoing loop with that city like 10 times. Can you give me some advice on how to fix?

    <h2>Search Classes by Location</h2>
    [insert_php]#wp_dropdown_pages("child_of=510");[/insert_php]

    <select id="page_id" name="page_id">
    [insert_php]
    $pages = get_pages("child_of=510");
    echo("<option value=''>Please Select...</option>");
    foreach ( $pages as $page ) {
    $title = $page->post_title;
    if (strpos($title, "Black Belt Training") != False) {
    $meta = get_post_meta( $page->ID, 'city', true );
    if ( '' != $meta ) {
    $option = '<option value="' . $page->ID . '">';

    $option .= get_post_meta( $page->ID, 'city', true );

    $option .= '</option>';
    }
    echo $option;
    }
    }
    [/insert_php]
    </select>

    <script type="text/javascript">
    var dropdown = document.getElementById("page_id");
    function onCatChange()
    {
    if ( dropdown.options[dropdown.selectedIndex].value > 0 )
    {
    location.href = "[insert_php]echo get_option('home');[/insert_php]/?page_id="+dropdown.options[dropdown.selectedIndex].value;
    }
    }
    dropdown.onchange = onCatChange;
    </script>

  9. bcworkz
    Member
    Posted 2 months ago #

    I don't see anything in the code here to cause that. It may be there are actually 10 copies of the post, check your page listings in admin. Possibly the get_pages() call is returning archive copies in addition to the published version, but that is unlikely unless some plugin has messed up the function in some way. The only way to check is to deactivate all plugins and see if the behavior changes.

    It could be some quirk of your PHP in post content plugin that I assume you are using. You can test by putting you code temporarily on your theme's page template (outside the loop) and opening a different page than the one with this code in the content. Make a backup copy first so it's easy to restore. For good measure, deactivate the plugin while doing this test.

    Or maybe some javascript is adding additional HTML beyond what PHP is outputting, see if the 10 options are in the HTML source view, this does not reflect anything added by javascript.

    That's all I can think of. Beyond that, all you could do is some detailed debugging by using var_dump() at various places to be sure the variables contain data they are supposed to, and functions are returning what they should. It's OK to do this even with core code (make backups!) if it helps you track down where the error is occurring. All the FTP required to do this can get quite tedious, but it's the only way to track down some problems.

Reply

You must log in to post.

About this Topic

Tags

No tags yet.