如何在页面的“帖子”标题中切换帖子内容?

时间:2014-02-21 15:01:28

标签: javascript php wordpress toggle posts

我正在尝试在特定页面中提供所有“帖子”标题。因此,如果有人点击了这些帖子,则必须在标题下方切换内容,如果有人再次点击标题,则内容必须隐藏。

我在WP-Archives插件的帮助下解决了一半的问题。我的页面看起来像这样Check this image here ..所以这些都是 “档案”标题链接。如果有人点击它,它将采取那个..我希望特定帖子中的内容以切换的形式在同一页面中(在abvoe图像..)。有可能吗?

3 个答案:

答案 0 :(得分:2)

听起来你在描述手风琴的功能。 Wordpress提供了一堆手风琴插件。以下是一些: http://wordpress.org/plugins/tags/accordion

如果您正在寻找具有更多控制权的东西,您可能希望直视jquery手风琴小部件。请参阅此处的信息:http://jqueryui.com/accordion/

修改

要将accordion插件添加到列表中,您需要应用以下更改。将以下内容添加到您的头标记中:

<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

在您的wp-archives插件中修改以下行:

echo "<div class='list'><ul>\n";

到此:

echo "<div class='list'><ul id=\"unique_id_of_your_choice\">\n";

对于每个帖子标题后面的内容,您需要再次更新插件以包含存档帖子内容(整个帖子或片段,无论您选择哪个)。如果包含内容的数据库字段名为“post_content”,它应该看起来像这样。

$arcresults2 = $wpdb->get_results("SELECT ID, post_date, post_title, post_content, comment_status FROM " . $wpdb-> posts . " WHERE post_date LIKE '$thisyear-$thismonth-%' AND $current_posts AND post_status='publish' AND post_password='' ORDER BY post_date DESC");
...

...
$arc_title = $arcresult2->post_title;
$arc_content = $arcresult2->post_content;
...

...
echo "<li class='list'><a href=\"" . $url . "\" title=\"" . $title_text . "\">" . wptexturize($text) .  "</a>\n";
echo"<ul><li>".$arc_content."</li></ul></li>\n";

答案 1 :(得分:0)

这可以通过jQueryUI Accordion之类的手风琴脚本实现,但如果您要将其应用到帖子页面,则还需要编辑页面模板。

这是一个简单的方法(见source):


1)将以下内容添加到主题的functions.php

function add_accordion_script() {
  wp_enqueue_script('acc', get_template_directory_uri() . '/acc.js', array('jquery-ui-accordion'));
}
add_action('wp_enqueue_scripts', 'add_accordion_script');

2)在主题目录中创建acc.js:

jQuery(document).ready(function($) {
    $( "#accordion" ).accordion({
            header: "div.accordion-header",
            collapsible: true,
            active:false });
});

3)最后将主题页面修改为:

<?php if (have_posts()): ?>
    <div id="accordion">
    <?php while (have_posts()) : the_post();?>
        <div class="accordion-header">
            <h1><?php the_title();?></h1>
            <?php the_excerpt();?>
        </div>
    <div><?php the_content();?></div>
    <?php endwhile; ?>
    </div>
<?php else:?>
    <p><?php _e('No posts'); ?></p>
<?php endif;?>

根据您的wordpress模板,这可能比上面概述的更复杂......在这种情况下,您必须提供更多信息。

另一种选择是使用专门为此目的设计的某种插件。我找到了三个可能对你有用的东西,但它们似乎没有得到很好的支持/维护......所以上述方法可能仍然是你最好的选择:

  1. Wordpress jQuery Accordion Plugin
  2. WP Post Accordion
  3. Read More Right Here Plugin

答案 2 :(得分:0)

wordpress的

Collapse-O-Matic Plugin是另一种选择,它的工作就像我的网站的魅力,但只有当我将其更改为Wordpress的默认主题时:(。似乎我的主题中有任何故障。无论如何,感谢上面发布的所有回复。