WP帖子只包含一个url,如何把它变成一个链接


WP post contains only a url, how to turn it into a link?

所以我正在使用其中一个很酷的新WP模板,它带有编辑器和所有功能(Enfold,用于这个项目)。

我使用内置的enfold (avia布局生成器)组件来列出我网站上某个区域的博客文章。这些帖子只包含一个url,所以我想要完成的是保持使用内置组件列出的帖子(链接),但一旦用户点击一个帖子的标题,他们应该采取的帖子主体的url,而不是一个页面显示该url在纯文本。

我使用这个设置是为了让我的用户能够通过wordpress的"通过电子邮件发布"功能来创建这些帖子,否则我可能会使用链接插件来代替。

说到这个,我已经厌倦了一直更新一百万个插件,所以我不想为了这个目的而用插件来膨胀我的WP安装,我也不想为编写这样的插件而付钱。根据我的经验,WP插件也是一个主要的安全风险。这些都没有

我的解决方案是劫持列表中的所有链接点击。我创建了两个数组,其中包含我所有的文章标题和相应的url。

我首先在我的标签(在header.php)之前添加了这个脚本块:

<script>
//Newsredirecter
news_redirector_title = new Array();
news_redirector_content = new Array();
<?php 
global $post;
$tmp_post = $post;
$recent = new WP_Query("cat=34&showposts=3");
while($recent->have_posts()) : $recent->the_post();
        echo("
            news_redirector_content.push('".get_the_content()."');
            news_redirector_title.push('".get_the_title()."');
            ");
endwhile;
$post = $tmp_post;
?>

$recent = new WP_Query("cat=34&showposts=3");

告诉WP取出类别34的所有帖子,并且只取出其中的3个。

在enfold中,您可以为section添加ID,我就是这样做的。我把包含我的博客文章的部分称为"newsreplacer",通过下面的代码,我劫持了该部分中的所有链接点击,并使用上面数组中的数据重定向它们。

$( document ).ready( function(){
    //click event for the edit buttons
    $("#newsreplacer").on('click', 'a', function(event) {
        url = $(this).attr('href');
        current_title = $(this).html();
        //console.log('original link to: ' + url);
        console.log('clicked link title: '+current_title);
        for (i = 0; i < news_redirector_title.length; i++) { 
            if (news_redirector_title[i] == current_title) {
                event.preventDefault();
                window.location.href = news_redirector_content[i];
            }
        }
    });
} )
</script>

值得注意的是-我也有其他正常的博客文章列在同一部分,我不希望他们被劫持。这很好,因为最后一个for循环在数组中查找与所单击链接的标题相同的标题。