Get WordPress Page Permalink from Title – Easy Trick!

Creating a Portfolio Page Drop-Down in WordPress Admin Panel

If you’re a WordPress developer, you know how important it is to provide users with an intuitive and user-friendly interface. One way to do this is by allowing users to select their portfolio page from a drop-down in the admin panel. This not only enhances usability but also helps with SEO. However, implementing this feature can be a bit tricky.

In this article, we’ll show you how to create a drop-down menu that allows users to select their portfolio page, which is based on a page template. We’ll also show you how to get the permalink of any page based on the page title only.

Storing WordPress Pages in an Array

To create a drop-down menu that allows users to select their portfolio page, we need to first store all the WordPress pages in an array. This can be done using the following code:

$pages = get_pages();

$page_options = array();

foreach ($pages as $page) {

$page_options[$page->ID] = $page->post_title;

}

This code retrieves all the pages in WordPress and stores them in an array called $pages. We then loop through each page and add its ID and title to another array called $page_options.

Creating the Drop-Down Menu

Now that we have all the pages stored in an array, we can create the drop-down menu using the following code:

wp_dropdown_pages(array(

‘name’ => ‘portfolio_page’,

‘selected’ => get_option(‘portfolio_page’),

‘show_option_none’ => __(‘Select a page’),

‘option_none_value’ => ‘0’,

‘echo’ => 1,

‘id’ => null

));

This code uses the wp_dropdown_pages function to create a drop-down menu. The ‘name’ parameter specifies the name of the input field, which will be used to save the selected page ID. The ‘selected’ parameter specifies the default selected page, which is retrieved from the WordPress options table using the get_option function. The ‘show_option_none’ parameter specifies the text to display when no page is selected. The ‘option_none_value’ parameter specifies the value to use when no page is selected. The ‘echo’ parameter specifies whether to display the drop-down menu or return it as a string. Finally, the ‘id’ parameter specifies the ID of the drop-down menu.

Getting the Permalink of a Page Based on the Page Title

When a user selects a page from the drop-down menu, we need to get its permalink so that it can be displayed in the breadcrumbs of the single-portfolio posts. However, when a user selects a page, the output for that option becomes the page title, not the URL, Slug or ID.

To get the permalink of a page based on its title, we can use the following function:

function get_page_permalink_from_name($page_name) {

global $post;

global $wpdb;

$pageid_name = $wpdb->get_var(“SELECT ID FROM $wpdb->posts WHERE post_title = ‘” . $page_name . “‘ LIMIT 0, 1”);

return get_permalink($pageid_name);

}

This function takes a page title as its parameter and uses it to retrieve the page ID from the WordPress database. It then returns the permalink of that page using the get_permalink function.

To use this function, simply call it with the desired page title as its parameter:

echo get_page_permalink_from_name(‘YOUR PAGE NAME’);

Conclusion

In this article, we showed you how to create a drop-down menu that allows users to select their portfolio page in WordPress. We also showed you how to get the permalink of any page based on its title only. By implementing these features, you can enhance usability and improve SEO for your WordPress site.

Stay in Touch

spot_img

Related Articles