WordPress get_template_part: solving the content error problem.

exist WordPress Themesdevelopment process.get_template_part() is a very commonly used function to split and reuse template files. However, many developers often encounter the Call to undefined function content() The errors are usually not WordPress bugs, but rather problems with the structure or naming of the template. This type of error is usually not a WordPress bug, but rather a problem with the template structure or naming convention.

Picture [1]-WordPress get_template_part Usage Explained: to solve the problem of content error reporting

What is get_template_part()?

get_template_part() is WordPress' recommended way of splitting templates, which calls the specified template section without duplicating code. Commonly used for loading content areas,footer,footers,a side-bar (in software)etc.

The most common way to write:

get_template_part( 'template-parts/content', get_post_type() ).

What this code does is: based on the current article type, it dynamically loads the article type such as template-parts/content-post.php maybe template-parts/content-page.php Documentation.

What is the reason for the content() error?

Reporting errors generally looks like this:Fatal error: Uncaught Error: Call to undefined function content()

Most of the time, it's not get_template_part() The problem with yourself is that you are in content.php It's directly in the file But content() This function doesn't exist.

In WordPress thematicMiddle.content.php is a "content template" and does not call the content() function.

What should be the correct structure?

The following structure is recommended for organizationtemplatesDocumentation:

Master template file:index.php maybe archive.php

Picture [2]-WordPress get_template_part Usage Explained: to solve the problem of content error reporting

Content template files: e.g. template-parts/content-post.php

<article id="post-<?php the_ID(); ?>" no numeric noise key 1002>
  <header class="entry-header">
    <h2 class="entry-title"><?php the_title(); ?></h2>
  </header>

  <div class="entry-content">
    <?php the_content(); ?>
  </div>
</article>
Picture [3]-WordPress get_template_part Usage Explained: to solve the problem of content error reporting

The point is:Do not write in these templates content()Because WordPress There is no such function.

Suggestions for naming the template structure

  • content.php: Default content template
  • content-post.php: Templates for article types
  • content-page.php: Templates for page types
  • content-product.php: WooCommerce Product Templates

WordPress will prioritize loading the version with the suffix, and if it doesn't exist, fall back to the content.phpThe

What if I want to pass variables?

get_template_part() Direct value passing is not supported by default. If you want to pass a value, you can use the following new method (WordPress 5.5 and above):

get_template_part( 'template-parts/content', 'custom', array( 'custom_class' => 'highlight' ) );

Then receive it in the template:

<div class="<?php echo esc_attr( $args['custom_class'] ?? '' ); ?>">
  ...
</div>

wrap-up

utilization get_template_part() When you do, remember the following:

  • The name of the file is content-xxx.phpInstead of calling a content() function (math.)
  • Write structures directly in the file instead of defining functions
  • Named according to the template hierarchy, WordPress will automatically match the
  • WordPress After 5.5, you can use $args Parameter passing for more flexibility

This will not only solve the error reporting problem, but also make your theme code clearer and more modular.

Recent Updates


Contact Us
Can't read the tutorial? Contact us for a free answer! Free help for personal, small business sites!
Customer Service
Customer Service
Tel: 020-2206-9892
QQ咨詢:1025174874
(iii) E-mail: info@361sale.com
Working hours: Monday to Friday, 9:30-18:30, holidays off
? Reprint statement
This article was written by Little Lin
THE END
If you like it, support it.
kudos1284 share (joys, benefits, privileges etc) with others
commentaries sofa-buying

Please log in to post a comment

    No comments