WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Exclude Current Page from "Pages" Widget (3 posts)

  1. jtroump
    Member
    Posted 1 year ago #

    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.

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

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    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;
    }
  3. jtroump
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic