WordPress中比较常用的函数列表

746次阅读
没有评论

Wordpress中比较常用的函数列表
如果您是 WordPress 主题开发人员或 WordPress 网站管理员,您可能不得不经常访问Wordpress开发文档,您会感觉有一个比较常用的函数列表很有必要。在这篇文章中,我提供了一份比较经常使用的WordPress实用函数列表,希望可以为您提供有用的参考

WordPress 基本核心函数

在下面的列表中,这些函数自动打印并显示一些内容:

<?php bloginfo('name'); ?>           Display website title.

<?php bloginfo('description');?>      Display the site's tagline (site description).

<?php bloginfo('admin_email');?>      Display the admin email address.

<?php bloginfo('url');?>              Display the website address (URL).

<?php bloginfo('wpurl');?>            Display the WordPress site address (URL).

<?php bloginfo('stylesheet_url');?>   Display the URL of the current theme's style.css file.

<?php bloginfo('template_directory'); ?>   Display the URL of the current theme's directory.

<?php bloginfo('atom_url'); ?>       Display the Atom feed URL of the site.

<?php bloginfo('rss_url'); ?>        Display the RSS feed URL of the site.

<?php bloginfo('rss2_url'); ?>       Display the RSS 2.0 feed URL of the site.

<?php bloginfo('rdf_url'); ?>        Display the RDF feed URL of the site.

<?php bloginfo('charset'); ?>        Display the character set used in your website.

<?php bloginfo('language'); ?>       Display the active language used on your website.

<?php bloginfo('text_direction');?>   Display the text direction used on your website.

<?php bloginfo('version'); ?>        Display the WordPress version of your website.

<?php wp_title(); ?>                Display the title of the current page.

<?php get_search_form() ?>          Display the search form on the site.

<?php the_search_query() ?>         Display the search query text after performing a search on the site.

<?php wp_tag_cloud(); ?>            Display a list of all tags used on the website.

<?php echo tag_description(id); ?>   Display the description of a specific tag - Replace 'id' with the tag ID.

<?php single_tag_title(); ?>        Display the name of the tag when viewing a specific tag page.

<?php single_post_title(); ?>       Display the title of the post when viewing a specific post page.

<?php single_cat_title(); ?>        Display the name of the category when viewing a specific category page.

<?php the_permalink(); ?>           Display the unique permalink of the current post.

<?php get_calendar(); ?>            Display a calendar on the website.

WordPress 列表和下拉菜单相关函数

<?php wp_dropdown_categories(); ?>     Display categories in a dropdown menu.
<?php wp_dropdown_pages(); ?>          Display pages in a dropdown menu.
<?php wp_dropdown_users(); ?>          Display users in a dropdown menu.
<?php wp_get_archives(); ?>            Display a list of website archives.
<?php wp_list_authors(); ?>            Display a list of website authors.
<?php wp_list_bookmarks(); ?>          Display a list of website bookmarks.
****************************
Note: By default, the link manager option is disabled in WordPress, and to enable it, you need to add the following code to your theme's functions.php file:

add_filter('pre_option_link_manager_enabled', '__return_true');
****************************
<?php wp_list_categories(); ?>         Display a list of website categories.
<?php wp_list_comments(); ?>           Display a list of website comments.
<?php wp_list_pages(); ?>              Display a list of website pages.
<?php wp_page_menu(); ?>               Display a list of website pages in a menu format

WordPress登陆和注销相关函数

在下面的部分中,提供了用于具有不同角色的各种用户的登录和注销功能的函数列表:

<?php wp_login_form(); ?>               Display the login form on the website.

<?php echo wp_login_url(); ?>           Display the login page URL.

<?php echo wp_logout_url(); ?>          Display the logout URL.

<?php echo wp_lostpassword_url(); ?>    Display the lost password page URL.

<?php echo wp_registration_url(); ?>    Display the user registration page URL.

