WordPress.org

Support

Support » Plugins and Hacks » [Resolved] wp_list_pages excluding a page by title

[Resolved] wp_list_pages excluding a page by title

  • marques_uk
    Member

    @marques_uk

    Hi I can’t seem to find a solution to my problem and I’ve been searching the internet for hours, so I was wondering if anyone in the WP forum can help?

    I’m using this code to display my pages

    <?php $args = array(
      'sort_column' => 'menu_order',
      'title_li' => '',
      'depth' => '1',
      'echo' => 0
    );
    $separator = '<img src="http://localhost/test_theme/images/nav-divide.png"';
    $pattern = '/(<\\/a>).*?(<\\/li>).*?(<li)/is';
    $replace = '</a>' . $separator . '</li><li';
    $subnav = preg_replace($pattern,$replace,wp_list_pages($args));
    echo $subnav; ?>

    This work well, however I have the problem that I want to exclude the holding page from the list.

    I usually use this code to remove a page

    <?php $page1 = get_page_by_title ('Holding'); wp_list_pages('exclude='. $page1->ID . '&title_li='); ?>

    but can’t use this because it doesn’t include the seperators, does anyone know how I can alter the code with the seperators to exclude the page with the title ‘Holding’?

    I have looked at the ‘exclude’ => ”, but would like to exclude by title rather than by number.

    Thanks in advance
    Mark

Viewing 5 replies - 1 through 5 (of 5 total)
  • marques_uk
    Member

    @marques_uk

    Any ideas anybody, I thought it would be fairly simple?

    Thanks

    Michael
    Forum Moderator

    @alchymyth

    marques_uk
    Member

    @marques_uk

    Thanks alchymyth, I can get the page by title no problem but I’m trying to alter this code

    <?php $args = array(
      'sort_column' => 'menu_order',
      'title_li' => '',
      'depth' => '1',
      'echo' => 0
    );
    $separator = '<img src="http://localhost/test_theme/images/nav-divide.png"';
    $pattern = '/(<\\/a>).*?(<\\/li>).*?(<li)/is';
    $replace = '</a>' . $separator . '</li><li';
    $subnav = preg_replace($pattern,$replace,wp_list_pages($args));
    echo $subnav; ?>

    I have found a solution to my problem and I can 'exclude' => '109', to remove the page in question.

    Would be nice to do it by title though. Tried this but no luck 🙁

    <?php $exclude1 = get_page_by_title('Sample Page Title'); ?>
    	'post__not_in' => array($exclude1->ID),

    Oh well….

    Michael
    Forum Moderator

    @alchymyth

    the posted links which should allow you to get the page id from the page title, and then use it in your code;

    please read the links carefully.

    there is even an example in http://codex.wordpress.org/Function_Reference/get_page_by_title matching your problem.

    marques_uk
    Member

    @marques_uk

    Ok managed to do it, I’m sure I’d already tried this but anyways, for anyone else looking to seperate their page lists and exclude a certain page by title, this is the code.

    <?php $exclude1 = get_page_by_title('Holding'); ?>
    <?php $args = array(
      'sort_column' => 'menu_order',
      'title_li' => '',
      'depth' => '1',
      'exclude' => $exclude1->ID,
      'echo' => 0
    );
    $separator = '<img src="http://localhost/test_theme/images/nav-divide.png"';
    $pattern = '/(<\\/a>).*?(<\\/li>).*?(<li)/is';
    $replace = '</a>' . $separator . '</li><li';
    $subnav = preg_replace($pattern,$replace,wp_list_pages($args));
    echo $subnav; ?>

    Thanks for pointing me in the right direction alchymyth, sometimes the answers are right under your nose but you just can’t see them, lol…

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] wp_list_pages excluding a page by title’ is closed to new replies.