1. 程式人生 > 實用技巧 >wordpress個人常用標籤呼叫

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>