0

I have a simple page structure like Page One, Page Two, Page Three.

Each page has tag of 'content'.

In the Wordpress loop how could I create an array for each page with the page title as the array name.

My code is like this.

    <?php
        $mt_test_args = array(
            'post_type' => 'page',
            'tag' => 'content',
            'order' => 'ASC'
        );

        $mt_test_loop = new WP_Query($mt_test_args);

        if($mt_test_loop->have_posts()):
            while($mt_test_loop->have_posts()):
                $mt_test_loop->the_post();

                //Trying to create array here from page title.

            $arr_name = get_the_title();

                $arr_name = str_replace(' ','_',$arr_name);

                $arr_name = array();

    ?>  

    <option><?php the_title(); ?></option>

    <?php endwhile; endif; ?>

    <?php wp_reset_postdata(); ?>

This is demo code, the actual set up is a lot more complex

0

I think this is what you're looking for:

$$arr_name = array();

If $arr_name = 'post_title', then the above would mean:

$post_title = array();
  • The page titles have spaces that I need to replace with underscores then use that name to name the array – Simon Cooper Jul 8 '13 at 18:47
  • you have that part... $arr_name = str_replace(' ','_',$arr_name); then replace the next line with what i gave you: $$arr_name = array(); and you'll have a PHP variable with the name of $what_ever_your_post_was_called. Is that what you want? Or do you want an array with all the names? Like, array('one_post_title', 'some_other_title', 'third_post_title')??? – Michael Lewis Jul 8 '13 at 18:55
  • Thanks Mike, I want the first thing you posted. So what does the double $$ do? – Simon Cooper Jul 8 '13 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.