<?php wp_loginout(); ?>                Display the login/logout link automatically on the website.

**********************************************

Conditional Function for Two Login States: Guest and Logged-in User

<?php
if ( is_user_logged_in() ) {
    // Actions to perform if the user is logged in.
} else {
    // Actions to perform if the user is a guest.
}
?>

WordPress中与内容相关的函数

在此列表中,我们具有与向用户显示和呈现页面内容相关的功能:

<?php posts_nav_link(); ?>              Display navigation links to go to the previous and next pages.

<?php previous_post_link(); ?>         Display a link to the previous post.

<?php next_post_link(); ?>             Display a link to the next post.

<?php the_category(); ?>              Display the list of categories related to the current post.

<?php the_permalink(); ?>            Display the unique permalink link of the current post.

<?php the_title(); ?>                 Display the title of the current post.

<?php the_content(); ?>              Display the content of the current post.

<?php the_excerpt(); ?>              Display a brief excerpt of the current post.

<?php the_time(); ?>                  Display the publish time of the current post.

<?php the_date('y/m/d'); ?>           Display the publish date of the current post.

<?php the_ID(); ?>                    Display the ID of the current post.

<?php the_author_posts(); ?>         Display the number of posts by the author of the current post.

<?php the_author(); ?>               Display the name of the author of the current post.

<?php the_shortlink(); ?>            Display the shortlink to the current post.

<?php the_tags(); ?>                  Display the tags of the current post.

<?php comments_number(); ?>         Display the number of comments given to the current post.

<?php comments_template(); ?>       Display the comments section - must be inside the single.php file.

<?php the_title_attribute(); ?>     Display the value of the title attribute for the current post.

<?php 
 if ( have_posts() ) {
  while ( have_posts() ) {
   the_post(); ?>
                        Location of the post-related functions
<?php  } // end while
 } // end if
?>

WordPress中缩略图函数

在下面的列表中,您将找到与在 WordPress 中显示每个帖子、页面等的特色图像相关的代码:
首先,将以下代码添加到您的functions.php 文件中:

echo add_theme_support( 'post-thumbnails' );
 <?php the_post_thumbnail('thumbnail'); ?>: Display the thumbnail image associated with each post or page - default size: 150x150 pixels.

    <?php the_post_thumbnail('medium'); ?>: Display the medium-sized thumbnail image associated with each post or page - default size: 300x300 pixels.

    <?php the_post_thumbnail('large'); ?>: Display the large-sized thumbnail image associated with each post or page - default size: 640x640 pixels.

    <?php the_post_thumbnail('full'); ?>: Display the full-sized thumbnail image associated with each post or page.

另外,上述四个函数用于以标准尺寸显示图像。要裁剪上传的图像并将其显示在您的网站上,请按照下列步骤操作:
1、将以下代码添加到您的functions.php 文件中

if (function_exists('add_image_size')):
add_image_size('your-name', width, height, true);
endif;

2、放置以下代码以显示缩略图:

<?php the_post_thumbnail('your-name'); ?>

WordPress中与菜单和导航相关的函数

要显示网站菜单,请使用以下代码:

<?php wp_nav_menu( array( 'theme_location' => 'nezafat-Nav', 'menu_class' => 'navbar' ) ); ?>

注意:此功能默认处于禁用状态。要启用它,您需要将以下函数添加到您的functions.php文件中:

function register_my_menu() {
  register_nav_menu('nezafat-Nav',__( 'nezafat-Nav' ));
}
add_action( 'init', 'register_my_menu' );

WordPress模板相关函数

这些功能对于构建自定义 WordPress 主题以及管理网站的布局和结构至关重要。

    <?php get_header(); ?> - This function includes the header template.
    <?php get_footer(); ?> - This function includes the footer template.
    <?php get_sidebar(); ?> - This function includes the sidebar template.
    <?php get_template_part('nezafat'); ?> - This function includes a specific template part named "nezafat".
正文完
 

公众号