をそれを置く動作しますブラケットが外されてしまうためです。しかし、プレースホルダは#profile_link#
のように使用できます。
functions.php
に次のコードを入力すると、#profile_link#
のカスタムメニュー項目を作成できます。これにより、ショートコードに置き換えられます。
/**
* Filters all menu item URLs for a #placeholder#.
*
* @param WP_Post[] $menu_items All of the nave menu items, sorted for display.
*
* @return WP_Post[] The menu items with any placeholders properly filled in.
*/
function my_dynamic_menu_items($menu_items) {
// A list of placeholders to replace.
// You can add more placeholders to the list as needed.
$placeholders = array(
'#profile_link#' => array(
'shortcode' => 'my_shortcode',
'atts' => array(), // Shortcode attributes.
'content' => '', // Content for the shortcode.
),
);
foreach ($menu_items as $menu_item) {
if (isset($placeholders[ $menu_item->url ])) {
global $shortcode_tags;
$placeholder = $placeholders[ $menu_item->url ];
if (isset($shortcode_tags[ $placeholder['shortcode'] ])) {
$menu_item->url = call_user_func(
$shortcode_tags[ $placeholder['shortcode'] ]
, $placeholder['atts']
, $placeholder['content']
, $placeholder['shortcode']
);
}
}
}
return $menu_items;
}
add_filter('wp_nav_menu_objects', 'my_dynamic_menu_items');
あなただけの$placeholders
配列に'shortcode'
を設定し、必要に応じて'atts'
と'content'
する必要があります。あなたのショートはこのようであれば
たとえば、:
は
[example id="5" other="test"]Shortcode content[/example]
あなたは更新されます:それは、リソース集約型の機能ではないですI don't use do_shortcode()
ので
'#placeholder#' => array(
'shortcode' => 'example';
'atts' => array('id' => '5', 'other' => 'test');
'content' => 'Shortcode content';
),
注意をこの場合の仕事のための適切なツールです。
それは実際に働いていたうわー、長い長い時間を探している@Tim responce – xyz
ちょうど私が探していた、ありがとう! – hotshots
@ J.D。 - こんにちは、私はあなたのコードにこのショートコードを追加しようとしていますが、動作しません([ip:GB] 020 7267 5222 [/ ip])。あなたは私を助けてくれるのですか –