表单操作重定向到同一页

时间:2013-02-14 作者:Amos N.

我在做我自己的主题。在我的主题中,我有一个使用自定义查询显示自定义帖子的页面。我向该页面添加了一个表单。表单包含2个组合框,其中包含我的自定义帖子的元数据值。我希望当用户从这个组合框中选择值并单击提交时,Wordpress将再次显示相同的页面,并将提交参数添加到我的自定义查询中(类似于过滤表)。

问题是,当点击提交时,Wordpress会下降到索引。php改为我的页面。

我的表格声明是:

<form action=\'"<?php echo get_page_link(SHIURIM_PAGE)?> "\' method=\'get\'>";
我应该在行动中加入其他东西吗?

感谢是高级的。

我的整页代码:

<?php
/*
Template Name: Shiurim page
*/

require \'search-form.php\';
?>

<?php get_header(); 

global $THEME_PATH;
global $IMAGES_PATH;

?>

<link rel="stylesheet" type="text/css" href="<?php _e($THEME_PATH)?>/shiurim.css">

<div id="page-header-pic">
    <img src=\'<?php _e($IMAGES_PATH)?>/contact-header.jpg\' alt="header picture" />
</div>

<div class="page-title">
    <?php wp_title(); ?>
</div>

<div class="areaBg">
    <div class="page-content-full">
        <div class="panel">
            <div class="panel-title"></div>
            <div class="panel-content">
                <div id="pageContentId">



