• 先ず或るカテゴリーに入った時にそのカテゴリーに属する投稿の一覧をサイドバーに表示する方法は以下を読んで、これでいける思ったのですが、その後のWordPressのバージョンアップに伴ってよりスマートなアプローチ法がもし在るなら教えて頂きたいのと、
    http://ja.forums.wordpress.org/topic/609?replies=5

    サイドバーに表示する項目は全てウェジットを使っているのですが、上記方法だとウェジットとバッティング(ウェジットが上書きされて上記記述が無視される、または、この反対)が起るのではないか?と心配しております。この点どうでしょうか?

    それとタイトルのサイドバーに表示させる一覧の表示順序をコントロールしたいのですが、、、たぶんカスタムフィールドを使うという手法になると思うのですが、具体的にどう使えば良いのか教えて頂けると助かります。
    ウェブ検索しても「カスタムフィールドを使うと便利!」というセリフは沢山散見されるものの具体例を挙げて「どういうケースの時に」「こういう使い方をすると便利」という説明が全然見付からず「便利そうなのはわかったけど、で、結局の処どういう使い方があるの?(・_・?)」となっています。

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • モデレーター Takuro Hishikawa

    (@hissy)

    カテゴリー一覧にてそのカテゴリーの投稿一覧をサイドバーに投稿する方法ですが、リンク先の方法でできます。dynamic_sidebar()の外に書けばウィジェットとバッティングもしませんし。
    このあたりの仕組みはバージョンアップによってそうそう変わるものではありません。また充分スマートな気がします。

    一応、ウィジェットとして実装することも可能ですが…(下記コードをテーマのfunctions.phpの末尾にコピペして保存してみてください)

    class WP_Widget_InCategory_Recent_Posts extends WP_Widget {
    
    	function WP_Widget_InCategory_Recent_Posts() {
    		$widget_ops = array('classname' => 'widget_incategory_recent_entries', 'description' => __( "The most recent posts in category on your site") );
    		$this->WP_Widget('incategory-recent-posts', __('In Category Recent Posts'), $widget_ops);
    		$this->alt_option_name = 'widget_incategory_recent_entries';
    
    		add_action( 'save_post', array(&$this, 'flush_widget_cache') );
    		add_action( 'deleted_post', array(&$this, 'flush_widget_cache') );
    		add_action( 'switch_theme', array(&$this, 'flush_widget_cache') );
    	}
    
    	function widget($args, $instance) {
    		global $cat;
    		$cache = wp_cache_get('widget_incategory_recent_posts', 'widget');
    
    		if ( !is_array($cache) )
    			$cache = array();
    
    		if ( isset($cache[$args['widget_id']]) ) {
    			echo $cache[$args['widget_id']];
    			return;
    		}
    
    		ob_start();
    		extract($args);
    
    		$title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Posts') : $instance['title'], $instance, $this->id_base);
    		if ( !$number = (int) $instance['number'] )
    			$number = 10;
    		else if ( $number < 1 )
    			$number = 1;
    		else if ( $number > 15 )
    			$number = 15;
    
    		$r = new WP_Query(array('showposts' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'caller_get_posts' => 1, 'cat' => $cat));
    		if ($r->have_posts()) :
    ?>
    		<?php echo $before_widget; ?>
    		<?php if ( $title ) echo $before_title . $title . $after_title; ?>
    		<ul>
    		<?php  while ($r->have_posts()) : $r->the_post(); ?>
    		<li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a></li>
    		<?php endwhile; ?>
    		</ul>
    		<?php echo $after_widget; ?>
    <?php
    		// Reset the global $the_post as this query will have stomped on it
    		wp_reset_postdata();
    
    		endif;
    
    		$cache[$args['widget_id']] = ob_get_flush();
    		wp_cache_set('widget_incategory_recent_posts', $cache, 'widget');
    	}
    
    	function update( $new_instance, $old_instance ) {
    		$instance = $old_instance;
    		$instance['title'] = strip_tags($new_instance['title']);
    		$instance['number'] = (int) $new_instance['number'];
    		$this->flush_widget_cache();
    
    		$alloptions = wp_cache_get( 'alloptions', 'options' );
    		if ( isset($alloptions['widget_incetegory_recent_entries']) )
    			delete_option('widget_incategory_recent_entries');
    
    		return $instance;
    	}
    
    	function flush_widget_cache() {
    		wp_cache_delete('widget_incaegegory_recent_posts', 'widget');
    	}
    
    	function form( $instance ) {
    		$title = isset($instance['title']) ? esc_attr($instance['title']) : '';
    		if ( !isset($instance['number']) || !$number = (int) $instance['number'] )
    			$number = 5;
    ?>
    		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
    		<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
    
    		<p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of posts to show:'); ?></label>
    		<input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
    <?php
    	}
    }
    add_action( 'widgets_init', create_function( '', 'return register_widget("WP_Widget_InCategory_Recent_Posts");' ) );

    表示順のコントロールは違う話題ですので別トピックにされたほうがいいかも。
    カスタムフィールドに数値を保存しておいて、その数値の昇順/降順で並び替えるということになると思いますが管理は大変になりますよ。

    投稿の順番を指定するプラグインです。両プラグインとも、ポストの menu_order フィールド(固定ページの順番を指定するフィールド)を使っています。

    1) プラグイン postMash(Filtered)
    プラグインを有効にして、[投稿-postmash] で順番を Drag&Drop で決定します。
    カテゴリーページ、サイドバーなどで、投稿が指定順に表示されます。
    ここは、日付順で、ここは指定順番で、ということはできません。

    2) プラグイン CMS Tree Page View
    固定ページとカスタム投稿タイプにしか対応していませんが、
    /plugins/cms-tree-page-view/index.php で「continue」(1ヶ所)を見つけこれをコメントにしてやると、投稿でも使えるようになります。

    どちらかというと、NEWS(投稿)は日付順、商品(カスタム投稿タイプ)は指定順で表示したいというようなときに便利です。

    プラグインを有効にして、[投稿-投稿 Tree View] で順番を Drag&Drop で決定します。

    以下テンプレートのコード

    functions.php で
    <?php
    function cms_view_orderPosts($orderBy) {
    	global $wpdb;
    	$orderBy = "{$wpdb->posts}.menu_order ASC";
    	return($orderBy);
    }
    ?>

    を定義して、順番指定が必要なところで、

    <?php
    	add_filter('posts_orderby', 'cms_view_orderPosts');
    	query_posts($query_string);
    	remove_filter('posts_orderby', 'cms_view_orderPosts');
    ?>
    
    とか、
    
    <?php
    	add_filter('posts_orderby', 'cms_view_orderPosts');
    	query_posts('cat=10&posts_per_page=-1');
    	remove_filter('posts_orderby', 'cms_view_orderPosts');
    ?>
    
    にします。

    直接SQLを使って投稿を抽出しているプラグイン(サイトマップなど)は、プラグインを直接修正するしかありません。

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • トピック「そのカテゴリーに属する投稿一覧をサイドバーに表示する時の並び順のコ」には新たに返信することはできません。