好吧,让我直截了当地说,我正在尝试使用Wp favorites posts插件显示特定类别这就是我添加的内容$category_id = get_cat_ID(\'news\');
但它似乎不起作用,它显示了最喜欢的帖子上的所有类别,而不是某个特定类别。
这就是我搞砸的,我把我的分类
<?php
foreach ( $favorite_post_ids as $o ) :
$category_id = get_cat_ID( \'news\' );
$p = get_post( $o );
if ( $p->post_status == \'publish\' ) {?>
&;这是整个最受欢迎的帖子框,使其发挥作用。
<div class="profile-box items-following">
<h3><?php _e( \'fav posts\', \'bd_lang\' ); ?></h3>
<div class="profile-content">
<?php $favorite_post_ids = wpfp_get_users_favorites();
if ( $favorite_post_ids ) { ?>
<?php foreach ( $favorite_post_ids as $o ) : $p = get_post( $o );
if ( $p->post_status == \'publish\' ) { ?>
<div class="profile-item">
<!--Begin Image-->
<?php if ( has_post_thumbnail( $o ) ) { ?>
<div class="post-thumbnail">
<a href="<?php get_permalink( $o ); ?>" title="<?php echo $p->post_title; ?>">
<?php $image = vt_resize( get_post_thumbnail_id( $o ), \'\', 50, 0, true ); ?>
<img src="<?php echo $image[url]; ?>" width="<?php echo $image[width]; ?>" height="<?php echo $image[height]; ?>" alt="<?php if ( get_post_meta( get_post_thumbnail_id( $o ), \'_wp_attachment_image_alt\', true ) ) {
echo get_post_meta( get_post_thumbnail_id( $o ), \'_wp_attachment_image_alt\', true );
} else {
echo $p->post_title;
} ?>" />
</a>
</div>
<?php } ?>
<!--End Image-->
<a href="<?php echo get_permalink( $o ); ?>" title="<?php echo $p->post_title; ?>"><?php echo $p->post_title; ?></a>
</div>
<?php } endforeach; ?>
<?php } else { ?>
<div class="profile-item">
<strong><?php _e( \'Your are not currently following any items.\', \'bd_lang\' ); ?></strong>
</div>
<?php } ?>
</div>
</div>
最合适的回答,由SO网友:tfrommen 整理而成
这应该满足您的需要:
$have_favorite_posts = false;
$favorite_post_ids = wpfp_get_users_favorites();
$cat = get_category_by_slug(\'news\')->cat_ID;
// or if you want to use the category name (as tried in your code)
// $cat = get_cat_ID(\'News\');
if (! empty($favorite_post_ids)) {
$fp_query = new WP_Query(array(
\'cat\' => $cat,
\'post__in\' => $favorite_post_ids,
));
if ($fp_query->have_posts()) {
$have_favorite_posts = true;
while ($fp_query->have_posts()) : $fp_query->the_post();
if (\'publish\' === get_post_status(get_the_ID())) { ?>
<div class="profile-item">
<!--Begin Image-->
<?php if (has_post_thumbnail()) { ?>
<div class="post-thumbnail">
<a href="<?php echo get_permalink(); ?>" title="<?php the_title(); ?>">
<?php
$image = vt_resize(get_post_thumbnail_id(), \'\', 50, 0, true);
$url = $image[url];
width = $image[width];
$height = $image[height];
$alt = get_post_meta(get_post_thumbnail_id(), \'_wp_attachment_image_alt\', true);
if (! $alt) $alt = get_the_title();
?>
<img src="<?php echo $url; ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>" alt="<?php echo $alt; ?>" />
</a>
</div>
<?php } ?>
<!--End Image-->
<a href="<?php echo get_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</div>
}
endwhile;
}
}
if (! $have_favorite_posts) { ?>
<div class="profile-item">
<strong><?php _e(\'You are not currently following any items in this category.\', \'bd_lang\'); ?></strong>
</div>
<?php } ?>
// EDIT: 现在,如果有最喜欢的帖子,但不在给定的类别中,您还会看到下面的消息。