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](http://gqxi.cn/wp-content/uploads/2025/05/20250530113731196-image.png)
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](http://gqxi.cn/wp-content/uploads/2025/05/20250530143625305-image.png)
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](http://gqxi.cn/wp-content/uploads/2025/05/20250530143006543-image.png)
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 templatecontent-post.php
: Templates for article typescontent-page.php
: Templates for page typescontent-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.php
The
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.php
Instead of calling acontent()
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
Link to this article:http://gqxi.cn/en/56872The article is copyrighted and must be reproduced with attribution.
No comments