Forums

[resolved] PHP Variables in Template Tags? (4 posts)

  1. Jakob Smith
    Member
    Posted 1 year ago #

    Trying to build a page menu with wp_page_menu();

    I have made a database call to the wp_posts table in the WordPress database, asking for the IDs of pages matching certain conditions. I have put the the IDs into a commaseparated list in a PHP variable and now wants to use wp_page_menu(); like this:

    wp_page_menu('include=$IDvariable');

    But that returns nothing.

    Is it not possible to use PHP variables inside WP Template Tags?

    Does anyone have any idea how I get those IDs dynamically into wp_page_menu(); ?

    Or other suggestions on how to dynamically build a menu of pages matching certain conditions regarding a specific value in a specific field in the wp_posts database table?

    Regards,
    Jakob Smith

  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Try wp_page_menu('include=' . $IDvariable); and ensure that your variable is a simple string - not an array.

  3. Mark / t31os
    Moderator
    Posted 1 year ago #

    The problem with your code is that you can't use variables inside single quotes, so the value is treated literally.

    Esmi's suggested code above will fix that by concatenating the string, alternatively you can also use double quotes so the variable is interpreted.

    Eg.

    wp_page_menu("include=$IDvariable");
  4. Jakob Smith
    Member
    Posted 1 year ago #

    The variable is indeed a simple string made with implode(); from an array.

    Both solutions work.

    Thanks! Really appreciate it.

Topic Closed

This topic has been closed to new replies.

About this Topic