wordpress常用调用标签

news/2024/10/21 7:56:24/

调用头部:<?php get_header(); ?>

调用底部:<?php get_footer(); ?>

调用自定义模板文件:<?php get_template_part('list-head'); ?>

路径:<?php bloginfo('template_url'); ?>

缩略图:<?php the_post_thumbnail_url( 'full' ); ?>

调用标题:<?php the_title(); ?>

发布时间:<?php the_time('Y-m-d');?>

文章内容:<?php the_content();?>

面包屑:<?php if (function_exists('cmp_breadcrumbs')) cmp_breadcrumbs(); ?>

分类页面:

调用分类图片:<?php if (function_exists('z_taxonomy_image_url')) echo z_taxonomy_image_url(); ?>

分类目录名称:

<?php $thiscat = get_category($cat);echo $thiscat->name; ?>

菜单调用           

在需要显示菜单的位置调用此代码: 

<?php wp_nav_menu(array('theme_location' => 'header_menu', 'container' => false,'items_wrap' => '<ul class="nav_ul">%3$s</ul>', 'fallback_cb' => 'Bing_menu_null_fallback')); ?>

在functions.php中添加菜单功能       

<?php// 添加菜单功能
function sanyi_setup()
{register_nav_menus(['header_menu' => __('顶部导航'),'footer_menu'  => __('底部导航')]);
}
add_action('after_setup_theme', 'sanyi_setup');?>

首页循环调用指定栏目 

