显示相似的帖子标题(类似于Stack Exchange)

时间:2011-12-29 作者:Dan

我真的很喜欢Stack Exchange站点处理帖子的方式,在您键入标题后,它会列出类似的标题。如何在WordPress安装上实现类似的功能?

原因是希望它能在顶部显示重复的标题,这样我就不会意外地发布一篇与旧文章标题相同的文章。

1 个回复
SO网友:5t3ph

这将为您提供大部分途径,但实际的搜索并不完美。It would be great if someone who knows more about $wpdb can help get a better result.

如果这是我的原始帖子标题:SOPA is dead, woot!

然后只要一个标题“SOPA”就会找到它,就像一个标题“已经死了”一样but not “叮咚,女巫死了”或“SOPA是个坏主意”。

但是,基本前提是在“检查标题”中添加一个按钮,按下时,标题将使用jQuery发布,并通过AJAX处理以返回匹配的标题,不包括当前帖子。如果您能够接受此内容并对其进行修改以返回更好的匹配项,请共享!

下面是基本插件代码,后面是jQuery,它应该包含在插件文件夹中一个名为duptitles的文件中。js公司

/*
Plugin Name: Duplicate Titles Check
Description: Prevent user\'s using similar post titles.
Version: 1.0
Author: 5t3ph
Author URI: http://stephscharf.me
*/

//jQuery to send AJAX request - only available on the post editing page
function dup_titles_enqueue_scripts( $hook ) {

    if( !in_array( $hook, array( \'post.php\', \'post-new.php\' ) ) )
        return;

    wp_enqueue_script( 
        \'duptitles\',
        plugins_url( \'/duptitles.js\', __FILE__ ),
        array( \'jquery\' )
    );
}
add_action( \'admin_enqueue_scripts\', \'dup_titles_enqueue_scripts\', 2000 );


// Invoke baked-in WP ajax goodness
// Codex: http://codex.wordpress.org/AJAX_in_Plugins
add_action(\'wp_ajax_title_check\', \'title_check_callback\');

function title_check_callback() {

    function title_check() {

        $title = $_POST[\'post_title\'];
        $post_id = $_POST[\'post_id\'];

        global $wpdb;

        $sim_titles = "SELECT post_title 
                    FROM $wpdb->posts 
                    WHERE post_status = \'publish\' AND post_type = \'post\' 
                    AND post_title LIKE \'%{$title}%\' 
                    AND ID != {$post_id}";

        $sim_results = $wpdb->get_results($sim_titles);

        if($sim_results) {
            $titles = \'<ul>\';
            foreach ( $sim_results as $the_title ) 
            {
                $titles .= \'<li>\'.$the_title->post_title.\'</li>\';
            }
            $titles .= \'</ul>\';

            return $titles;
        } else {
            return \'<p>This title is unique</p>\';
        }
    }

    echo title_check();

    die();
}
开始jQuery forduptitles.js

jQuery(document).ready(function($){
    // Post function
    function checkTitle(title, id) {
        var data = {
            action: \'title_check\',
            post_title: title,
            post_id: id
        };

        //var ajaxurl = \'wp-admin/admin-ajax.php\';
        $.post(ajaxurl, data, function(response) {
            $(\'#message\').remove();
            $(\'#poststuff\').prepend(\'<div id=\\"message\\" class=\\"updated fade\\"><p>\'+response+\'</p></div>\');
        }); 
    };

    // Add button to "Check Titles" below title field in post editor
    $(\'#edit-slug-box\').append(\'<span id="check-title-btn"><a class="button" href="#">Check Title</a></span>\');

    // Click function to initiate post function
    $(\'#check-title-btn a\').click(function() {
        var title = $(\'#title\').val();
        var id = $(\'#post_ID\').val();
        checkTitle(title, id);
    });

});

结束

相关推荐

如何在PRE_GET_POSTS过滤器内使用$Query->set(‘order’,‘asc’);更改查询顺序方向?

根据:http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters默认顺序为DESC,默认orderby为date。在自定义插件中,插件正在应用以下pre\\u get\\u posts过滤器:function custom_search_filter($query) { //echo \'<pre>\'; var_dump($_GET); echo \'</pr