0

this my code

  function form_creation(){ ?>
<select name="page_id" id="page_id">
 <?php
 global $post;
 $args = array('cat'=>19);
 $posts = get_posts($args);
 foreach( $posts as $post ) : setup_postdata($post); ?>
                <option value="<? echo $post->ID; ?>"><?php the_title(); ?></option>
 </select>
<?php
            $my_postid = $post->ID;
            $content_post = get_post( $my_postid);
            $content = $content_post->post_content;
            $content = apply_filters('the_content', $content);
            $content = str_replace(']]>', ']]&gt;', $content);
            echo $content;

                         ?>
<?php } add_shortcode('r', 'form_creation');

I want to display post content when I choose its title from the dropdown menu

but actually, it always displays the first one if I click on any title

I don't know why

can you help me, please?

1 Answer 1

0

To fix this issue, move the closing curly brace } for the foreach loop before the add_shortcode function, like this:

function form_creation(){ ?>
<select name="page_id" id="page_id">
 <?php
 global $post;
 $args = array('cat'=>19);
 $posts = get_posts($args);
 foreach( $posts as $post ) : setup_postdata($post); ?>
                <option value="<? echo $post->ID; ?>"><?php the_title(); ?></option>
 <?php endforeach; ?> <!-- Move this here -->
 </select>
<?php
            $my_postid = $post->ID;
            $content_post = get_post( $my_postid);
            $content = $content_post->post_content;
            $content = apply_filters('the_content', $content);
            $content = str_replace(']]>', ']]&gt;', $content);
            echo $content;

                         ?>
<?php } add_shortcode('r', 'form_creation');

This should ensure that each dropdown menu option corresponds to its own post content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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