• Resolved jtroump

    (@jtroump)


    Hi all,
    I would like to apologize if this question is already answered somewhere but I was Googling it for many hours and I didn’t find the solution.

    So, here is what I want to do:
    I have created a “Pages” widget which shows all my pages at the sidebar.
    What I want is my current page to be excluded from the widget.

    For example, let’s say I have the widget “Other Services” which lists all pages related to services. This widget is shown in every service’s page.
    So, if I am in page ‘Service 1’, the widget shows a list of links, like

    • Service 1
    • Service 2
    • Service 3

    What I want is the widget to automatically detect that I am currently in page ‘Service 1’ and exclude it from the widget’s list so I can avoid creating different “Other Services” widgets from every service page.

    Is this possible with some way?

    Thank you in advance.

    https://sxetizesthai.gr/services/workplace-stress

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this in your theme’s functions.php:

    add_filter('widget_pages_args', 'exclude_current_page');
    function exclude_current_page($args) {
      if(is_page()){
        global $post;
        $args['exclude'] = ($args['exclude'] == '') ? $post->ID : $args['exclude'] . ',' . $post->ID;
      }
      return $args;
    }

    Thread Starter jtroump

    (@jtroump)

    Yes, worked like a charm!
    Thank you very much.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Exclude Current Page from "Pages" Widget’ is closed to new replies.