Crazy about ActionScript or Something!

WordPress 小技覚え書き

投稿内のパスを変更する

MT からの乗り換え時に画像が色々な場所にアップされてしまっていたので、旧画像置き場のパスを変更しました。手作業で変更していたらそりゃもうあんた大変ですね。
phpAdmin などで以下を実行します。

UPDATE テーブル名 SET フィールド名 = REPLACE (フィールド名, '検索文字列', '置換文字列');

img ディレクトリから img-old ディレクトリに変更する場合、以下の記述になります。

UPDATE wp_posts SET post_content = REPLACE  (post_content, 'http://blog.chimanaco.net/img', 'http://blog.chimanaco.net/img-old');

特定の親カテゴリーの場合のみ表示する

例えばTee カテゴリに所属する記事を表示したい場合、以下のように記述します。

投稿画面で Teeカテゴリ にチェックを入れる場合

<?php if ( in_category( 'Tee' ) ): ?>
// Teeカテゴリ特有の 記述
<?php endif; ?>


Tee のサブカテゴリがあって、投稿画面で Tee カテゴリにチェックを入れない場合

(汎用性は無いですが…)

<?php if ( is_tee ( get_the_category() ) ): ?>
  // Teeカテゴリ特有の 記述
<?php endif; ?>

function.php に記述

// 6 = Tee カテゴリの番号

function is_tee($cat) {
	//$cat = get_the_category(); 省略
	$cat = $cat[0];
	$parents = (int)$cat->category_parent;
	if ( 6 ==$parents ){
		return true;
	}else {
		return false;
	}
}

ループの中で最初や最後や奇数とかを判別する

function.php で関数を設定します。

function isFirst(){
    global $wp_query;
    return ($wp_query->current_post === 0);
}

function isLast(){
    global $wp_query;
    return ($wp_query->current_post+1 === $wp_query->post_count);
}

function isOdd(){
    global $wp_query;
    return ((($wp_query->current_post+1) % 2) === 1);
}

function isEvery(){
    global $wp_query;
    return ((($wp_query->current_post+1) % 2) === 0);
}

例えば最後の記事のみ背景を変えたいなどという場合、最後のみ Last クラスを付与します。

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="section entry <?php if(isLast())echo Last; ?>">
</div>
<?php endwhile; else: ?>
<?php endif; ?>

css に 該当する class の記述をします。

.Last {
 background: #FF0000;
}

まだまだあった気がするが忘れてる

0 Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Comments links could be nofollow free.

(c) 2010 chimanaco blog | powered by WordPress with Barecity