2016-12-28 1 views
0

WordPress의 페이지에서 비주얼 페이지 빌더에서 사용할 단축 코드를 만들 수있는 방법이 있는지 궁금합니다. PHP 포함. form.php라는 파일의 PHP 코드를 내 페이지에 추가해야하고 비주얼 페이지 빌더 만 HTML을 추가 할 수 있습니다. 가능한 경우 플러그인을 사용하지 않는 것이 좋습니다. 이 코드를 사용해야하지만 인식하지 못합니다.WordPress 게시물 및 페이지에 사용하기 위해 PHP 포함과 단축 코드 만들기

<?php include 'form.php';?> 

답변

1

물론 - 당신이 (포함하는 파일)을 하나 개의 매개 변수를 취하는 단축 코드를 가지고 단순히 콘텐츠를 출력 할 수 있습니다이 도움이

add_shortcode('include', 'include_php_file'); 
function include_php_file($atts=array(), $content='') { 
    $atts = shortcode_atts(array(
     'file' => '' 
    ), $atts); 
    if(!$atts['file']) return ''; // needs a file name! 
    $file_path = dirname(__FILE__).'/'.$atts['file']; // adjust your path here 
    if(!file_exists($file_path)) return ''; 
    ob_start(); 
    include($file_path); 
    $html = ob_get_contents(); 
    ob_end_clean(); 
    return $html; 
} 

희망을!

+0

덕분에 응답을 위해? 그런 다음 페이지에서 사용할 수있는 단축 코드는 무엇입니까? –

+0

오, 미안, 그 부분을 놓친 거지? 그래서 예, 당신의 테마의 function.php 파일에 이것을 추가 할 것입니다. (자신 만의 테마가 아니라면, 당신이 아이 테마를 만들었 으면 좋겠다.) 다음은 이것을 사용하는 페이지 또는 게시물의 콘텐츠 상자에 넣으십시오 : [include file = 'form.php'] – MacPrawn

0

해당 페이지의 템플릿을 편집하고 해당 게시물에 대해서만 해당 파일을 조건부로 포함 할 수도 있습니다.

ES :

if(is_page('contact') include('path/to/file.php'); 

당신이 is_page에 배열을 전달 하나 이상의 페이지에서 해당 파일을 필요로하는 경우 : 나는 functions.php 파일에 이것을 추가하려면,

if(is_page(array('about-us', 'contact', 'management')) 
    //either in about us, or contact, or management page is in view 
    include('path/to/file.php'); 
else 
    //none of the page about us, contact or management is in view 
관련 문제