How to Display a Specific Page in WordPress

Home » Snippets » How to Display a Specific Page in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you looking to display a specific page on your WordPress website? Perhaps you have created a landing page or a special promotional page that you want to direct your visitors to. In this article, we will guide you through the process of easily displaying a specific page using WordPress. Whether you want to showcase a page with important information or promote a new product, we’ve got you covered. Stay tuned to learn the step-by-step instructions to effectively display a specific page on your WordPress site.

					function wpturbo_display_specific_page() {
    $args = array(
        'post_type' => 'page',
        'post_status' => 'publish',
        'p' => 123 // Replace 123 with the ID of the page you want to display
    );
    $query = new WP_Query($args);
    while ($query->have_posts()) {
        $query->the_post();
        the_title();
        the_content();
    }
    wp_reset_postdata();
}
add_action('wp', 'wpturbo_display_specific_page');
				

The code snippet provided is a function called wpturbo_display_specific_page(). Its purpose is to display a specific page on a WordPress website.

To achieve this, the code uses the WP_Query class, which is a powerful tool in WordPress for retrieving posts from the database.

Before diving into the code, it’s important to note that you need to replace 123 in the $args array with the actual ID of the page you want to display. This ID can be found in the WordPress dashboard by going to the Pages section and hovering over the page link. The ID will appear in the URL at the bottom of most browsers.

The $args array is used to define the parameters for the query that will retrieve the specific page. In this case, we are setting the post_type to 'page' and the post_status to 'publish'. This ensures that only published pages are retrieved.

After setting up the $args array, we create a new instance of the WP_Query class by passing it the $args array as an argument. This will perform the query and retrieve the page we want to display.

The code then enters a while loop using the have_posts() method of the $query object. This loop will continue as long as there are pages in the query result.

Inside the loop, we call the the_post() method to set up the current post, allowing us to use functions like the_title() and the_content() to display the title and content of the page, respectively. You can modify this part of the code to display any other information about the page, such as the author or the featured image, by using the appropriate WordPress functions.

After the while loop, we call wp_reset_postdata(). This function is used to reset the global $post variable of WordPress, ensuring that other functions or plugins down the line do not get affected by the previous WP_Query.

Finally, we hook the wpturbo_display_specific_page function into the wp action using add_action(). This ensures that the function is executed when the WordPress framework is ready to render the page.

By adding this code to your WordPress theme’s functions.php file, you will be able to display a specific page on your website.

Register an account to save your snippets or go Pro to get more features.