22のWordPressテーマハック

Ads

wordpress1
instantShiftより22のWordPress
のテーマハック方法とソースコードが
公開されていたのでご紹介します。
今日は私用のため、時間が無いので
はしょってご紹介しますので
残りは記事元でご確認下さい。

いずれもプラグインを使用しない方法です。

人気の記事を抽出する

sidebar.phpを開いて以下のコードを挿入。

<h2>Popular Posts</h2>
<ul>
<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
foreach ($result as $post) {
setup_postdata($post);
$postid = $post->ID;
$title = $post->post_title;
$commentcount = $post->comment_count;
if ($commentcount != 0) { ?>

<li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>">
<?php echo $title ?></a> {<?php echo $commentcount ?>}</li>
<?php } } ?>

</ul>

関連記事を抽出する

single.phpを開いて以下のコードを任意の場所に挿入。

<?php
//for use in the loop, list 5 post titles related to first tag on current post
$tags = wp_get_post_tags($post->ID);
if ($tags) {
  echo 'Related Posts';
  $first_tag = $tags[0]->term_id;
  $args=array(
    'tag__in' => array($first_tag),
    'post__not_in' => array($post->ID),
    'showposts'=>5,
    'caller_get_posts'=>1
   );
  $my_query = new WP_Query($args);
  if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
      <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
      <?php
    endwhile;
  }
}
?>

記事の最初の画像を自動表示

functions.phpに以下を追加

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;
}

画像を表示したい場所に以下のコードを挿入。

<?php echo catch_that_image() ?>

「印刷する」ボタンを追加

以下のコードを印刷用のリンクを出したい箇所に挿入。

<a href="javascript:window.print()" rel="nofollow">Print this Article</a>

その他、トラックバックとコメントを分ける方法や、画像ギャラリーを
作る方法、Feedburnerの人数をテキスト表示する方法、などなど
22のカスタマイズ方法が掲載されています。リンク先でどうぞ。

22 Mixed Quality WordPress Hacks

タイトルとURLをコピーしました