WordPress.org

Ready to get started?Download WordPress

Forums

Using Custom Fields to control Menu pages (1 post)

  1. tytycadet
    Member
    Posted 3 years ago #

    Hi There,

    I've been working on this and I'm quite stumped.

    I want to be able to use the wp_list_pages function, however rather than using the exclude/include commands to control which pages show, I wanted to use a custom field so that the control can be done via the administrator rather than by the developer.

    I've been working with several variations of this code but none seem to work. I think i'm close but my mastery of PHP syntax is limited.

    I found the following code and added this to my function.php code.

    <?php function get_custom_field($key, $echo = FALSE) {
    global $post;
    $custom_field = get_post_meta($post->ID, $key, true);
    if ($echo == FALSE) return $custom_field;
    echo $custom_field;
    }
    ?>

    i've added the custom field "Menu_field" and added this to pages I wanted to control, giving it the value of 1 or 2 depending on whether i wanted to include or exclude.

    I was then advised to use the code below
    get_custom_field('Menu_level', true);

    This returns the value for the Menu Level,

    I though therefor that I could then use the Menu level in the exclude part of the wp_pages_list function.

    <?php wp_list_pages('include=&menu_level=1,&title_li='); ?>

    This didn't work. I then found something different on another forum and I tried this

    wp_list_pages('include=&meta_key=Menu_level&meta_value=1,&title_li=');

    This did not work either.

    I should clarify that the code I have used here and the advise given has been taken from forums looking to use custom fields to alter which posts are shown. Therefore I thought I could use similar logic for the menu.

    The long and the short of it. I just want to be able to alter what shows in the menu using a custom field or some other input that is shown in the administrator panel.

    Advice welcome.

    I found a post in the forum from four years ago !!! Which didn't not have a resolution to this same problem and I have hunted all over google, so I hope I have earned the right to post this and ask for help.

Topic Closed

This topic has been closed to new replies.

About this Topic