Converting HTML to WordPress involves using various WordPress functions to ensure that your static HTML elements become dynamic and manageable through the WordPress admin panel. Here’s a detailed list of basic WordPress functions and their purposes:
This can be the second step after setting up wordpress project. Before Understanding this functions, you should know how to setup wordpress projects. We already published a blog about how to setup wordpress project. You can follow this steps to setup wordpress project.
1. Header and Footer Functions
- get_header(): Includes the header.php template file.
- Purpose: This function is used to include the site’s header section, which typically contains meta tags, links to stylesheets, and the site’s navigation menu.
<?php get_header(); ?>
- get_footer(): Includes the footer.php template file.
- Purpose: This function is used to include the site’s footer section, which usually contains scripts, copyright information, and additional navigation links.
<?php get_footer(); ?>
2. Template Tags
- get_template_part( $slug, $name ): Loads a template part into a template.
- Purpose: It is used to include modular template files, like including a specific part of a page layout (e.g., content.php for post content).
<?php get_template_part( 'content', 'page' ); ?>
- get_sidebar(): Includes the sidebar.php template file.
- Purpose: This function is used to include the site’s sidebar section, where widgets, links, and other information can be displayed.
<?php get_sidebar(); ?>
3. Loop Functions
- have_posts(): Checks if there are posts to display.
- Purpose: This function is used within the loop to check if there are any posts to display.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- Loop content goes here -->
<?php endwhile; endif; ?>
- the_post(): Sets up the current post data.
- Purpose: This function is used within the loop to set up the current post data so that template tags can retrieve post information.
<?php while ( have_posts() ) : the_post(); ?>
<!-- Loop content goes here -->
<?php endwhile; ?>
4. Post and Page Functions
- the_title(): Displays the post title.
- Purpose: It is used to display the title of the current post.
<h1><?php the_title(); ?></h1>
- the_content(): Displays the post content.
- Purpose: It is used to display the main content of the post.
<div><?php the_content(); ?></div>
- the_excerpt(): Displays the post excerpt.
- Purpose: It is used to display a summary or excerpt of the post content.
<p><?php the_excerpt(); ?></p>
- the_permalink(): Retrieves the permalink for the current post.
- Purpose: It is used to get the URL of the current post.
<a href="<?php the_permalink(); ?>">Read more</a>
- the_post_thumbnail(): Displays the post’s featured image.
- Purpose: It is used to display the post’s featured image if one is set.
<?php if ( has_post_thumbnail() ) : ?>
<div><?php the_post_thumbnail(); ?></div>
<?php endif; ?>
5. Metadata Functions
- the_author(): Displays the post author’s name.
- Purpose: It is used to display the name of the author of the post.
<p>By <?php the_author(); ?></p>
- the_date(): Displays the post date.
- Purpose: It is used to display the date when the post was published.
<p>Published on <?php the_date(); ?></p>
- the_category(): Displays the post categories.
- Purpose: It is used to display the categories assigned to the post.
<p>Filed under: <?php the_category( ', ' ); ?></p>
- the_tags(): Displays the post tags.
- Purpose: It is used to display the tags assigned to the post.
<p>Tags: <?php the_tags( '', ', ', '' ); ?></p>
6. Navigation Functions
- wp_nav_menu(): Displays a navigation menu.
- Purpose: It is used to display a custom navigation menu created through the WordPress admin panel.
<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>
- posts_nav_link(): Displays links to next and previous pages of posts.
- Purpose: It is used to navigate through paginated post listings.
<?php posts_nav_link(); ?>
7. Dynamic Sidebar and Widgets
- dynamic_sidebar( $index ): Displays the sidebar with the given index.
- Purpose: It is used to display widgets added to the sidebar through the WordPress admin panel.
<?php if ( is_active_sidebar( 'primary-sidebar' ) ) : ?>
<div id="sidebar">
<?php dynamic_sidebar( 'primary-sidebar' ); ?>
</div>
<?php endif; ?>
8. Custom Fields and Meta Data
- get_post_meta( $post_id, $key, $single ): Retrieves the value of a custom field.
- Purpose: It is used to get the value of a custom field associated with a post.
<?php $value = get_post_meta( get_the_ID(), 'custom_field_key', true ); ?>
<?php if ( $value ) : ?>
<p><?php echo $value; ?></p>
<?php endif; ?>
9. Enqueueing Scripts and Styles
- wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ): Properly includes JavaScript files.
- Purpose: It is used to enqueue JavaScript files in the theme.
function my_theme_scripts() {
wp_enqueue_script( 'custom-script', get_template_directory_uri() . '/js/custom-script.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'my_theme_scripts' );
- wp_enqueue_style( $handle, $src, $deps, $ver, $media ): Properly includes CSS files.
- Purpose: It is used to enqueue CSS files in the theme.
function my_theme_styles() {
wp_enqueue_style( 'custom-style', get_template_directory_uri() . '/css/custom-style.css', array(), '1.0.0', 'all' );
}
add_action( 'wp_enqueue_scripts', 'my_theme_styles' );
10. General Utility Functions
- bloginfo( $show ): Retrieves information about the blog.
- Purpose: It is used to display various information about the site, such as the site’s name, URL, and description.
<p><?php bloginfo( 'name' ); ?></p>
<p><?php bloginfo( 'description' ); ?></p>
- is_front_page(): Checks if the current page is the front page.
- Purpose: It is used to conditionally display content or load templates on the front page.
<?php if ( is_front_page() ) : ?>
<h1>Welcome to the Front Page</h1>
<?php endif; ?>
- is_home(): Checks if the current page is the blog page.
- Purpose: It is used to conditionally display content or load templates on the blog page.
<?php if ( is_home() ) : ?>
<h1>Welcome to the Blog</h1>
<?php endif; ?>
Using these functions allows you to make your HTML content dynamic and integrated with WordPress features, making it easier to manage and update content through the WordPress admin interface.
WordPress is very easy to setup, install and to build a site and it is 100% FREE available. As per the latest statistics in 2021, 35% of the internet is powered by WordPress. It’s easily available by one download click. If you are still not getting output, you can contact team of experienced website developers Astranic for detailed help.