Forums

Stuck on passing a custom fuction to a theme (4 posts)

  1. David Law
    Member
    Posted 1 year ago #

    Working on a theme admin menu using http://planetozh.com/blog/my-projects/wordpress-theme-toolkit-admin-menu/ and have a bunch of functions that are used to customise a theme.

    Have added over 40 options, but stuck on this one.

    Code from header.php file:

    <?php wp_list_pages('sort_column=menu_order&title_li=&depth=1&exclude='); ?>

    Have created a function within the admin menu that stores the exclude numbers for excluding pages from the navigation menu. If I wanted pages 1, 7 and 10 excluded I'd add 1,7,10 after the exclude= in the header.php file.

    The function I've created (pageexclude) allows a list of numbers like 1,7,10 to be stored in a database allowing easier excluding of pages (no need to edit the template files). The concept is working fine, for example using

    <?php echo pageexclude(); ?>

    Within the header.php (not within other PHP code though) file results in 1,7,10 being printed.

    but adding this or similar code like:

    <?php wp_list_pages('sort_column=menu_order&title_li=&depth=1&exclude=pageexclude()'); ?>

    Doesn't work. I've used this sort of format through out the theme with no problems, though this is first time I'm trying to use it this way. It would appear the pageexclude function isn't parsed when put within the built in WordPress function.

    This is beyond my PHP skills, any thoughts on a solution?

    Thanks

    David

  2. alchymyth
    The Sweeper
    Posted 1 year ago #

    it might work if you transfer the returned string of your function into a variable, and use the variable in wp_list_pages:

    <?php $pageexclude = pageexclude();
    wp_list_pages('sort_column=menu_order&title_li=&depth=1&exclude=' . $pageexclude); ?>

    (untested)

  3. Mark / t31os
    Moderator
    Posted 1 year ago #

    The problem lies here.. in how you're calling your function.

    <?php wp_list_pages('sort_column=menu_order&title_li=&depth=1&exclude=pageexclude()'); ?>

    You're executing a function inside a string (won't work).

    Concatenate the string so you get the return value from your function on the end of string, like so..

    <?php wp_list_pages('sort_column=menu_order&title_li=&depth=1&exclude=' . pageexclude()); ?>

    Else your function call is treated as a literal value.

    Hope that helps... :)

  4. David Law
    Member
    Posted 1 year ago #

    ROFLOL, turns out I'd already got it working, but have found an interesting feature of WordPress that if you exclude all top parent level pages (which I had in my test) you get the second level child pages shown!

    Would that be a WordPress bug?

    Anyway, both of the above two solutions worked, I'd tried the first one with the test of blocking all top level pages, but didn't realise the links had changed from the top level to the second level.

    Thanks

    David

Topic Closed

This topic has been closed to new replies.

About this Topic