Wordpress get posts as a function

Hi I'm trying to create a function to show a list of posts in my admin menu of wordpress but because I'm calling the function a few times on the same page I needed to add some extra statements but it breaks the output and I don't know why

Here is my current code that outputs the basics:

function test() {
    // The Query
    query_posts( array ('posts_per_page' => -1 ) );
    // The Loop
     while ( have_posts() ) : the_post(); 
     ?>
    <option value="<?php the_permalink() ?>"><?php the_title(); ?></option>
    <?php endwhile;
    // Reset Query
    wp_reset_query();
    }

Output code:

<select>
<?php test(); ?>
</select>

Returned Output:

<select>
<option value="http://website/posttitle1">POST TITLE 1</option>
<option value="http://website/posttitle2">POST TITLE 2</option>
<option value="http://website/posttitle3">POST TITLE 3</option>
</select>

But I need to add a select option on like this:

function test($select) {
    // The Query
    query_posts( array ('posts_per_page' => -1 ) );
    // The Loop
     while ( have_posts() ) : the_post(); 
     if ($select == the_permalink()) { $selected = " selected"; }
     ?>
    <option value="<?php the_permalink() ?>"><?php the_title(); ?></option><?php echo "\n"; ?>
    <?php endwhile;
    // Reset Query
    wp_reset_query();
    }

Output code:

<select>
<?php test("..GET Permalink from Database.."); ?>
</select>

But then this is my output:

<select>
http://website/posttitle1<option value="http://website/posttitle1">POST TITLE 1</option>
http://website/posttitle2<option value="http://website/posttitle2">POST TITLE 2</option>
http://website/posttitle3<option value="http://website/posttitle2">POST TITLE 3</option>
</select>

I dont understand?

Solutions

the_permalink() print the value and get_permalink() returns the value.

Try this.

Change the following line

if($select == the_permalink()) { $selected = " selected"; }

to

if ($select == get_permalink()) { $selected = " selected"; }

And this line to

<option value="<?php the_permalink() ?>"><?php the_title(); ?></option><?php echo "\n"; ?>

this

<option value="<?php echo get_permalink() ?>"><?php echo get_the_title(); ?></option><?php echo "\n"; ?>
Tags: PHP / Wordpress

Similar questions

Exclude Featured Posts in Wordpress 'Recent Posts' Function
Can I use the "exclude" array in the wp_get_recent_posts function to exclude Featured Posts? I have a plugin called NS Featured Posts which pulls featured posts through a key in the wp query i.e.: could I somehow use this to target and exclude NS Featured Posts in the wp_get_recent_posts call e.g.: Thanks for any insight.
How do I correctly get all posts within the last year using the query_posts function?
Specifically, I am making a custom rss feed. I have modeled it after the rss2 feed that is in the wp-includes folder. I can successfully retrieve all posts using the standard query_posts('posts_per_page=-1'). Here is how I am attempting to get just the ones posted in the past year: Yes this returns only posts from 2011 forward. Can someone help me ...
If meta key exists in get posts function otherwise create it
The following code fetches posts with the meta key home telephone numbers. I want to add another meta to look for. The problem is that this meta may not exist on all posts. I want to get posts that either don't have the meta key or don't have a value for it. This code will only get posts that have the key without a value. I also want posts that do ...
how could I get the pagination as I want to when query posts using get_posts function
I am using WP3.5, and I have a post_list.php which will show all the posts order by post date DESC. Everything is ok except pagination. I have 15 posts, for test purpose, I want to every page show 2 posts, others will be show next page like this. Some parts of post_list.php Yes, as you see, every page show 2 posts as I set, but I cannot get the pag...
Get the last month with posts using a recursive function…
There is probably another way but I would like to understand why this code returns null... I only have posts in october and here's the recursive function:
How to get posts with list of ID from SQL function result?
I'm newbie in PHP, and I need your advice. Thank you for help! I'm working with PHP and I need to get posts with image attachment only. I've wrote sql function: Now I want to use results of this function for working with get_posts("p= results of function"); How to connect result with calling this function? I can't understand how to pass a parameter...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.