2016-08-06 3 views
0

私は次のJavaScriptjavascriptでwordpress php関数を使う方法は?

var val; 
function OnChangeSelect() 
{ 
    var e=document.getElementById("typeselect"); 
    val = e.value; 
    if(val==2) 
    { 
     document.getElementById("formbody").innerHTML ="<?php get_template_part("+"publish_game_nr"+");?>"; 
    } 
    else 
    { 
     document.getElementById("formbody").innerHTML ="<?php get_template_part("+"publish_game_fe"+");?>"; 
    } 
} 

にリンクして、テンプレートのルートディレクトリ

<?php 
    /** 
     ** 
     * Template Name: publish 
     * 
     * @package WordPress 
     * @subpackage Twenty_Fourteen 
     * @since Twenty Fourteen 1.0 
     */ 
     ?> 
    <link rel="stylesheet" type="text/css" href="../Asset/css/style.css"> 
    <script src="../Asset/scripts/script.js"></script> 
    <?php 
    get_header(); ?> 
    <div class="col-md-12"> 
     <div style="background-color: #D3D3D3;height:1px;"></div> 
     </div> 
    <div id="primary" class="content-area"> 
    <main id="main" class="site-main site-main--single" role="main"> 
    <div class="container-fluid"> 

    <div class="col-md-5"></div> 

    <div class="col-md-2"> 
    <div align="center" > 
     <select id="typeselect" name="value" onChange="OnChangeSelect()"> 
      <option value="1">Featured</option><br /> 
      <option value="2">General</option><br /> 
     </select> 
    </div> 
     </div><!-- type select--> 

     <div class="col-md-12"> 
     <div style="background-color: #D3D3D3;height:1px;margin:5px"></div> 
     </div> 

    <div id="formbody" class="col-md-12"> 
     <!-- include php files here --> 
    </div> 

    <script>OnChangeSelect()</script> 
    </div><!-- container --> 
    </main> <!-- #main --> 
    </div> <!-- #primary --> 

<?php //get_sidebar(); ?> 
<?php get_footer(); ?> 

でカスタムページpublish.phpを持っている私は

<div id="formbody" class="col-md-12"> 
    <!-- include php files here --> 
</div> 

の内側に内容を変更したいですプルダウンメニューで選択したオプションに基づいてpublish.php

ので、私は2つのseprate PHPファイルpublish_game_nr.phpjavascriptpublish_game_fe.php

divinnerhtmlwordpress

get_template_part() 

を使用してこれらのPHPファイルを含めるように変更しようとしていますを作成しましたが、ファイルがロードされません。私がdivタグで直接関数を使用すると。ファイルが正常に読み込まれます

これを行う正しい方法は何ですか?

答えて

0

はこれを試してみてください。

document.getElementById("formbody").innerHTML ="<?php get_template_part('slug'); ?>"; 
+0

を使用しました動作していない –

0

これは

簡単だったので、私は

<div id="formbody" class="col-md-12"> 
    <div style="display:none" id="featured-div"> 
    <?php get_template_part("publish_game_fe") ?> 
    </div> 
    <div style="display:block" id="general-div"> 
    <?php get_template_part("publish_game_nr") ?> 
    </div> 
</div> 

にformbodyのdivを変更していた、すでに次のJavaScriptコード

function OnChangeSelect() 
{ 
    var e=document.getElementById("typeselect"); 
    var val = e.value; 
      if(val==1){ 
     document.getElementById("general-div").style.display="block"; 
     document.getElementById("featured-div").style.display="none"; 
     } 
     else{ 
     document.getElementById("general-div").style.display="none"; 
     document.getElementById("featured-div").style.display="block"; 
     } 
} 
関連する問題