在Wordpress网站上,如果用户正在查看2014年1月2日发布的帖子,我想在侧边栏中显示早于01/02/2014的帖子。是否有任何插件可以执行此操作或任何代码完成这件事?任何帮助将不胜感激。谢谢你。
答案 0 :(得分:2)
此代码生成特定日期之前的帖子。获取当前帖子的日期并替换为代码中的日期“2012-03-01” $ query_string =“order = ASC& posts_per_page = -1”;
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
$where .= " AND post_date > '2012-03-01'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$custom_query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
答案 1 :(得分:1)
我认为在SQL中,查询将如下所示:
SELECT ID,post_title FROM `wp_posts` WHERE `post_date` < "2014-02-01 00:00:00" AND `post_type` = "post" AND `post_status` = "publish" order by `post_date` desc
这意味着:在the post id
表格中选择the post title
,the post date
和wp_posts
,其中the post_date
早于2014-02-01 00:00:00
且帖子为{ {1}}帖子是published
(不是菜单项或其他内容)。
在php中,你可以这样做:
a post
它会回应:
<div class="test">
<?php
global $wpdb;
$query = "
SELECT ID, post_title,post_date
FROM $wpdb->posts
WHERE post_date < '2013-12-01 00:00:00'
AND post_status = 'publish'
AND post_type = 'post'
ORDER BY post_date DESC
";
$results = $wpdb->get_results( $query );
// Open an unordered list
echo '<ul>';
foreach ( $results as $result )
{
echo '<li>';
echo '<span>'.$result->ID.'</span>'; // Your post ID
echo '<time datetime="'.$result->post_date.'">'.$result->post_date.'</time>'; // The post date
echo '<span>'.$result->post_title.'</span>'; // the post title
echo '</li>'; // Close your list item
}
echo '</ul>'; // Close your unordered list
?>
</div>
当然,您可以在帖子标题上添加<ul>
<li>
<span>a post ID</span>
<time datetime="a post date">a post date</time>
<span>a post title</span>
</li>
<li>
<span>another post ID</span>
<time datetime="another post date">another post date</time>
<span>another post title</span>
</li>
...
</ul>
并删除帖子ID或发布日期。
我不知道这是否是更好的方法,但它工作正常:)
哦,如果你想在侧边栏中回显结果,你应该创建一个新的小部件:)