2013-08-12 1 views
0

나는 다음과 같은 코드를 작성하여 여러 개의 게시물을 작성했습니다. 이 훌륭하게 떨어져 한 가지에서 작동하고 그 어떤 페이지에 링크 드롭 다운 목록에 기본 값을 추가 할 수있는됩니다Wordpress - 아무 페이지로도 링크하지 않는 드롭 다운에 기본값 추가하기

<?php 
$mypostype = get_posts('post_type=rentals'); 
if($mypostype) : ?> 
<form action="" id="myform"> 
<label for="myselect">Rentals</label> 
<select id="myselect" name="select_post_type" onChange='document.location.href=this.options[this.selectedIndex].value;'> 
<?php foreach ($mypostype as $mypost ) : ?> 
<option value="?rentals=<?php echo $mypost->post_name; ?>"><?php echo $mypost->post_title; ?></option> 
<?php endforeach; ?> 
</select> 
</form> 
<?php endif ?> 

같은 텍스트는 '대여 선택'.

그것은 누군가가이 값을 추가하는 방법을 설명 할 수 있다면. 시간에 대한

감사 좋을 사전에 도움이 될 것이다. 선택된 옵션의 값이 비어 있지 않은 경우 <select><option> 및 확인하여 자바 스크립트 onChange 경우에 약간의 변화를 추가함으로써

답변

0

: 이것은 당신의 요구를 충족해야

<?php 
    $mypostype = get_posts('post_type=rentals'); 
    if($mypostype) : ?> 
    <form action="" id="myform"> 
    <label for="myselect">Rentals</label> 
    <select id="myselect" name="select_post_type" onChange="((this.options[this.selectedIndex].value != '') ? document.location.href=this.options[this.selectedIndex].value : false);"> 
    <option value="" selected="selected">Select Rentals</option> 
    <?php foreach ($mypostype as $mypost ) : ?> 
    <option value="?rentals=<?php echo $mypost->post_name; ?>"><?php echo $mypost->post_title; ?></option> 
    <?php endforeach; ?> 
    </select> 
    </form> 
    <?php endif ?> 

...

그런데 당신은 포스트 링크 (절대 링크)를 에코하려면 다음과 같이 할 수 있습니다.

<option value="<?php echo get_permalink($mypost->ID); ?>"><?php echo $mypost->post_title; ?></option> 
+0

이것은 거의 트릭입니다. 이제 기본 옵션이 채워지지만 게시물을 선택하면 더 이상 자동으로 페이지로 이동하지 않습니다. – Johnny

+0

javascript 문장에서'return' 키워드를 삭제했습니다. 나는 그것을 테스트하고 잘 작동합니다. – Strategio

관련 문제