【问题标题】:Wordpress - Posts and PagesWordpress - 帖子和页面
【发布时间】:2013-05-26 23:44:07
【问题描述】:

我目前正在开发一个多站点 wordpress 设置,每个站点都是不同的语言(例如:site.com、site.dk 等)。

这些网站由许多包含静态内容的页面组成,但是我还想在这两个网站中都包含帖子(博客)。

第一季度。是否可以创建一个页面来显示按最新发布日期列出的所有帖子,并带有按类别过滤的下拉列表?我该怎么做呢?需要参考loop.php吗?

基本上它应该为所有帖子返回以下代码...

<article class="post">
<a href="<URL Link to Post Article>" rel="bookmark">
<figure>
<img title="<Post Title>" alt="<Post Title>" src="<http://url/PostImage.jpg>" width="900" height="600" />
</figure>               
<div class="cover">
<h2>Post Title</h2>
<time pubdate="2013-03-27T21:09:59+00:00">November 18, 2012</time>
</div>
</a>
</article>

为什么这不起作用?它什么都不返回?

<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<!-- article -->
<article class="post">
<?php get_posts(); ?>
<div id="grid-switcher">
<a href="#" data-block="featured-posts" id="featured">featured</a>
<a href="#" data-block="latest-posts" id="latest" class="active">latest</a>
</div>
<div id="view-blocks">
<div id="latest-post" class="post-grid active">
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail(array(250,250)); // Declare pixel size you need inside the array ?>
<?php endif; ?>
<!-- /post thumbnail -->
<div class="cover">
<h2><?php the_title(); ?></h2>
<time pubdate="<?php the_date(); ?>"><?php the_date('Y-m-d', '<h2>', '</h2>'); ?></time>
</div>
</a>

我已经为上面创建了一个页面模板portfolio-page.php,但是不知道如何循环浏览帖子并使用上面的代码返回它们?

第二季度。如何返回包含所有类别的下拉列表?

第三季度。如何从下拉列表中按类别筛选?

感谢您的帮助! :)

对不起,我是 PHP 和 wordpress 的新手...

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    这个问题有点模糊,所以我将分享一些起点:

    A1) 要获取帖子列表,您可以使用 wordpress 中的 get_posts 函数。它已经按发布日期 desc 对它们进行了排序,如果选择了一个,您还可以为该类别添加一个参数。

    A2) 要获得类别下拉列表,有一个函数可以做到这一点:wp_dropdown_categories

    查看文档,因为有很多参数可以与 get_posts 函数一起使用,因此您应该能够根据需要过滤它们。

    【讨论】:

    • @myatix 您正在循环中使用 get_posts,我不知道该页面上是否存在..请查看该功能的手册页(答案中有链接)..您将看到他们在返回值上使用 foreach 语句而不是作为“循环”的一部分来迭代结果
    • 即使我使用“带有偏移的帖子列表”示例,它也不起作用?我有一个页面模板的代码......它需要在loop.php文件中还是我可以直接在页面模板中引用它?抱歉,我说我对此很陌生...
    • 好的...我将 ID 设置为错误的类别...现在它只显示一个帖子,即使有 3 个帖子属于该类别???
    【解决方案2】:

    我认为检查运行 wordpress 的数据库是个好主意。

    如果您检查 wp_posts 表,您可以看到可以过滤的不同字段。

    您最好使用自定义查询来过滤帖子并对其进行排序

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-20
      • 1970-01-01
      相关资源
      最近更新 更多