【问题标题】:list wordpress post titles from one category in multiple columns in alphabetical order按字母顺序在多列中列出一个类别的 wordpress 帖子标题
【发布时间】:2013-08-07 18:45:16
【问题描述】:

我正在尝试建立一种客户项目的存档,显示在每个页面的底部,就像在此处的“体验”部分中所做的那样:http://toth.com/#experience - 除了在我的情况下我只需要完整的项目列表,而不是小标题或任何其他结构。

我进行了一些设置,以便我客户的每个项目都是一个帖子。所以我需要一种方法来显示帖子的标题,从我创建的“工作档案”类别中(以便客户可以通过选中/取消选中每个帖子中的类别框来轻松地从档案中添加和删除内容) ,按字母垂直顺序,跨四列自动调整大小以平均填充容器。显然,存档中的每个帖子标题也需要链接到该帖子。

我已经为此在网上搜索了几天,虽然我发现了一些看起来很有帮助的代码,但似乎不可能(以我有限的 PHP 知识)集成它们来满足我的所有要求.我还研究了许多 WordPress 插件,但仍然没有成功。虽然我会接受任何解决方案,但理想情况下,我宁愿在 PHP/模板级别解决问题,以尽可能对客户端后端隐藏。

非常感谢您对此的任何帮助。

【问题讨论】:

标签: wordpress categories posts alphabetical


【解决方案1】:

听起来最好的方法可能是设置一个新的 WP Query 对象。更多信息在这里: http://codex.wordpress.org/Class_Reference/WP_Query

<?php

$args = 'category_name=work-archive&orderby=title&order=asc&posts_per_page=9999';
// assuming 'work-archive' is the slug to your category, we are also doing ascending order by title (a,b,c,d), and pulling 9999 posts (hopefully that is more than the number of posts you have!)

// The Query
$query = new WP_Query( $args );

// Keeping track of the count
$count = 0;

// Number of items per column
$num_per_column = round($query->post_count / 4); // dividing total by columns

// The Loop
if ( $query->have_posts() ) : ?>
    <ul>
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>

        <?php if ( $count % $num_per_column == 0 ) : // If the current count is up to it's limit throw in a new column ?>
    </ul>
    <ul>
        <?php endif; ?> 

        <li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>

        <?php $count++; // Increment counter ?>

    <?php endwhile; ?>
    </ul>
<?php endif; 

/* Restore original Post Data */
wp_reset_postdata();

?>

用一些 CSS 完成它!

【讨论】:

  • 非常感谢。那里似乎有90%。它只是不创建超过一列。关于它的其他一切都很好!恐怕我不知道如何解决列问题..?还是像在 css 中设置一些浮点数一样简单?如果是这样,我的目标是什么?
  • 请其他人对此有所了解?
  • 拉尔夫,我认为问题出在您评论“如果当前计数达到其限制...”的那一行。我认为您实际上并没有编写添加代码一个新的专栏,你刚刚写了评论。非常感谢任何帮助。
  • 康拉德,你能给我发个链接吗?我不确定是否可以在没有看到输出的情况下进行诊断。
  • 在本地主机上开发,所以上传网站和数据库等需要做一些工作,但是从查看源代码我可以看到它只是输出一个单一的
      帖子标题作为它们之间的列表项。即它没有创建一个新的
        ,根据我从你的代码中了解到的应该是......?
    【解决方案2】:

    要修复打开的 ul,请更改此条件:

    if ( $count % $num_per_column == 0 && $count != 0)
    

    它会阻止 ul 在第一次通过时关闭

    【讨论】:

    • 这似乎并没有真正回答 OP 的问题
    • 不,它只是修复了打开的 ul 结果
    猜你喜欢
    • 2014-03-11
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多