Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

WordPress Snippet

ワードプレスをカスタマイズしよう

最近の投稿ウィジェットを個別投稿サイドバーで、投稿タイプ毎にスイッチする

カスタム投稿は、カスタム投稿を作成する手順の他にテンプレートの扱い、カスタム投稿アーカイブと個別カスタム投稿の遷移周りをうまく設定しておく必要があり、全体としてちょっとスキルが高めの作業に思われていますが、

カスタム投稿タイプはCustom Post Type UIプラグインを使うと比較的簡単に作成できます。

Display Posts Shortcodeのようなブラグインを使えば、リスト形式で、カスタム投稿の投稿一覧をテキストウィジェットに表示することが出来ます。

Display Posts Shortcodeをテキストウィジェットに表示する場合のTips

テーマのfunctions.phpに、以下のコードを追加してテキストウィジェットでショートコードが有効になるよう設定が必要な場合があります。

add_filter('widget_text','do_shortcode');

テキストウィジェットで、リストを表示する場合に、ショートコードが反映しない場合は、試してみてください
以下のように、post_typeを指定してやることで特定のカスタム投稿タイプのリストを表示することが出来ます

[display-posts post_type="movies" posts_per_page="3"]

このショートコードは、デフォルトの投稿で、トップページに「最近の投稿」のような、時系列にピックアップしたリストに相当します。

ただ実際問題、このリストでは満足いかず。自身でショートコードのスクリプトを書き出すことも少なくないのではないかと思います。

つまり、デザイン的な問題や、表示したい内容に見合うhtmlブロックが欲しくなったりスタイルを連携させたいといった事でカスタムコードを記述する事が多いと思いますが、カスタム投稿が複数設定される場合など、それぞれにカスタムショートコードを記述するのも やや、難儀です。

以下のコードは、カスタム投稿が複数存在しても、それなりに 最近のカスタム投稿一覧をシングルページで表示しようというものです。

この方法のメリットは、標準のウィジェットを利用するため、新たにスタイルを設定したりする必要がなく、複数のカスタム投稿が存在しても、それぞれのカスタム投稿毎に何か特別な事をしなくて済む点にあります。

functions.phpに以下のPHPコードを追加します。

add_filter('widget_posts_args', 'filter_custom_post_content');

function filter_custom_post_content( $args ) {
	
	if ( is_singular() ) {
		$post_type= get_post_type( get_the_ID() );

		if ( "post"== $post_type | | "page"== $post_type | | "attachment"== $post_type | | "revision"== $post_type | | "nav_menu_item"== $post_type ) {
			return $args;
		}
		$obj= get_post_type_object( $post_type );
		
		if ( !empty( $obj ) && true== $obj->has_archive ) {
			$args['post_type']= $post_type;
		}
	}
	return $args;
}
add_filter( 'widget_title','filter_custom_post_title',10,3 );

function filter_custom_post_title( $title, $instance, $id_base ) {
	
	if( 'recent-posts'== $id_base ) {
		if ( is_singular() ) {
			
			$post_type= get_post_type( get_the_ID() );

			if ( "post"== $post_type | | "page"== $post_type | | "attachment"== $post_type | | "revision"== $post_type | | "nav_menu_item"== $post_type ) {

				return $title;
			}
			$obj= get_post_type_object( $post_type );

			if ( !empty( $obj ) && true== $obj->has_archive ) {

				return sprintf(__('最近の%1$s','text_domain'),$obj->label);
			}		
		}		
	}
	return $title;
}

[emulsion_relate_posts]