<?php
$args = array('cat' => 4,   // 分类ID  'posts_per_page' => 3, // 显示篇数 
);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post();
?><div class="swiper-slide"><!-- 文章链接 --><a href="<?php the_permalink(); ?>"><div class="over"><!-- 调用文章缩略图 --><img src="<?php the_post_thumbnail_url('full'); ?>" alt="" class="w100"></div><div class="content"><div class="title"><!-- 文章标题 --><?php the_title(); ?></div><div class="txt"><!-- 文章摘要 --><?php if (has_excerpt()) {echo $description = get_the_excerpt(); //文章编辑中的摘要} else {echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 170, "……"); //文章编辑中若无摘要,自定截取文章内容字数做为摘要} ?></div><div class="time"><!-- 调用发布日期 --><?php echo date('M');the_time('d,Y'); ?></div></div></a></div>
<?php endwhile;
endif;
wp_reset_query(); ?>

 首页调用指定分类的二级分类

<?php
$args = array('include' => '1',
);
$categories = get_categories($args);
foreach ($categories as $category) { ?><?php$argszi = array('child_of' => $category->term_id,'parent' => $category->term_id,'hide_empty' => '0','orderby' => 'ID','order' => 'ASC',);$categorieszi = get_categories($argszi);foreach ($categorieszi as $categoryzi) {?><div class="col-md-2"><a href="<?php echo get_category_link($categoryzi->term_id); ?>"><div class="box"><div class="content"><div class="en_name2"><?php echo $categoryzi->name; ?></div></div></div></a></div><?php }
}
?>

 category分类循环调用文章

<?php while (have_posts()) : the_post(); ?><li class="list"><a href="<?php the_permalink(); ?>"><div class="over"><img src="<?php the_post_thumbnail_url('full'); ?>" alt="<?php the_title(); ?>" class="w100"></div><div class="content"><div class="en_name"><?php the_title(); ?></div><!-- 所属分类 --><div class="txt"><?php$category = get_the_category();echo $category[0]->cat_name;?></div></div></a></li>
<?php endwhile; ?><?php while (have_posts()) : the_post(); ?><div class="col-sm-6 col-12"><a href="<?php the_permalink(); ?>"><div class="bord"><div class="over"><img src="<?php the_post_thumbnail_url('full'); ?>" alt="" class="w100"></div><div class="content"><div class="title"><?php the_title(); ?></div><div class="txt"><?php if (has_excerpt()) {echo $description = get_the_excerpt(); //文章编辑中的摘要} else {echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 170, "……"); //文章编辑中若无摘要,自定截取文章内容字数做为摘要} ?></div><div class="time"><?php echo date('M');the_time('d,Y'); ?><!-- July 28,2022 --></div></div></div></a></div>
<?php endwhile; ?>

single文章页面调用循环 

<div class="list_news"><div class="container"><div class="row"><?php while (have_posts()) : the_post(); ?><div class="col-sm-6 col-12"><a href="<?php the_permalink(); ?>"><div class="bord"><div class="over"><img src="<?php the_post_thumbnail_url('full'); ?>" alt="" class="w100"></div><div class="content"><div class="title"><?php the_title(); ?></div><div class="txt"><?php if (has_excerpt()) {echo $description = get_the_excerpt(); //文章编辑中的摘要} else {echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 170, "……"); //文章编辑中若无摘要,自定截取文章内容字数做为摘要} ?></div><div class="time"><?php echo date('M');the_time('d,Y'); ?><!-- July 28,2022 --></div></div></div></a></div><?php endwhile; ?></div><div class="pageinfo"><?php par_pagenavi(9); ?></div></div>
</div>

functions.php 

<?php// 添加菜单功能
function sanyi_setup()
{register_nav_menus(['header_menu' => __('顶部导航'),'footer_menu'  => __('底部导航')]);
}
add_action('after_setup_theme', 'sanyi_setup');// 激活缩略图
add_theme_support('post-thumbnails');// 分类选择模板
class Select_Category_Template
{public function __construct(){add_filter('category_template', array($this, 'get_custom_category_template'));add_action('edit_category_form_fields', array($this, 'category_template_meta_box'));add_action('category_add_form_fields', array(&$this, 'category_template_meta_box'));add_action('created_category', array(&$this, 'save_category_template'));add_action('edited_category', array($this, 'save_category_template'));do_action('Custom_Category_Template_constructor', $this);}// 添加表单到分类编辑页面public function category_template_meta_box($tag){$t_id = $tag->term_id;$cat_meta = get_option("category_templates");$template = isset($cat_meta[$t_id]) ? $cat_meta[$t_id] : false;
?><tr class="form-field"><th scope="row" valign="top"><label for="cat_Image_url"><?php _e('Category Template'); ?></label></th><td><select name="cat_template" id="cat_template"><option value='default'><?php _e('Default Template'); ?></option><?php page_template_dropdown($template); ?></select><br /><span class="description"><?php _e('为此分类选择一个模板'); ?></span></td></tr><?phpdo_action('Custom_Category_Template_ADD_FIELDS', $tag);}// 保存表单public function save_category_template($term_id){if (isset($_POST['cat_template'])) {$cat_meta = get_option("category_templates");$cat_meta[$term_id] = $_POST['cat_template'];update_option("category_templates", $cat_meta);do_action('Custom_Category_Template_SAVE_FIELDS', $term_id);}}// 处理选择的分类模板function get_custom_category_template($category_template){$cat_ID = absint(get_query_var('cat'));$cat_meta = get_option('category_templates');if (isset($cat_meta[$cat_ID]) && $cat_meta[$cat_ID] != 'default') {$temp = locate_template($cat_meta[$cat_ID]);if (!empty($temp))return apply_filters("Custom_Category_Template_found", $temp);}return $category_template;}
}$cat_template = new Select_Category_Template();/*分页*/
function par_pagenavi($range = 9)
{global $paged, $wp_query;if (!$max_page) {$max_page = $wp_query->max_num_pages;}if ($max_page > 1) {if (!$paged) {$paged = 1;}if ($paged != 1) {echo "<a href='" . get_pagenum_link(1) . "' class='extend' title='First Page'><i class='iconfont icon-left-1'></i></a>";}// previous_posts_link('Pre');if ($max_page > $range) {if ($paged < $range) {for ($i = 1; $i <= ($range + 1); $i++) {echo "<a href='" . get_pagenum_link($i) . "'";if ($i == $paged) echo " class='current'";echo ">$i</a>";}} elseif ($paged >= ($max_page - ceil(($range / 2)))) {for ($i = $max_page - $range; $i <= $max_page; $i++) {echo "<a href='" . get_pagenum_link($i) . "'";if ($i == $paged) echo " class='current'";echo ">$i</a>";}} elseif ($paged >= $range && $paged < ($max_page - ceil(($range / 2)))) {for ($i = ($paged - ceil($range / 2)); $i <= ($paged + ceil(($range / 2))); $i++) {echo "<a href='" . get_pagenum_link($i) . "'";if ($i == $paged) echo " class='current'";echo ">$i</a>";}}} else {for ($i = 1; $i <= $max_page; $i++) {echo "<a href='" . get_pagenum_link($i) . "'";if ($i == $paged) echo " class='current'";echo ">$i</a>";}}// next_posts_link('Next');if ($paged != $max_page) {echo "<a href='" . get_pagenum_link($max_page) . "' class='extend' title='Last Page'><i class='iconfont icon-right-1-copy'></i></a>";}}
}// 后台菜单:设置-通用内容设置
function options_admin_menu()
{add_submenu_page('options-general.php', '通用内容设置', '通用内容设置', 'administrator', 'custom-setting', 'customSetting');
}
// 通过add_action来自动调用options_admin_menu函数
add_action('admin_menu', 'options_admin_menu');
include_once('setContent.php');/* 分类摘要支持html
/* -------------------------------- */
remove_filter('pre_term_description', 'wp_filter_kses');
remove_filter('pre_link_description', 'wp_filter_kses');
remove_filter('pre_link_notes', 'wp_filter_kses');
remove_filter('term_description', 'wp_kses_data');
function get_excerpt($excerpt)
{$content = get_the_content();$content = strip_tags($content, '<h2><ul><ol><li><a><strong><h3><blockquote><strong>');$content = mb_strimwidth($content, 0, 400, '...');return wpautop($content);
}
add_filter('the_excerpt', 'get_excerpt');/* 获取当前分类ID */
function get_current_category_id()
{$current_category = single_cat_title('', false); //获得当前分类目录名称return get_cat_ID($current_category); //获得当前分类目录ID
}/* 获取当前页面跟分类ID */
function get_category_root_id($cat)
{$this_category = get_category($cat); // 取得当前分类 while ($this_category->category_parent) // 若当前分类有上级分类时,循环 {$this_category = get_category($this_category->category_parent); // 将当前分类设为上级分类(往上爬) }return $this_category->term_id; // 返回根分类的id号 
}/**后台分类列表添加ID显示**/// Prepend the new column to the columns array
function ssid_column($cols)
{$cols['ssid'] = 'ID';return $cols;
}
// Echo the ID for the new column
function ssid_value($column_name, $id)
{if ($column_name == 'ssid')echo $id;
}
function ssid_return_value($value, $column_name, $id)
{if ($column_name == 'ssid')$value = $id;return $value;
}
// Output CSS for width of new column
function ssid_css()
{?><style type="text/css">#ssid {width: 50px;}/* Simply Show IDs */</style>
<?php
}
// Actions/Filters for various tables and the css output
function ssid_add()
{add_action('admin_head', 'ssid_css');add_filter('manage_posts_columns', 'ssid_column');add_action('manage_posts_custom_column', 'ssid_value', 10, 2);add_filter('manage_pages_columns', 'ssid_column');add_action('manage_pages_custom_column', 'ssid_value', 10, 2);add_filter('manage_media_columns', 'ssid_column');add_action('manage_media_custom_column', 'ssid_value', 10, 2);add_filter('manage_link-manager_columns', 'ssid_column');add_action('manage_link_custom_column', 'ssid_value', 10, 2);add_action('manage_edit-link-categories_columns', 'ssid_column');add_filter('manage_link_categories_custom_column', 'ssid_return_value', 10, 3);foreach (get_taxonomies() as $taxonomy) {add_action("manage_edit-${taxonomy}_columns", 'ssid_column');add_filter("manage_${taxonomy}_custom_column", 'ssid_return_value', 10, 3);}add_action('manage_users_columns', 'ssid_column');add_filter('manage_users_custom_column', 'ssid_return_value', 10, 3);add_action('manage_edit-comments_columns', 'ssid_column');add_action('manage_comments_custom_column', 'ssid_value', 10, 2);
}
add_action('admin_init', 'ssid_add');/*** WordPress 添加面包屑导航 * https://www.wpdaxue.com/wordpress-add-a-breadcrumb.html*/
function cmp_breadcrumbs() {$delimiter = '<span> > </span>'; // 分隔符$before = '<li class="fl"><a href="">'; // 在当前链接前插入$after = '</a></li>'; // 在当前链接后插入if ( !is_home() && !is_front_page() || is_paged() ) {global $post;$homeLink = home_url();// echo '<li class="fl"> <a itemprop="breadcrumb" href="' . $homeLink . '">' . __( 'HOME' , 'cmp' ) . '</a></li> ' . $delimiter . ' ';if ( is_category() ) { // 分类 存档global $wp_query;$cat_obj = $wp_query->get_queried_object();$thisCat = $cat_obj->term_id;$thisCat = get_category($thisCat);$parentCat = get_category($thisCat->parent);if ($thisCat->parent != 0){$cat_code = get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' ');echo $cat_code = str_replace ('<a','<li class="fl"> <a itemprop="breadcrumb"', $cat_code );}echo $before . '' . single_cat_title('', false) . '' . $after;} elseif ( is_day() ) { // 天 存档echo '<li class="fl"> <a itemprop="breadcrumb" href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> </li>' . $delimiter . ' ';echo '<li class="fl"> <a itemprop="breadcrumb"  href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a> </li>' . $delimiter . ' ';echo $before . get_the_time('d') . $after;} elseif ( is_month() ) { // 月 存档echo '<li class="fl"> <a itemprop="breadcrumb" href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> </li>' . $delimiter . ' ';echo $before . get_the_time('F') . $after;} elseif ( is_year() ) { // 年 存档echo $before . get_the_time('Y') . $after;} elseif ( is_single() && !is_attachment() ) { // 文章if ( get_post_type() != 'post' ) { // 自定义文章类型$post_type = get_post_type_object(get_post_type());$slug = $post_type->rewrite;echo '<li class="fl"> <a itemprop="breadcrumb" href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a> </li>' . $delimiter . ' ';echo $before . get_the_title() . $after;} else { // 文章 post$cat = get_the_category(); $cat = $cat[0];$cat_code = get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');echo $cat_code = str_replace ('<a','<li class="fl"> <a itemprop="breadcrumb"', $cat_code );echo $before . get_the_title() . $after;}} elseif ( !is_single() && !is_page() && get_post_type() != 'post' ) {$post_type = get_post_type_object(get_post_type());echo $before . $post_type->labels->singular_name . $after;} elseif ( is_attachment() ) { // 附件$parent = get_post($post->post_parent);$cat = get_the_category($parent->ID); $cat = $cat[0];echo '<li class="fl"> <a itemprop="breadcrumb" href="' . get_permalink($parent) . '">' . $parent->post_title . '</a> </li>' . $delimiter . ' ';echo $before . get_the_title() . $after;} elseif ( is_page() && !$post->post_parent ) { // 页面echo $before . get_the_title() . $after;} elseif ( is_page() && $post->post_parent ) { // 父级页面$parent_id  = $post->post_parent;$breadcrumbs = array();while ($parent_id) {$page = get_page($parent_id);$breadcrumbs[] = '<li class="fl"> <a itemprop="breadcrumb" href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a> </li>';$parent_id  = $page->post_parent;}$breadcrumbs = array_reverse($breadcrumbs);foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';echo $before . get_the_title() . $after;} elseif ( is_search() ) { // 搜索结果echo $before ;printf( __( 'Search Results for: %s', 'cmp' ),  get_search_query() );echo  $after;} elseif ( is_tag() ) { //标签 存档echo $before ;printf( __( 'Tag Archives: %s', 'cmp' ), single_tag_title( '', false ) );echo  $after;} elseif ( is_author() ) { // 作者存档global $author;$userdata = get_userdata($author);echo $before ;printf( __( 'Author Archives: %s', 'cmp' ),  $userdata->display_name );echo  $after;} elseif ( is_404() ) { // 404 页面echo $before;_e( 'Not Found', 'cmp' );echo  $after;}if ( get_query_var('paged') ) { // 分页if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() )echo sprintf( __( '( Page %s )', 'cmp' ), get_query_var('paged') );}}
}?>

 

 

 

 


http://www.ppmy.cn/news/342655.html

相关文章

【html/css】前端入门案例:画盒子

一、环境准备 【vscode】前端环境配置 二、画盒子 输入英文的感叹号&#xff01;并回车&#xff0c;然后不断按Tab直到文本编辑光标在body标签内。输入.red.green.red并回车。html代码部分完毕。在head标签最后写入style标签。写入css样式代码 .red {w100h100bc:red; } .gr…

页面滚动table头部悬浮

页面滚动头部悬浮&#xff0c;一般都会首相想到监听滚动条离顶部高度然后给需要悬浮的部分position设置成fixed&#xff0c;在一般布局里面是可以的&#xff0c;不过在table布局里面&#xff0c;如果要fixed悬浮thead里面的th会发现宽度样式都乱了。只能想其他办法了&#xff0…

小程序云开发教程五: 图片上传及发表文字的实现

微信给了我们存储空间以及图片上传的功能&#xff0c;我们怎么可以轻易放过呢&#xff1f; 先看看界面&#xff1a; 二话不说&#xff0c; 先实现界面&#xff1a; <!--pages/pulish/pulish.wxml--> <view classflexDownC><view classflexDownC w100> <…

js的form.reder()函数渲染table表格中的下拉框数据

目录 问题描述 form.reder()函数 简单介绍一下form.reder() layui官网对于渲染页面的介绍 页面渲染 问题描述 在我的项目中遇到一个问题在table中添加输入框但是下拉输入框中没有我们的数据 如图: 看代码 : function add(data) {data.forEach((item, index) > {let …

【工控老马】欧姆龙PLC Socket发送Fins/TCP命令解析

欧姆龙Socket tool发送Fins/TCP命令 实验设备&#xff1a;CJ2M-CPU33(PLC) 实验目的&#xff1a;发送Fins/TCP命令读写CJ2M-CPU33的数据 实验步骤&#xff1a; 1、 系统概述&#xff0c;硬件搭建和接线&#xff1a; 图1.1 2、软件设置 ①首先在PLC里通过CX-Programmer设置CJ…

动量梯度下降法(gradient descent with momentum)

简介 动量梯度下降法是对梯度下降法的改良版本&#xff0c;通常来说优化效果好于梯度下降法。对梯度下降法不熟悉的可以参考梯度下降法&#xff0c;理解梯度下降法是理解动量梯度下降法的前提&#xff0c;除此之外要搞懂动量梯度下降法需要知道原始方法在实际应用中的不足之处…

VUE+Layui 输入框、下拉选择框、复选框、开关、文本域 数据回显

一、说明二、效果三、代码四、如果失效了 一、说明 用了Layui的界面&#xff0c;加上vue的指令&#xff1a;v-model&#xff0c;v-bind&#xff0c;v-for 等 1、注意&#xff1a;开关按钮如果是关闭状态&#xff1a;status字段是不会在表单提交字段中的&#xff0c;打开了就是s…

【每天一个java设计模式(六)】 - 适配器模式

当某种业务功能已经实现&#xff0c;但它们与当前系统的接口不兼容&#xff0c;如果重新开发成本又很高&#xff0c;这时用适配器模式能很好地解决这些问题。 适配器模式是作为两个不兼容的接口之间的桥梁&#xff0c;属于结构型模式&#xff0c;它结合了两个独立接口的功能。…