0

I'm trying to use a predefined variable (generated from an earlier sweep of the database) in wordpress' standard get_permalink( get_page_by_title) function.

I'm assuming that this is possible? But I can't get it working, it always returns the url of the current page.

For the sake of clarity below I'm just manually assigning the variable.

Code...

<?php $mypagename = "My First Page"; ?>
<a href="<?php echo esc_url( get_permalink( get_page_by_title( $mypagename ) ) ); ?>">LINK</a>

N.B - My First Page (my-first-page) does exist in the database

Any ideas where I'm going wrong or if it's not possible to do it like this do you have any other suggestions?

Many thanks!

1
  • Try var_dump( get_page_by_title( $mypagename ) ). I bet you get NULL. That's why get_permalink is returning the URL for the current page. Are you sure the "My First Page" page exists? Commented Jan 20, 2015 at 17:02

1 Answer 1

0

get_page_by_title() returns a object by default and get_permalink() needs the ID as first parameter. Try:

$mypagename = "My First Page";

$permalink = get_permalink( get_page_by_title( $mypagename )->ID );
1
  • Ahh that will be why it's not working then ;) Thanks! Commented Jan 20, 2015 at 17:29

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.