企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
课程:[https://ke.qq.com/course/448367](https://ke.qq.com/course/448367) 【theme基本显示】 可以跳过自己搭建wordpress后台核心函数的调用 直接在twentyseventeen选取functions.php和inc文件夹,实现官方模板使用的wordpress核心函数 添加wordpress主题根目录函数 ``` <?php bloginfo('template_directory'); ?> // 对应的文件夹为: <?php bloginfo('template_directory'); ?>/css/ <?php bloginfo('template_directory'); ?>/js/ <?php bloginfo('template_directory'); ?>/images/ ``` 首页路径 ``` <?php echo get_option('home'); ?> ``` 网站标题 ``` <?php bloginfo('name'); ?> ``` 网站描述 ``` <?php bloginfo('description'); ?> ``` 【get_template_part();的使用】 ``` <?php get_template_part( 'nav' ); ?> // Navigation bar (nav.php) <?php get_template_part( 'nav', '2' ); ?> // Navigation bar #2 (nav-2.php) <?php get_template_part( 'nav', 'single' ); ?> // Navigation bar to use in single pages (nav-single.php) ``` 【#分拆为header.php和footer.php】 index.php提取的代码: ``` <?php get_header(); ?> <?php get_footer(); ?> ``` 在wordpress主题中,通常会有`<?php wp_head(); ?>`和`<?php wp_footer(); ?>`这两个是wordpress插件向主题头部和尾部加载内容使用的函数。这样方便在主题指定位置中动态添加代码,来实现插件的动态配置。 ``` <?php wp_head(); ?> <?php wp_footer(); ?> ``` 参考: [https://www.menglei.net/2721/](https://www.menglei.net/2721/) 【#文章循环输出】 ``` <?php $categores = new WP_Query('post_type=post&cat=&posts_per_page=12'); if($categores->have_posts()): while($categores->have_posts()) : $categores->the_post(); ?> <!-- code... --> <?php endwhile; else: endif; ?> ``` ``` <?php the_title(); ?> <?php the_time('Y年n月j日, G:i:s'); ?> <?php the_permalink(); ?> <?php the_post_thumbnail('product-thumbnails'); ?> <?php the_category(); ?> ``` funtion.php ``` add_image_size( 'product-thumbnails', 500, 202, true ); add_image_size( 'product-thumbnails-search', 500, 185, true ); ``` 【#文章内容】 ``` <?php if(have_posts()): while(have_posts()): the_post(); the_content(); endwhile; else: // something can say this is nothing endif; ?> <?php previous_post_link('上一页: %link'); ?><br/> <?php next_post_link('下一页: %link'); ?> ``` 【#菜单】 ``` <li> <?php $args = array( //指定显示的导航名,如果没有设置,则显示第一个 'theme_location' => 'top', //导航菜单ul标签的class名 'menu_class' => '', //导航菜单ul标签的id名 'menu_id' => "nav" ); wp_nav_menu($args); ?> </li> ``` 【#分类】 集合: ``` <?php the_category(); ?> ``` 可加入分隔符: ``` <?php $categories = get_the_category(); $separator = ", "; $output = ''; if($categories) { foreach ($categories as $key => $category) { $output .= '<a href="' . get_category_link($category->term_id) . ' ">' . $category->cat_name . '</a>'. $separator; } echo trim($output, $separator); } ?> ``` 【#留言功能】 ``` <?php if(comments_open() || get_comments_number()): comments_template(); endif; ?> ``` 总分类 ``` <?php wp_list_cats('sort_column=name&optioncount=0&hierarchical=0'); ?> ``` 最新内容 ``` <?php wp_get_archives('type=postbypost&limit=10'); ?> ``` 【4.页面、文章样式选择显示】 设置页面模板,需要在指定页面加入如下代码: ``` <?php /* template name:模板名字 */ ?> ``` 【指定循环输出数量文章】 ``` <?php $current_page = max(1, get_query_var('paged')); //当前第几页 //查询参数 $args = array_filter(array( // 'orderby' => 'title', // 'order' => 'ASC', 'post_type' => 'post', 'ignore_sticky_posts' => 1 , 'posts_per_page' => 18, 'paged' => $current_page, //当前页 )); //开始查询 $query = new WP_Query($args); $total_pages = $query->max_num_pages; //总共多少页 while ($query->have_posts()): $query->the_post(); ?> <!-- post code --> <?php endwhile; ?> <li> <!-- 输出分页 --> <?php echo paginate_links( array( 'prev_text' => __( '上一页', 'YChinaTours' ), 'next_text' => __( '下一页', 'YChinaTours' ), 'screen_reader_text' => null, 'total' => $total_pages, //总页数 'current' => $current_page, //当前页数 ) ); ?> </li> ``` 【分类和其他类型的文章归类】 ``` <?php if(have_posts()) : ?> <?php if( is_category()){ single_cat_title(); }elseif(is_tag()){ single_tag_title(); }elseif(is_author()){ the_post(); echo 'Author Archives:'. get_the_author(); rewind_posts(); }elseif(is_day()){ echo 'Daily Archives:' .get_the_date(); }elseif(is_month()){ echo 'Daily Archives:' .get_the_date('F Y'); }elseif(is_year()){ echo 'Daily Archives:' .get_the_date('Y'); }else{ echo 'Archives'; } ?> <?php while(have_posts()) : the_post(); ?> <!-- post code --> <?php endwhile; ?> <?php echo paginate_links(); ?> <?php else : ?> <center><h2>对不起,没有找到相关内容,请搜索其他关键词</h2></center> <?php endif; ?> ``` 【搜索页面】 搜索: ``` <?php the_search_query(); ?> ``` ``` <?php if(have_posts()) : ?> <center> <h2>搜索结果:<?php the_search_query(); ?></h2> </center> <?php while(have_posts()) : the_post(); ?> <!-- post code --> <?php endwhile; ?> <?php echo paginate_links(); ?> <?php else : ?> <h2><strong>没有找到</strong>你想要找的东西 :-(</h2> <h3>请重新搜索<strong>相关的</strong>关键词 !</h3> <?php endif; ?> ```