oli_collins
Member
Posted 1 year ago #
Im using the following code to show some of the latest posts that are in all categories apart from category 3. $latestPost=get_posts('numberposts=3&cat=-3'); if i have cat=-3 in the query then it just shows posts from all categories, if i use category=-3 then no posts show, if i use &cat=4,5 then posts in all categories show and if i use category=4,5 it just shows the post that is in category 4
With get_posts you have to use "category". Can you show us the whole foreach you are using to display the posts.
oli_collins
Member
Posted 1 year ago #
<? $latestPost=get_posts('numberposts=2&category=3');
foreach($latestPost as $pst){?>
<? $contentAge=$pst->post_content; $contentAg=substr($contentAge,0,57); ?>
<? $custom_fields = get_post_custom($pst->ID); $post_category = get_the_category($pst->ID); ?>
<li><a href="<?php bloginfo('url'); ?>/<?=$post_category[0]->category_nicename;?>/<?=$pst->post_name;?>/"><?=$pst->post_title?></a><br /><?=$contentAg?>...<br /><div class="more"><a href="<?php bloginfo('url'); ?>/<?=$post_category[0]->category_nicename;?>/<?=$pst->post_name;?>/">More...</a></div></li>
<? } ?>
As per my initial post I have tried using category but there were still problems with it
Have you tried with setup_postdata. Like so:
<? $latestPost=get_posts('numberposts=2&category=3');
foreach($latestPost as $pst){
setup_postdata($pst);
?>
<? $contentAge=$pst->post_content; $contentAg=substr($contentAge,0,57); ?>
<? $custom_fields = get_post_custom($pst->ID); $post_category = get_the_category($pst->ID); ?>
<li><a href="<?php bloginfo('url'); ?>/<?=$post_category[0]->category_nicename;?>/<?=$pst->post_name;?>/"><?=$pst->post_title?></a><br /><?=$contentAg?>...<br /><div class="more"><a href="<?php bloginfo('url'); ?>/<?=$post_category[0]->category_nicename;?>/<?=$pst->post_name;?>/">More...</a></div></li>
<? } ?>
oli_collins
Member
Posted 1 year ago #
ok my code is now shown below and it shows no posts but its not relatest the setup_postdata($pst); line as it was like that before i just put that in:
<? $latestPost=get_posts('numberposts=3&category=-3');
foreach($latestPost as $pst){
setup_postdata($pst);?>
<? $custom_fields = get_post_custom($pst->ID); /*print_r($custom_fields);*/ $post_category = get_the_category($pst->ID); ?>
<li><a href="<?php bloginfo('url'); ?>/<?=$post_category[0]->category_nicename;?>/<?=$pst->post_name;?>/"><span><?=$pst->post_title?></span><br /><?php echo $custom_fields['Location'][0]; ?><? if($custom_fields['Salary'][0]!=''){?><br /><span><? echo $custom_fields['Salary'][0]; ?></span><? } ?></a></li>
<? } ?>