In WordPress, use the related_posts() function to display the article names of other categories in the article. This function has the following parameters: limit: the number of articles to be displayed exclude: the articles to be excluded IDcategory: the category directory ID from which the articles are to be pulled title: the title to be displayed
Display the article names of other categories in WordPress articles
In WordPress, you can display the article names of other categories in the article by using the related_posts
function . The following are the steps:
Open the template file to display related articles, usually single.php
or page.php
.
related_posts
FunctionAdd the following code where related articles need to be displayed:
<code class="php"><?php echo related_posts(); ?></code>
related_posts
The function accepts multiple parameters and can be used to customize how related articles are displayed. The most commonly used parameters include:
limit
: the number of articles to display (default value is 5) exclude
: to exclude The article ID (the default value is the current article ID) category
: The category directory ID to pull the article from title
: Displayed Title (default value is "Related Posts") For example, to display 3 related articles in the same category as the current article, you can use the following code:
<code class="php"><?php echo related_posts(array('limit' => 3, 'category' => get_the_category()[0]->term_id)); ?></code>
Save changes to the template file.
After completing these steps, related articles will appear in Articles. You can further customize the display by adjusting the parameters of the related_posts
function.
The above is the detailed content of How to display the article names of other categories in WordPress related articles. For more information, please follow other related articles on the PHP Chinese website!