文章

將WordPress某分類隱藏

將Wordpress某分類隱藏

當我們想在網站的分類列表中排除某些分類時,有兩種方式:

1.修改

wp-includes 文件夾的 category-template.php 檔

搜尋"wp_list_categories"

'exclude' => '分類的 ID',// 排除分類的 ID ,多個用","分隔

例如我要排除分類ID為2,可改成:

'exclude' => '2',

若是要排除多個分類ID:

'exclude' => '2,3,4',

 

2.修改functions.php

function exclude_widget_categories($args){
$exclude = "分類ID,分類ID";
$args["exclude"] = $exclude;
return $args;
}
add_filter("widget_categories_args","exclude_widget_categories");

WordPress 抓取文章第一張圖片顯示在首頁

WordPress 某些主題在首頁只會顯示"特色圖片",你可以用以下兩種方式直接顯示文章的第一張照片

方式1
//抓取文章第一張圖片
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 = "/images/default.jpg";
}
return $first_img;
}

<p><img src="<?php echo catch_that_image() ?>"></p><!-- 顯示第一張圖片 -->

方式2:
複製下面的代碼到 functions.php 中

//Automatically Set the Featured Image in WordPress
function autoset_featured() {
global $post;
$already_has_thumb = has_post_thumbnail($post->ID);
if (!$already_has_thumb)  {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
}
}
}
}  //end function
add_action('the_post', 'autoset_featured');
add_action('save_post', 'autoset_featured');
add_action('draft_to_publish', 'autoset_featured');
add_action('new_to_publish', 'autoset_featured');
add_action('pending_to_publish', 'autoset_featured');
add_action('future_to_publish', 'autoset_featured');