wordpress个人常用标签调用

wordpress常见标签调用,老是容易忘记,又要找半天,干脆搬到网站上。

<?php bloginfo('name');?>网站名称

url

<?php echo home_url();?>获取网站根url
<?php echo get_template_directory_uri();?>资源地址
<?php get_template_part('home-mode/banner');?>调用模板php地址

标题

<?php the_title();?>内容标题
<?php wp_title(''); ?>网页标题-配合seo插件
<?php echo $post->post_title;?>标题
//输出指定id的文章标题
<?php $page_data = get_page( );echo $page_data->post_title;?>

内容

<?php echo $post->post_content;?>文章内容
<?php get_post($post_id)->post_content;?> 获取文章内容
<?php $page_data = get_page( 78 );?> 获取指定id的文章对象
//获取指定id的文章内容
<?php $post_id =1;echo get_post($post_id)->post_content;?>

日期

<?php echo $post->post_date;?>日期
<?php the_time("Y-m-d");?>自定义日期格式的文章
<?php the_time("Y年m月d日 H:i");?>

链接

<?php the_permalink();?>获取文章链接
<?php the_permalink(2);?>获取id为2的文章链接

分类

<?php the_category();?> 获取分类目录
<?php echo get_cat_name(get_query_var('cat'));?>调用当前所属分类的名称
//指定分类别名,获取该别名分类的链接
<?php $cat=get_category_by_slug('news-gs');echo get_category_link($cat->term_id);?>
//输出分类目录id
<?php if(is_single()){$category = get_the_category();$slug = $category[0]->slug;}echo $slug;?>
//调用当前文章所属分类的链接,用于返回列表
<?php $category = get_the_category();if($category[0]){echo get_category_link($category[0]->term_id );}?>

文章附件

<?php $media = get_attached_media( 'audio', 102 );?> //调用文章附件image,audio
//调用设定的$top_id的文章的图片附件
<?php $media = get_attached_media( 'image', $top_id );echo current($media)->guid;?>

文章图片调用详情

作者

<?php echo get_the_author_meta( 'display_name', $post->post_author )?>作者

自定义字段

<?php echo get_post_meta($post->ID, '演示地址', true); ?>//获取自定义字段
//指定长度调用自定义标签的内容
<?php $title = get_post_meta($post->ID, '工程概述', true);$trimmed_title = wp_trim_words( $title, 60);echo $trimmed_title;?>

摘要

<?php the_excerpt();?> //Post/Page 摘要
<?php echo $post->post_excerpt;?>摘要
//指定长度的摘要
<?php $title = $post->post_excerpt;$trimmed_title = wp_trim_words( $title, 60);echo $trimmed_title;?>

tags

//获取当前文章的标签,三个参数对应输出标签之前、标签之间分隔符、标签之后
<?php $tag_list = get_the_tag_list( $before, $sep, $after ); ?>

上一页/下一页

<?php next_post_link(' %link') ?>下一页
<?php previous_post_link('%link') ?>上一页
<?php//指定文章分类,方便调用上一页下一页在同一个分类
$categories = get_the_category();
$categoryIDS = array();
foreach ($categories as $category) {
array_push($categoryIDS, $category->term_id);
}
$categoryIDS = implode(",", $categoryIDS);
?>
<?php if (get_previous_post($categoryIDS)) { previous_post_link('%link','%title',true);} else { echo "已是最后文章";} ?>
<?php if (get_next_post($categoryIDS)) { next_post_link('%link','%title',true);} else { echo "已是最新文章";} ?>

文章调用

//常见的wordpree文章调用方法
<?php if( have_posts() ) : while( have_posts() ) : the_post(); ?>
循环文章调用
<?php endwhile; ?>
<div><?php wp_pagenavi(); ?></div>//分页功能
<?php endif; ?>
//query_posts文章调用方法
<?php query_posts('cat=1&showposts=3');?> //cat是要调用的分类ID,showposts是需要显示的文章数量
<?php while (have_posts()) : the_post(); ?>
循环调用指定数量,指定分类的文章
<?php endwhile; wp_reset_query(); ?>
第二篇开始调用可以加一个参数&offset=1,第三篇开始调用就用&offset=2

调用指定数量文章详情

阅读数量

<?php
//文章阅读数量详情
function getPostViews($postID){//查询getPostViews(get_the_ID());
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 View";
}
return $count.' Views';
}
function setPostViews($postID) {//计数setPostViews(get_the_ID());
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
?>

a标签跳转方式

<ul>
<li><a href="https://www.luojiasan.com">跳转到www.luojiasan.com</a></li>
<li><a href="https://www.luojiasan.com" target="_blank">打开新页面www.luojiasan.com</a></li>
<li><a href="###">不做任何操作</a></li>
<li><a href="#">刷新当前页面,url后面会加个#</a></li>
<li><a href="">刷新当前页面</a></li>
</ul>
原文地址:https://www.cnblogs.com/duolaAmengblog/p/13563347.html