<?php 
    function getParam($paramName, $defaultValue) {
        if ($_SERVER[\'REQUEST_METHOD\'] === \'GET\') {
            if (!empty($_GET[$paramName])) {
                return $_GET[$paramName];
            }
        }
        else {  // it\'s a post
            if (!empty($_POST[$paramName])) {
                return $_POST[$paramName];
            }
        }
        return $defaultValue;
    }

    $pageIndex = getParam(\'paged\', 0);
    $searchRav = getParam(SEARCH_RAV, 0);
    $searchSidra = getParam(SEARCH_SIDRA, 0);
    $searchText = getParam(SEARCH_TEXT, "");

    $searchText = trim($searchText);

    $queryArgs = array (
        \'post_type\' => array( POST_TYPE_SHIUR ),
        \'orderby\' => \'title\',
        \'order\'    => \'ASC\',
        \'paged\' => $pageIndex
    );

    $paginateSearchArgs = array();
    $metaQuery = array();
    if ($searchRav != 0) {
        $metaQuery[] = array(
            \'key\' => POST_TYPE_RAV,
            \'value\' => $searchRav
        );

        $paginateSearchArgs[SEARCH_RAV] = $searchRav;
    }

    if ($searchSidra != 0) {
        $metaQuery[] = array(
            \'key\' => POST_TYPE_SIDRA,
            \'value\' => $searchSidra
        );

        $paginateSearchArgs[SEARCH_SIDRA] = $searchSidra;
    }

    if (count($metaQuery) > 0) {
        $queryArgs[\'meta_query\'] = $metaQuery;
    }

    if (! empty($searchText)) {
        $queryArgs[\'s\'] = $searchText;

        $paginateSearchArgs[SEARCH_TEXT] = $searchText;
    }

?>

<div id="shiurimSearchPanel">
    <?php renderSearchFrom($searchRav, $searchSidra, $searchText); ?>
</div>
<div>
<?php 
    //query_posts($queryArgs);
    $shiurim_query = new WP_Query( $queryArgs );
    global $wp_query;
    // Put default query object in a temp variable
    $tmp_query = $wp_query;
    // Now wipe it out completely
    $wp_query = null;
    // Re-populate the global with our custom query
    $wp_query = $shiurim_query;

    $big = 999999999; // need an unlikely integer

    echo paginate_links( array(
        \'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
        \'format\' => \'?paged=%#%\',
        \'current\' => max( 1, get_query_var(\'paged\') ),
        \'total\' => $wp_query->max_num_pages,
        \'prev_text\' => \'&lt;&lt;\',
        \'next_text\' => \'&gt;&gt;\'
    ) );
?>
</div>

<?php
    $is_first = true;
    if ( $shiurim_query->have_posts() ) : while ( $shiurim_query->have_posts() ) : $shiurim_query->the_post();

?>
            <?php if (!$is_first) { ?>
                    <div class=\'shiurSeperator\'></div>
            <?php } ?>

            <?php
                $is_first = false;

                get_template_part( "content", "shiur-list" );
            ?> 
<?php endwhile; else: ?>
<p><?php _e(\'Nothing is found\'); ?></p>

<?php 

endif;
// Restore original query object
$wp_query = $tmp_query;
// Be kind; rewind
wp_reset_postdata();

 ?>
                </div>
            </div>
        </div>
    </div>

    <div class="clear"></div>
</div>

<?php get_footer(); ?>
搜索表单。php(我也在index.php中使用它)。

<?php

/**
 *  Search panel
 */

function renderSearchFrom($searchRav = array(), $searchSidra = array(), $searchText = "") {
    $rabanimList = getPostArrayResult(POST_TYPE_RAV);
    $sdatorList = getPostArrayResult(POST_TYPE_SIDRA);  

    $html = "<div id=\'search-panel\'>";
    $html .= "  <form action=\'" . get_page_link(SHIURIM_PAGE). "\' method=\'post\'>";
    $html .= "      By rav";
    $html .= "      <select name=\'search_rav\'>";
    $html .= "          <option value=\'0\'>All ravs</option>";

    foreach ($rabanimList as $rav) {
        $isSelected = \' \';
        if ($rav[\'id\'] == $searchRav) {
            $isSelected = \' selected \';
        }

        $html .= "<option value=\'" . $rav[\'id\'] . "\'" . $isSelected . ">" . $rav[\'title\'] . "</option>";

    } 

    $html .= "</select>";

    $html .= "      By sidra";
    $html .= "      <select name=\'search_sidra\'>";
    $html .= "          <option value=\'0\'>All sidras</option>";

    foreach ($sdatorList as $sidra) {
        $isSelected = \' \';
        if ($sidra[\'id\'] == $searchSidra) {
            $isSelected = \' selected \';
        }

        $html .= "<option value=\'" . $sidra[\'id\'] . "\'" . $isSelected . ">" . $sidra[\'title\'] . "</option>";
    }

    $html .= "</select>";

    $html .= "      Free text";
    $html .= "      <input type=\'text\' name=\'search_text\' class=\'searchFreeText\' value=\'" . $searchText . "\'/>";

    $html .= "      <input type=\'submit\' value=\'Search\' />";
    $html .= "  </form>";
    $html .= "</div>";

    echo $html;
}

?>

4 个回复
最合适的回答,由SO网友:Amos N. 整理而成

我找到了问题和解决方案。

The problem:

使用:

<form action="<?php echo get_page_link(66) ?>"  method="get"> 
这不好。在生成的HTML中,我可以看到:

<form action="http://mySite/?page_id=66" method="get">
但是,在提交表单时?page\\u id=66消失。所以Wordpress不知道如何将请求重定向到我的页面模板。

The solution:

该操作将指向主页并添加隐藏字段,该字段将保存page\\u id。

<form action="<?php echo home_url ?>"  method="get">
    <input type=\'hidden\' name=\'page_id\' value=\'66\'>
祝你好运。

SO网友:jberculo

如果将操作留空,表单将提交到同一页面。

所以只要这样做:<form action=\'\' method=\'get\'>";

SO网友:yunieski dieguez garcia

您可以使用这种方式,它更短,效果更好:

<form action="<?php echo get_bloginfo(\'home\').\'/?page_id=\'.get_the_ID();  ? >" method="get" >

SO网友:Marco Panichi

将表单发布到同一页面的另一种方法是使用php server variables:

echo "<form name=\'myForm\' action=\'http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI]."\' method=\'post\'>";
Using the_permalink() function 也可能会失败,因为您可能在一个循环中,并且\\u permalink()returns the permalink of the $post object 当前由循环本身处理。

结束

相关推荐

无法更改WP_LINK_PAGES之前的“Pages:”

下面是一个函数。wp\\u link\\u页面的php代码,允许显示分页和上一个/下一个链接。它还将每个分页链接包装在一个名为“classlinks”的span类中。我需要在分页之前更改默认的“Pages:”,同时维护其余代码。我的尝试取消了显示下一个/上一个链接和分页的功能。有什么帮助吗?// WP_LINK_PAGES: Add prev and next links to a numbered link list add_filter(\'wp_link_pages_args\', \'wp