Home  >  Article  >  CMS Tutorial  >  How to set up article template in wordpress

How to set up article template in wordpress

下次还敢
下次还敢Original
2024-04-16 01:39:17493browse

How to set up a post template in WordPress? Create a new file called "template-name.php" where "template-name" is the name of your desired template. Add the code "Template Name: My Custom Template", where "My Custom Template" is your desired template name. Customize the template content and add the code you want to display. Edit the article and assign the template to the article in the Properties section.

How to set up article template in wordpress

How to set up a post template in WordPress

Step 1: Create a post template file

  1. Create a new file in your theme folder.
  2. Name the file "template-name.php", where "template-name" is the name of your desired template. For example: "template-blog.php".

Step 2: Add Template Code

  1. In the file you created, add the following code:
<code class="php"><?php
/*
Template Name: My Custom Template
*/
?></code>

Replace "My Custom Template" with your desired template name.

Step 3: Customize the template

  1. In <?php get_header(); ?> and&lt ;?php get_footer(); ?> Add the code you want to display in the template.
  2. Customize content, sidebars, headers and footers to create the unique layout you need.

Step 4: Assign the template to the post

  1. Edit the post for which you want to use the template.
  2. In the Properties section of the sidebar, select the template you created.

Example

To create a template called "Blog Template", follow these steps:

  1. Create a A file named "template-blog.php".
  2. Add the following code:
<code class="php"><?php
/*
Template Name: Blog Template
*/
?></code>
  1. In <?php get_header(); ?> and <?php get_footer (); ?> Add the following code between them to display the post list:
<code class="php"><?php
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
    'post_type' => 'post',
    'paged' => $paged,
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
    get_template_part( 'template-parts/content', get_post_format() );
endwhile;
wp_reset_postdata();
?></code>

Now, you have created a custom template for WordPress posts.

The above is the detailed content of How to set up article template in wordpress. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn