1. 程式人生 > >wordpress常用程式碼分享

wordpress常用程式碼分享

jiathis.com獲取‘分享程式碼’程式碼
-----------------------------------------------------------
//加分類頁面跳轉程式碼:(子分類也能這樣獲取)
<?php  
    $cat_gg=get_category_by_slug('classic'); 
    $cat_links_gg=get_category_link($cat_gg->term_id);
?> 
    <a href="<?php echo $cat_links_gg; ?>">檢視更多>></a
>
----------------------------------------------------------- 本地圖片地址前面要加:<?php bloginfo('template_directory'); ?>/ ----------------------------------------------------------- 分頁 <div class="container-fluid text-center"> <div class="row"> <div class="col-md-10 col-md-offset-1 col-xs-12 blue_list"
>
<div class="col-md-12 col-xs-12 col-sm-12"> <?php pagination($query_string); ?> </div> </div> </div> </div> //此分頁只對當前分類目錄下的文章起作用 ----------------------------------------------------------- <?php query_posts('category_name=industry'
); //取yewu所有置頂的文章?>
<?php while (have_posts()) : the_post(); ?> <?php if(is_sticky($id)==true) {?> <li><a href="<?php echo get_permalink($id); ?>"><?php the_title(); ?><span><?php the_time("Y-m-d"); ?></span></a></li> <?php }?> <?php endwhile;?> <?php wp_reset_query(); ?> 特別注意:在當前分類下就不要用<?php query_posts('category_name=industry');否則分頁不起作用 如果當前分類下有多個分類的文章列表,這些分類一定要是當前分類的子分類,不然分頁無效 ----------------------------------------------------------- <?php the_post_thumbnail(); ?>//特色圖片 ----------------------------------------------------------- <?php query_posts('showposts=1&category_name=xxzyjs'); //獲取學校專業介紹前1條資料?> ----------------------------------------------------------- //點選跳轉首頁: <script> window.onload = function(){ var obj = document.getElementById('ttt'); obj.onclick=function(){ window.location.href="http://localhost/wordpress/"; } } </script> ----------------------------------------------------------- //普通文章列表頁 <?php while(have_posts()) : the_post(); ?> <li> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> <span style="display:block;float:right; text-align:right;"><?php the_time("Y-m-d"); ?></span> </a> </li> <?php endwhile;?> ----------------------------------------------------------- //文章詳細內容 <?php echo $post->post_content;?> ------------------------------------------------------------ //分類模板外掛 <?php /* * Template Name: XXX分類模版 */ ?> ------------------------------------------------------------ //取文章前多少字 <?php echo wp_trim_words(get_the_content(), 80,'...' ) ?> ----------------------------------------------------------- //特色圖片不響應: //index.css寫樣式,把樣式加到特色圖片 .content-img img{ width:100%; height:auto; display:block; } ----------------------------------------------------------- 獲取文章中第一張圖片或視訊 在function中寫函式 //獲取第一張圖片 function catch_that_image() { global $post, $posts; $first_img = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); $first_img = $matches [1] [0]; if(empty($first_img)){ //Defines a default image $first_img = "http://yourwebsite/default.jpg"; } return $first_img; } 或 function catch_the_image( $id ) { // global $post, $posts; $first_img = ''; // 如果設定了縮圖 $post_thumbnail_id = get_post_thumbnail_id( $id ); if ( $post_thumbnail_id ) { $output = wp_get_attachment_image_src( $post_thumbnail_id, 'large' ); $first_img = $output[0]; } else { // 沒有縮圖,查詢文章中的第一幅圖片 ob_start(); ob_end_clean(); $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); $first_img = $matches [1] [0]; if(empty($first_img)){ // 既沒有縮圖,文中也沒有圖,設定一幅預設的圖片 $first_img = "http://yourdomain.tld/images/default.jpg"; } } return $first_img; }//我用了無效 //獲取第一個視訊 function catch_that_video() { global $post, $posts; $first_video = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/<embed.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); $first_video = $matches [1] [0]; return $first_video; } 圖片呼叫:<img src="<?php echo catch_that_image() ?>" alt="" /> 視訊呼叫:<iframe style="height:250px;width:100%;" src="<?php echo catch_that_video() ?>" ></iframe> ----------------------------------------------------------- //直系子分類 <?php $tax_terms = get_terms('category', array('parent' => $cat));?> <?php foreach($tax_terms as $tax_term){ ?> <h3><?php echo $tax_term->name;?></h3>//分類名稱 <p><?php echo $tax_term->description; ?></p>//分類描述 <?php } ?> ----------------------------------------------------------- <?php $category = get_queried_object_id();?>//獲取當前分類ID <?php echo category_description($category); ?>//獲取當前分類描述 ----------------------------------------------------------- <?php single_cat_title(); ?>//獲取當前分類名 ----------------------------------------------------------- //獲取子分類下的子分類: <?php $tax_terms = get_terms('category', array('parent' => $cat));?>//第一級子分類 <?php foreach($tax_terms as $tax_term){ ?> <div class="dropdown"> <dd class="dropbtn d-dd"><a style="color:black;" href="<?php echo get_category_link($tax_term->term_id); ?>"><?php echo $tax_term->name;?></a><span >>></span></dd> <div class="dropdown-content"> <?php $tax_cterms = get_terms('category', array('parent' => $tax_term->term_id));?>第二級子分類 <?php foreach($tax_cterms as $tax_cterm){ ?> <a href="<?php echo get_category_link($tax_cterm->term_id);?>"><?php echo $tax_cterm->name;?></a> <?php } ?> </div> </div> <?php } ?> //千萬注意:一定要該分類下有文章才會顯示 ----------------------------------------------------------- //獲取該頁面分類下的直系子分類以及該分類下的文章名 <?php $tax_terms = get_terms('category', array('parent' => $cat));?>//直系子分類 <?php foreach($tax_terms as $tax_term){ ?> <div class="dropdown"> <dd class="dropbtn d-dd"><a style="color:black" href="<?php echo get_category_link($tax_term->term_id); ?>"><?php echo $tax_term->name;?></a><span >>></span></dd> <div class="dropdown-content"> <?php query_posts('cat='.$tax_term->term_id);?> //該分類下的文章 <?php while (have_posts()) : the_post(); ?> <a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a> <?php endwhile;?> <?php wp_reset_query(); ?> </div> </div> <?php } ?> ----------------------------------------------------------- //直系子分類列表 <?php $tax_terms = get_terms('category', array('parent' => $cat));?> <?php foreach($tax_terms as $tax_term){ ?> <div class="row"> <div class="col-md-12 col-xs-12 col-sm-12"> <div class="row"> <div class="col-md-5 col-xs-12 col-sm-12 content-img"> <img src="<?php echo z_taxonomy_image_url($tax_term->term_id); ?>" /> //分類圖片呼叫 </div> <div class="col-md-7 col-xs-12 col-sm-12 s-right"> <h3><?php echo $tax_term->name;?></h3> <p><?php echo $tax_term->description; ?></p> //分類資訊描述呼叫 <button onclick="javascript:window.location.href='<?php echo get_category_link($tax_term->term_id); ?>'">點選瞭解</button> </div> </div> </div> </div> <div class="row">&nbsp;</div> <?php } ?> ----------------------------------------------------------- //文章頁獲取分類資訊 <?php ob_start(); $cat_class=the_category_id();ob_clean(); //獲取id值,the_category_id()會直接輸出id,ob_start和clean為了清除此輸出 $tax_terms = get_terms('category',array('parent' =>get_category_root_id($cat_class)));?> <?php foreach($tax_terms as $tax_term){ ?> <div class="dropdown"> <dd class="dropbtn d-dd"><a style="color:black" href="<?php echo get_category_link($tax_term->term_id); ?>"><?php echo $tax_term->name;?></a><span >>></span></dd> <div class="dropdown-content"> <?php query_posts('cat='.$tax_term->term_id);?> <?php while (have_posts()) : the_post(); ?> <a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a> <?php endwhile;?> <?php wp_reset_query(); ?> </div> </div> <?php } ?> ----------------------------------------------------------- //子分類 $tax_term->term_id //下一級子分類id $tax_cterm->term_id ----------------------------------------------------------- //獲取分類描述資訊 <?php echo $tax_term->description; ?> //獲取分類圖片 <?php echo z_taxonomy_image_url($tax_term->term_id); ?> ----------------------------------------------------------- //文章內容頁加上一頁和下一頁 <?php previous_post_link("上一頁: %link") ?> <?php next_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 "已是最新文章";} ?> ----------------------------------------------------------- //文章頁顯示父分類名 <?php foreach((get_the_category()) as $category){echo $category->cat_name;}?> ----------------------------------------------------------- //wordpress呼叫不帶超連結的Tag標籤 function tagtext(){ global $post; $gettags = get_the_tags($post->ID); if ($gettags) { foreach ($gettags as $tag) { $posttag[] = $tag->name; } $tags = implode( ',', $posttag ); echo $tags; } } <?php tagtext();?> ----------------------------------------------------------- //獲取文章指定自定義欄位值 <?php $xzmeta = get_post_meta(get_the_ID(),'xzmeta',true);?> ----------------------------------------------------------- //迴圈獲取文章全部自定義欄位和值 <?php $lianjie='#'; $custom_field_keys = get_post_custom_keys(); foreach ( $custom_field_keys as $key => $value ) { $valuet = trim($value); if ( '_' == $valuet{0} ) continue; $f = get_post_meta($post->ID, $value, true); if($value=="訂購連結"){ $lianjie=$f; } if($value=="custom_post_template") { echo ""; } else { echo "<p>".$value.":".$f."</p>"; } } echo "<div id='".lianjie."'><a href='".$lianjie."'>訂購連結</a></div>"; ?> ----------------------------------------------------------- <?php echo get_settings('home'); ?> //首頁超連結 -----------------------------------------------------------