在互联网上漂浮是一段PHP代码,旨在显示WordPress中最近更新的帖子/页面。在向专家提交请求之后,我很快就得到了我需要的原始代码。
然而,原始的PHP代码仍然不能用于我的WordPress安装。经过一些调整和许多额外的花里胡哨之后,我设法拼凑了仅显示最近更新的帖子所需的代码。
此时我请求帮助仅显示特定类别的帖子。是否可以仅显示特定类别的帖子?你能帮忙吗?
这是当前的代码:
<div class="statistics">
<?php
$today = current_time('mysql', 1);
$howMany = 10; //Number of posts you want to display
if ($recentposts = $wpdb->get_results("SELECT ID, post_title, post_modified FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND post_name NOT LIKE '%revision%' AND post_name NOT LIKE '%autosave%' AND post_modified_gmt < '$today' ORDER BY post_modified_gmt DESC LIMIT $howMany")) :
?>
<h2><?php _e('ULTIME SERIE TV AGGIORNATE'); ?></h2>
<ul>
<?php
foreach($recentposts as $post) {
if ($post->post_title == '') {
$post->post_title = sprintf(__('Post #%s'), $post->ID);
}
/* If no post title exists one will be assigned to it. */
echo "<li><a href='".get_permalink($post->ID)."'>";
echo mysql2date('d/m/Y', $post->post_modified);
echo " - ";
echo $post->post_title;
echo '</a></li>';
}
?>
</ul>
<?php endif; ?>
</div>
该表是存储在 wp_term_taxonomy 中的类别,该方法用于存储特定类别数据库下的项目及其 ID ( term_taxonomy_id )这是一张图片:
在插件(recently-updated-posts)中我发现了这个功能,但我不知道怎么把它放在我的代码中......
if ($options['excludeCategory']) {
$select .= ", GROUP_CONCAT(`tt`.`term_id`) AS `terms`";
$from .= " LEFT JOIN `{$wpdb->term_relationships}` AS `tr` ON `tr`.`object_id` = `p`.`ID`"
. " LEFT JOIN `{$wpdb->term_taxonomy}` AS `tt` ON `tt`.`term_taxonomy_id` = `tr`.`term_taxonomy_id`";
$where .= " AND `tt`.`taxonomy` = 'category'"
. " AND `tt`.`term_id` NOT IN ({$options['excludeCategory']})";
$group = "GROUP BY `ID`";
}
答案 0 :(得分:1)
我会使用wp_get_recent_posts并传入类别的参数。
<ul>
<?php
$args = array( 'category' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $post ){
if ($post["post_title"] == '') {
$post["post_title"] = sprintf(__('Post #%s'), $post["ID"]);
}
?>
<li><a href='<?php echo get_permalink($post["ID"]) ?>'>
<?php echo mysql2date('d/m/Y', $post["post_modified"])?>
-
<?php echo $post["post_title"]?>
</a></li>
}
?>
</ul>
答案 1 :(得分:0)
最终代码:
<div class="update">
<?php
$today = current_time('mysql', 1);
if ($recentposts = $wpdb->get_results("SELECT ID, post_title, post_modified FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' AND post_name NOT LIKE '%revision%' AND post_name NOT LIKE '%autosave%' AND post_modified_gmt < '$today' ORDER BY post_modified_gmt")) :
?>
<h2><?php _e('GLI ULTIMI EPISODI AGGIUNTI'); ?></h2>
<ul>
<?php $args = array('category' => '30', 'orderby' => 'modified', 'post_status' => 'publish', 'posts_per_page' => 11);
$recent_posts = wp_get_recent_posts($args);
foreach ($recent_posts as $post) {
if ($post["post_title"] == ''){ $post["post_title"] = sprintf(__('Post #%s'), $post["ID"]);
}
?>
<li><?php echo mysql2date('d/m/Y', $post["post_modified"]) ?>
-
<a href='<?php echo get_permalink($post["ID"]) ?>'>
<?php echo $post["post_title"] ?>
</a></li> <?php } ?>
</ul>
</div>
<?php endif; ?>
答案 2 :(得分:0)
此代码是获取最近更新的帖子和页面的另一种方法。
<?php
$today = current_time('mysql', 1);
$number = 5; // number of posts
if($recentposts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_modified_gmt < '$today' ORDER BY post_modified_gmt DESC LIMIT $number")):
?>
<h2><?php _e("Recently Updated"); ?></h2>
<ul>
<?php
foreach($recentposts as $post) {
if($post->post_title == '') $post->post_title = sprintf(__('Post #%s'), $post->ID);
echo '<li><a href="'.get_permalink($post->ID).'">'.the_title().'</a></li>';
} ?>
</ul>
<?php endif; ?>