DotDragnet
May 24, 2012, 07:12:31 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Mobile users - Our forum is Tapatalk enabled. http://www.tapatalk.com/
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Wordpress - display latest posts from category x if its page x or a subpage  (Read 812 times)
Matt
DDN Contribs
Hero Member
*****
Posts: 1710



View Profile WWW Awards
« on: September 15, 2011, 09:08:22 AM »

Like the title says basically!

In Wordpress we have pages and subpages which, on their sidebar, I want to include latest x posts from category x

I would like them to be display on a page with id of x and its subpages, and not on any other pages.

So For example, science department have a news category and a science page with subpages, on those pages I would like it to display the latest x posts from their news category.

I know I can create custom page templates, but I dont want to have staff selecting a template page for every page they are creating (we already have over 750 in a week)

Does anyone know the PHP? Way over my head, but Im sure its possible, maybe!?
Logged

sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #1 on: September 15, 2011, 11:46:11 AM »

You need a way to link the page ID/slug with the category ID/slug. You can easily get the page parent ID/slug for subpages (though if you have grandchildren pages or lower you will need to loop through until you get the top level page), so take that out of the mix.

Simply put you want to have

Code:
if (is_page('X') || $post->post_parent == X) :
$posts = get_posts('typical parameters here to retrieve from category Y');
foreach($posts AS $post) :
// as usual
endforeach;
endif;

If it's not easy to dynamically associate the page with the category then you are going to have to tell the page what category (if any) to display. You've got two options with this

1. Use a custom field called sidebar category, for example, and get the user to type in the category slug
2. Use a custom field but set it up with a bit of script in a meta box to display drop down selection of all the categories available, and then the user can just select one.

Then on the front end you can just check if the custom field has a value and if so, pull it out and display the posts in the category.

Personally I'd go for option 2 if you can't do the dynamic association. It's a bit more work but there's less room for error. It'd take a bit of scripting but once it's done it's the best solution really.
Logged

Matt
DDN Contribs
Hero Member
*****
Posts: 1710



View Profile WWW Awards
« Reply #2 on: September 19, 2011, 02:49:31 PM »

This is what I have come up with:

Code:
<?php if (is_page('3248') || $post->post_parent == 3248) :
$posts get_posts('numberposts=7&offset=0&category=130');
foreach(
$posts AS $post) : ?>

<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<? endforeach;
endif; ?>

Which appears on the right page, but it turns that page into the latest post?! It doesnt for example list the subpages of the current page which it should with this:

Code:
<?php
  $children 
