Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-settings.php on line 472

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-settings.php on line 487

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-settings.php on line 494

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-settings.php on line 530

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-includes/cache.php on line 103

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-includes/query.php on line 21

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-includes/theme.php on line 623

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/custom_posts/custom_posts.php on line 22

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/pukiwiki.php on line 46

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/pukiwiki.php on line 67

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/pukiwiki.php on line 75

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/pukiwiki.php on line 92

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/modPukiWiki/class/PukiWikiRender.php on line 70

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/modPukiWiki/class/PukiWikiElement.php on line 181

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/modPukiWiki/class/PukiWikiElement.php on line 538

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/modPukiWiki/class/PukiWikiElement.php on line 695

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/modPukiWiki/class/PukiWikiElement.php on line 1087

Deprecated: Function ereg() is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/wp-ban/wp-ban.php on line 112

Deprecated: Function ereg() is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/wp-ban/wp-ban.php on line 112

Deprecated: Function ereg() is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/wp-ban/wp-ban.php on line 112

Deprecated: Function ereg() is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/wp-ban/wp-ban.php on line 112

Deprecated: Function ereg() is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/wp-ban/wp-ban.php on line 112

Deprecated: Function ereg() is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/wp-ban/wp-ban.php on line 112
[WordPress] 現在のクエリを変更してループに。 | おれせか
おれせか

なんかそれっぽいのをうにうに。


Deprecated: Function split() is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/modPukiWiki/lib/func.php on line 75

Deprecated: Function ereg() is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/modPukiWiki/plugin/ref.inc.php on line 207

Deprecated: Assigning the return value of new by reference is deprecated in /home/users/1/fool.jp-ichi/web/blog/wp-content/plugins/modPukiWiki/plugin/region.inc.php on line 14

「カテゴリページだけ1ページの表示数を○件に~」というように、現在のループ(カテゴリページならその該当カテゴリ、日付ページならその日付って条件の。)のクエリをちょこっと変更したいとき、query_posts()get_posts()だとまっさらな状態からループをつくってしまい、現行ループのクエリが残らない。

色々調べてみたんだけど結局どうすりゃいいのかよくわかんなかったので、今あるループの一部のクエリだけ変更してループ生成するプラグインをつくてみた。

ダウンロード:custom_posts.zip

使い方とか

<?php custom_posts('posts_per_page=30&order=ASC'); //←こんなかんじ ?>
<?php while (have_posts()) : the_post(); ?>
    <!-- ~ループの中~ -->
<?php endwhile; ?>

ソース

<?php
/*
Plugin Name: custom posts
Plugin URI: http://ichi.fool.jp/blog/
Description: 現在のクエリを変更してループに。
Version: 0.1
Author: ichi
Author URI: http://ichi.fool.jp/blog/
*/ 

function &custom_posts($query){
    if(!$query) return;

    $temp_query_vars = $GLOBALS['wp_query']->query_vars;

    if(!is_array($query))
        parse_str($query, $query);

    $query = array_merge($temp_query_vars, $query);

    unset($GLOBALS['wp_query']);
    $GLOBALS['wp_query'] =& new WP_Query();
    return $GLOBALS['wp_query']->query($query);
}
?>

やってることは単純。query_posts()の改変。今あるqueryにちょろと上書きしてもっかいループ生成。

PAGETOP
PAGETOP
PAGETOP

Comments (12)

query_postsのposts_per_pageのバグ?に数か月間悩まされていましたが、
こちらの記事のおかげで解消できました。ありがとうございました。

tsubu
[2009/03/20 (金) 16:19]

すぐ出来そうな事なのになかなか出来なくって、こゆ需要けっこうあるんじゃないかなーとか思いつつ書いたので、お役に立てたようでなによりですー。

[2009/03/20 (金) 18:28]

私はこのブログは本当に私はそれを行うように確信させたことを申し上げたいと思います!おかげで、非常に良い記事。

[2012/12/20 (木) 16:42]

I hesitate a whole lot and don’t manage to get nearly anything done.

[2013/05/21 (火) 12:23]

The blog is awesome, how do you manage to do this?

[2013/12/19 (木) 06:39]

しっかり読まなくてもアジェンダの応用例見るだけでも何ができるのかはすぐわかるよ。

[2014/12/25 (木) 13:00]

流行を見抜くことができるのも実は巡回するで。

異なる風格は過ぎて数年また流行り始めて。

休む騒動の飛躍、各種が高くて大きい上の贅沢なブランドにも次から次へとこ。

[2015/09/16 (水) 15:58]

l直書きでネスト作るこ

[2015/10/13 (火) 17:00]

かつて2015年のバーゼルのシャネル激安時計展初めて見得、

シャネルの超薄自動的にの上で鎖陀はずみ車腕時計、
今年プラチナの金の上着をはおって、

そのデザインはアイデアを高く備えて、
シャネル激安http://www.cctoh.com/

シャネルバッグ、腕時計、財布、靴、ピアス、眼鏡

超薄は表して殻のシャネル中で時計の歴史、
上で最も偉大な発明の中の一つを埋蔵していて。

[2015/11/23 (月) 00:35]

てのがマスター/サーヴァントだったりとか。

[2015/11/23 (月) 17:30]

まぁなんかあるでしょ。

[2016/01/15 (金) 17:48]

やってることは単純。query_posts()の改変。今あるqueryにちょろと上書きしてもっかいループ生成。

[2016/06/20 (月) 11:00]
PAGETOP

Comment Form

PAGETOP

Trackbacks (0)

トラックバックはありません。

PAGETOP

Trackback URL

PAGETOP

Search