Forums

wp_list_pages and adding to the href (6 posts)

  1. mortenfriis.info
    Member
    Posted 1 year ago #

    Hallo,

    In my main menu, I want the links to link to e.g. ?page_id=2#main, so people don't have to see the top again, after clicking the link.

    So I would like to know how to put a #main in thhe end of the href string.

    Thank you for your time...

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Put this in your theme's functions.php:

    function main_in_link($wp_list_pages) {
    $patterns = '/(\<li class="page_item[^>]*><a href=")(.*?)\"(.*?)<\/a\>/';
    $replacements = '\1\2#main"\3</a>';
    return preg_replace($patterns, $replacements, $wp_list_pages);
    }
    add_filter('wp_list_pages', 'main_in_link');

    Now all links produced by wp_list_pages have #main in the url.

  3. alchymyth
    The Sweeper
    Posted 1 year ago #

    a hack:

    instead of
    <?php wp_list_pages('title_li='); ?>

    you could try and use

    <?php $pali = wp_list_pages('title_li=&echo=0'); echo str_replace('" title=','#main" title=',$pali); ?>

    keep all your other paramters.

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    Use alchymyth solution. This way you have #main on the links for this particular wp_list_pages on your main menu.

  5. alchymyth
    The Sweeper
    Posted 1 year ago #

    @keesiemeijer

    thanks for the filter solution as well, as it allows a more global approach, for instance if the pages list is in widgets where the code is not easily accessible.

  6. mortenfriis.info
    Member
    Posted 1 year ago #

    Thanks guys, works perfect!

Topic Closed

This topic has been closed to new replies.

About this Topic