wp_list_pages('title_li=&depth=1&child_of='.$post->ID.'&echo=0');
  if (
$children) { ?>

<div class="widget"><h3><?php _e('Also in this section''zbench'); ?></h3>
 <ul>
  <?php echo $children?>
  </ul>

Logged

sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #3 on: September 19, 2011, 03:59:50 PM »

Depends where you're running the code. If you're running it within the main page loop then it will overwrite the original loop and so your resulting content output can get messed up. Although if this is in the sidebar then surely it's in a separate file to your main loop?

Sounds to me like your main loop is getting overridden. If the get posts is within your main loop then you need to read up on multiple loops and get it to reset the query after you've done your post list output.
Logged

Matt
DDN Contribs
Hero Member
*****
Posts: 1710



View Profile WWW Awards
« Reply #4 on: September 19, 2011, 06:58:18 PM »

Depends where you're running the code. If you're running it within the main page loop then it will overwrite the original loop and so your resulting content output can get messed up. Although if this is in the sidebar then surely it's in a separate file to your main loop?

Sounds to me like your main loop is getting overridden. If the get posts is within your main loop then you need to read up on multiple loops and get it to reset the query after you've done your post list output.


It is in the separate sidebar.php - does that mean it shouldnt overwrite it? Ill look at that link thanks, looks likes thats what I need.

Im picking things up slowly! This system has caused me to try and do a lot more then ever before with wordpress, php and mysql. So its a challenge!
Logged

sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #5 on: September 19, 2011, 07:13:15 PM »

Right okay, second thought, is the sidebar loading before the main loop? If so then possibly using $post is overriding the main $post variable (it shouldn't in theory, but as it's set to global it could be).

So change your code to be

Code:
<?php if (is_page('3248') || $post->post_parent == 3248) :
$posts get_posts('numberposts=7&offset=0&category=130');
foreach(
$posts AS $mypost) : ?>

<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach;
endif; 
?>

Also note I've added 'php' to your opening tag on the last but one line. Whilst <? echo "blah" ?> will work, it's bad practice as it's then dependent on having short style tags on smile
Logged

Matt
DDN Contribs
Hero Member
*****
Posts: 1710



View Profile WWW Awards
« Reply #6 on: September 20, 2011, 07:47:48 AM »

Thanks that for Sarah, I even vagely understand what you are telling it to do. That leads me onto my next question. The output is per post if Im right? and the first bit says if its that page or a child display the output per post. So where abouts would I put my <h2> title - because I dont want it to repeat, and I only want it on the pages specified. Where would I break the PHP up to put it?

Like this? - And is it possible to grab the categories title I specified? Ive found a bit of code Ive put in, does it make sense?

Code:
<?php if (is_page('3248') || $post->post_parent == 3248) :
$posts get_posts('numberposts=7&offset=0&category=130'); ?>

<h2>

<?php
$cat 
2;
$category=get_category($cat);
// echo ""; print_r($category); echo "";
echo $category->cat_name;
?>
Newsfeed </h2>


<?php foreach($posts AS $mypost) : ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach;
endif; 
?>

Logged

Matt
DDN Contribs
Hero Member
*****
Posts: 1710



View Profile WWW Awards
« Reply #7 on: September 20, 2011, 07:58:42 AM »

Tried that, still does the same, the category name thing doesn't work either Sad Ill keep digging
Logged

sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #8 on: September 20, 2011, 12:48:58 PM »

For the category name

Code:
$cat = get_the_category('2');
$catname = $cat[0]->cat_name;

After your endforeach; for the post list try adding wp_reset_query();

If that doesn't work then you need to do some debugging. echo out the Post ID before you start this code ie. $post->ID and then echo out the post ID after the code, and do it again right before the loop for that page. Then you can see what it starts off as and if/when it changes
Logged

Matt
DDN Contribs
Hero Member
*****
Posts: 1710



View Profile WWW Awards
« Reply #9 on: October 03, 2011, 09:18:00 AM »

I had an idea for perhaps an easier way to do this - how about saying if its page x then include a particular php file? That php file could then call the latest x posts from the relevant category??
Logged

sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #10 on: October 03, 2011, 07:04:28 PM »

You're doing nothing different except putting the get posts code in a different file. What did my correction above give you? If it still didn't work can you supply all of your code again and I'll test it on my test site.
Logged

Matt
DDN Contribs
Hero Member
*****
Posts: 1710



View Profile WWW Awards
« Reply #11 on: October 06, 2011, 12:24:19 PM »

I placed the wp_reset in there, and it didnt make any difference - when you visit the page id, the first bit must be working, but it displays the first post content instead of the page content.

Could this be because the Sidebar is included before the content?
Logged

sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #12 on: October 06, 2011, 04:45:17 PM »

Well, it is because of that but you're not overriding the post ID so it shouldn't be an issue. I need to see all of your code ideally to be able to try it myself, either on here or if you can send me a link to where it is plus a login to view the PHP code then I can maybe get it working for you.

It's hard guessing without knowing what you're fully working with smile
Logged

Matt
DDN Contribs
Hero Member
*****
Posts: 1710



View Profile WWW Awards
« Reply #13 on: October 06, 2011, 07:35:46 PM »

Well, it is because of that but you're not overriding the post ID so it shouldn't be an issue. I need to see all of your code ideally to be able to try it myself, either on here or if you can send me a link to where it is plus a login to view the PHP code then I can maybe get it working for you.

It's hard guessing without knowing what you're fully working with smile

I've emailed you smile
Logged

Whatever
Hero Member
*****
Posts: 750



View Profile WWW Awards
« Reply #14 on: October 07, 2011, 08:05:06 PM »

Very interested in this as looking to do the same thing shortly.
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #15 on: October 07, 2011, 08:54:15 PM »

Well get_posts() wasn't playing ball and as I didn't have the time to work out why I just altered it to query_posts instead. here's an example of working code if you need it smile

Code:
$cat_to_get = X;
query_posts('post_type=post&cat='.$cat_to_get.'&posts_per_page=5');
if (have_posts()) :
$cat = get_category($cat_to_get);
echo "<h3>Category: ".$cat->name."</h3>\n";
echo "<ul>\n";
while (have_posts()) :
the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile;
wp_reset_query();
echo "</ul>\n";
endif;

I don't know why get_posts() didn't want to work on the sidebar. The order shouldn't have any bearing but maybe it does, however the above will work within or outside the loop smile
Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!