programing

워드프레스 메뉴에 단축코드를 삽입하는 방법

lastmemo 2023. 3. 17. 19:34
반응형

워드프레스 메뉴에 단축코드를 삽입하는 방법

이 코드로 메뉴를 만들었습니다.메뉴 항목은 표시되지만 쇼트 코드 출력은 표시되지 않습니다.추가할 수 있는 방법이나 다른 방법이 있나요?나는 또한 이것이 도움이 되기를 바라며 덧붙였다.

add_filter('wp_nav_items', 'do_shortcode', 7);

아니면 누군가가 이것이 불가능하다는 것을 알고 나에게 말할 수도 있다.

/* Nav Menu */
function add_profile_link_to_nav(){ 
 if ( is_user_logged_in() ) { ?> 

<ul> 
  <li class="menu-item"id="one"> <a href="http://example.com/members/">All  Members</a>
  <ul class="sub-menu"> 
      <li class="menu-item"><?php echo custom_execute_shortcode(); ?> </li>
  </ul> 
 </li>
</ul>    <!--end menu--->
<?php } 
}
add_action( "wp_nav_items","add_profile_link_to_nav" );

function custom_execute_shortcode() {
$myfunction= '[my shortcode"]';
$myfunction_parsed = do_shortcode($myfunction);
return $myfunction_parsed;
}

감사해요.

@Tim 이 코드는 동작합니다.

기능을 합니다.php 파일

add_filter('wp_nav_menu_items', 'do_shortcode');

괄호가 제거되므로 메뉴 페이지의 메뉴 URL에서 바로 쇼트 코드를 사용할 수 없습니다.그러나 플레이스 홀더를 다음과 같이 사용할 수 있습니다.#profile_link#.

다음 코드를 입력했을 경우functions.php, URL 을 사용하여 커스텀메뉴 항목을 작성할 수 있습니다.#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' );

설정만 하면 됩니다.'shortcode'에서$placeholders어레이 및 옵션'atts'그리고.'content'.

예를 들어 쇼트코드가 다음과 같은 경우:

[example id="5" other="test"]Shortcode content[/example]

업데이트:

'#placeholder#' => array(
    'shortcode' => 'example';
    'atts' => array( 'id' => '5', 'other' => 'test' );
    'content' => 'Shortcode content';
),

이 경우 리소스를 많이 사용하는 기능으로 작업에 적합한 도구가 아니기 때문에 사용하지 않습니다.

메뉴 페이지에서 설명을 활성화하고 단축코드 링크 설명 텍스트 영역에 붙여넣습니다.php 다음 코드 추가:

add_filter('walker_nav_menu_start_el', function($item_output, $item) {
    if (!is_object($item) || !isset($item->object)) {
        return $item_output;
    }

    if ($item->ID === 829) {
        $item_output = do_shortcode($item->description);
    }

    return $item_output;
}, 20, 2);

언급URL : https://stackoverflow.com/questions/11403189/how-to-insert-shortcode-into-wordpress-menu

반응형