WordPress.org

Ready to get started?Download WordPress

Forums

Grab variable and use it elsewhere. (4 posts)

  1. Endlyss
    Member
    Posted 7 months ago #

    Primarily PHP, but I am using it in wordpress, and hopefully can get some kind of help with this.

    Recently I created a multi-check selection area that auto-populated based on pages listed. (below is the code)

    if ( $pages ) {
    $prop = 'post_title';
    $pages = wp_list_pluck( $pages, $prop );
    
    $options[] = array(
    'name'    => __( 'What Pages to Include?' ),
    'class'   => 'inner_section_header',
    'id'      => 'select_pages_to_display',
    'options' => $pages,
    'std'     => '',
    'type'    => 'multicheck' );
    }

    (thanks to mod keesiemeijer)

    But now I wanted to create a container based on the page title from each of those checkboxes (currently set as the label for each option)
    The framework sets up each option using this in 'inc/options-interface.php' (an outside script):

    case "multicheck":
    foreach ($value['options'] as $key => $option) {
    $checked = '';
    $label = $option;
    $option = preg_replace('/[^a-zA-Z0-9._\-]/', '', strtolower($key));
    
    $id = $option_name . '-' . $value['id'] . '-'. $option;
    $name = $option_name . '[' . $value['id'] . '][' . $option .']';
    
    if ( isset($val[$option]) ) {
    $checked = checked($val[$option], 1, false);
    }
    
    $output .= '<input id="' . esc_attr( $id ) . '" class="checkbox of-input" type="checkbox" name="' . esc_attr( $name ) . '" ' . $checked . ' /><label for="' . esc_attr( $id ) . '">' . esc_html( $label ) . '</label>';
    }
    break;

    This produced a check-menu that looked like this
    []Home
    []Services
    []Portfolio
    []Contact

    Which is exactly how I wanted it...it shows all of the pages that I had created.

    Basically How would I get the label (or the post title...or the option...whatever works) from the options menu to a variable so that I could use it multiple times through out the loop.
    For Example, Say we stored it in a new variable called $title. I would then like to take it and place it as such:

    echo '<div id="' . $title . '">;
    $post = get_page_by_title($title);
    $content = apply_filters('the_content', $post->post_content);
    echo $content;
    wp_reset_query();
    echo '</div>';

    This is what I have so far as a function in my functions.php, and I am getting it to display true for each page that is checked in the theme options...but I am having a bit of bad luck getting it to grab the page title for its current item.

    function selected_pages_endlyss(){
    
    	$multicheck = of_get_option('select_pages_to_display');
    	foreach ( $multicheck as $current=>$value){
    			if ( $value == 1 ) {
    				echo 'true';
    			}
    			else {
    			 //silence
    			}
    			unset($current);
    		}
    	}

    Again, any Help would be greatly appreciated. Thank you.

  2. Sandeep
    Member
    Posted 7 months ago #

    You need to store the post ids, in the options table (serialize and store all selected post ids against a key). Retrieve this key and get titles for each of these post ids.

  3. Endlyss
    Member
    Posted 7 months ago #

    I'll give it a shot when I get home. Asked that question right before I left for work.
    If any examples are available as to how I would do that, that would be awesome.
    Thanks for the feedback sandy.

  4. Endlyss
    Member
    Posted 7 months ago #

    Gah. I am still lost.
    I have been trying to accomplish what you mentioned Sandy, but no luck.

Reply

You must log in to post.

About this Topic