根据与当前所选值匹配的标题显示自定义帖子类型

时间:2011-12-13 作者:remi90

好吧,这是一件非常复杂的事情,我有点不确定如何使用wordpress,但我希望有一些天才可以帮助我。

我基本上是在创建一些比较功能,目前页面上有两个选择框。这些选择框将显示自定义帖子类型中所有帖子的列表\'countries\' 在它们内部,当用户从列表中选择一个选项时,我将使用ajax显示下面特定帖子的内容。

我目前有一个非wordpress版本的我正在尝试做的事情,下面是代码。

HTML

<select id="compare-one">
<option value="United Kingdom">United Kingdom</option>
<option value="Belarus">Belarus</option>
<option value="Albania">Albania</option>

Jquery

$(\'#compare-one\').change(function(e){
$(\'#compare-one-container\').empty();
$(this).find(\':selected\').each(function(i,e){
    $.get("callbacks.php",
      // used to prevent caching in IE
      { nbRandom: Math.random() },
      function(data){
        $("#compare-one-container").html(data);
     });  
});
});

callbacks.php

<?php $test = "Home" ?>
正如你所看到的,我还没有在Wordpress中实现这一点,只是因为我不确定这样做的正确方式!

I think my main question is how to replace this current php code with something that checks to see if the current selected value is equal to the page title, and if so echo the information (content, custom fields etc) from that particular page title it matches.

在此方面的任何帮助都将不胜感激!

谢谢

1 个回复
最合适的回答,由SO网友:nonsensecreativity 整理而成

没有真正了解你的主要问题。。您是否尝试比较两种帖子类型并将其显示在页面模板中?或者您是否尝试将当前页面与选定的帖子类型进行比较?

尝试创建页面模板

<?php
/*
 * Template Name: Compare
 */
?>

<?php
get_header();   
?>


        <form id="customsearch" action="" method="post">
            <select name="compare-one" id="compare-one">
                <option value="130">1</option>
                <option value="102">2</option>
            </select>

            <select name="compare-two" id="compare-two">
                <option value="130">1</option>
                <option value="102">2</option>
            </select>

</form>
<script type="text/javascript">
        $(document).ready(function() {  
            $(\'#compare-one\').change(function() {
                var compareone_value = $(\'#compare-one\').val();
                var data = {
                    \'action\':\'callback_function\',
                    \'compare-one\':compareone_value, 
                };
                $.post(ajax_object.ajaxurl, data, function(response) {
                    $(\'#compare-one-container\').html(response); 
                });
            });

            $(\'#compare-two\').change(function() {
                var comparetwo_value = $(\'#compare-two\').val();
                var data = {
                    \'action\':\'callback_function\',
                    \'compare-two\':comparetwo_value, 
                };
                $.post(ajax_object.ajaxurl, data, function(response) {
                    $(\'#compare-two-container\').html(response); 
                });
            });

        });
</script>              
<div id="compare-container">
    <div id="compare-one-container">

    </div>

    <div id="compare-two-container">

    </div>

</div>

<?php get_footer(); ?>
接下来,向函数中添加回调函数。php

add_action(\'wp_ajax_callback_function\', \'callback_function\');
add_action(\'wp_ajax_nopriv_callback_function\', \'callback_function\'); // for not logged in user
function callback_function() {
if (isset($_POST[\'compare-one\']) || isset($_POST[\'compare-two\'])) {
            $arr_id = array($_POST[\'compare-one\'],$_POST[\'compare-two\']);
            $temp = $wp_query;
            $wp_query = null;
            $wp_query = new WP_Query();

            $wp_query->query(array(\'post_type\' => \'listing\', \'posts_per_page\' => 2, \'post__in\' => $arr_id));
            while ($wp_query->have_posts()) : $wp_query->the_post();
                $custom = get_post_custom($post->ID);
                $beds = $custom[\'_beds\'][0];
                $baths = $custom[\'_baths\'][0];
                apply_filters(\'the_title\', the_title());
                apply_filters(\'the_content\', the_content());
            endwhile;
            $wp_query = null; $wp_query = $temp;
            rewind_posts();

    }
    die();
}
?>
别忘了本地化WordPress ajax脚本,以便在首页中使用

wp_enqueue_script( \'ajax-script\', \'ajax_object\', array(\'jquery\'));
    wp_localize_script( \'ajax-script\', \'ajax_object\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) ); // setting 

结束