ThemeShaper Forums » Thematic

[closed]

Listing subpages on parent - as excerpts?

(17 posts)
  • Started 2 years ago by vicky515
  • Latest reply from helgatheviking
  • This topic is resolved
  1. vicky515
    Member

    Ready for a challenge? :)

    I'd like to list subpages in my parent pages as excerpts.
    So, the title of the subpage, the featured image thumbnail, and the first couple lines of text. I found a few plugins to list subpages but they only provide a list of cold links - no excerpt or thumbnails.

    Any help would be appreciated.

    Posted 2 years ago #
  2. that's actually more of a WordPress question than a thematic question... and also the sort of "i need someone to do this for me" type of question that you really shouldn't expect free support for. i don't mean any offense. and if you come up w/ some code that you think is close, but isn't quite working i'd be happy to take a look.

    the plugins you've found must do a query and get the child pages. then they undoubtedly go through a loop. you could mod the plugin to include the_excerpt() in said loop.

    OR take a look at the Codex:
    http://codex.wordpress.org/Function_Reference/get_pages

    so much info in the codex. right in the middle of the page is an example loop for showing pages that are children of the current page. you'll just need to mod the content appropriately for the excerpt.

    Posted 2 years ago #
  3. vicky515
    Member

    That wasn't my intention... Rather than write 10 paragraphs of how I think it might be done that I'd update a few times when I got different ideas, I thought it'd be easier to just make a clear statement of what I'd like and compare what I was thinking with the solution that was presented and ask questions. I've seen other people do it. I think Thematic was a bad way to get introduced to Wordpress Themes because I get confused where Wordpress ends and Thematic starts... It's a little overwhelming. So I apologize if it seemed like I was being lazy or something. That is not the case.

    The loop I currently have (seen below) uses wp_list_pages, so I looked at the codex and didn't see any functionality for getting excerpts. I'm also not sure if I even have it in the best spot. I knew it was supposed to go in the loop, so I tried random places until it worked.

    I ended up putting it after

    edit_post_link(__('Edit', 'thematic'),'<span class="edit-link">','</span>') ?>

    in the page.php file.

    No I'm not sure how to connect functions, I guess.

    <?php
    $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
    if ($children) { ?>
    <h4>Related Articles</h4>
    <ul>
    <?php echo $children; ?>
    </ul>
    <?php } ?>

    Having said all that, I think the get_pages you provided is far more useful. I will look into how to get the excerpt.

    Thank you, and I'm sorry again.

    Posted 2 years ago #
  4. soneal
    Member

    Have a look at this article.

    http://www.wpbeginner.com/wp-themes/how-to-add-related-posts-with-a-thumbnail-without-using-plugins/

    This can at least get you moving in the right direction.

    Posted 2 years ago #
  5. hey no problem, though it does help sometimes to see what you've tried and to know what is failing and how it is failing. i don't see it too often here, but i just like to make sure that i am helping someone learn and not doing the work for them.

    but the link i posted should get you started. take what they have and hook it in somewhere with a function

    function child_pages(){
    global $post;
    
    $mypages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
    
    	foreach($mypages as $page)
    	{
    		$content = $page->post_content;
    		if(!$content) // Check for empty page
    			continue;
    
    		$content = apply_filters('the_content', $content);
    	?>
    		<h2><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></h2>
    		<div class="entry"><?php echo $content ?></div>
    	<?php
    	}
    }
    add_action('thematic_abovemainasides','child_pages');

    i didn't test this, now it should be just a matter of adjusting the $content from:
    $content = $page->post_content;

    to whatever you'd like. and adding whatever markup you want too.

    Posted 2 years ago #
  6. vicky515
    Member

    Alright, rock and roll... While trying to figure this out I think I got a bit closer to better understanding filters. Although it doesn't work, so maybe not!

    So here is my modified version for attempting to accomplish this (thanks Helga!).

    function child_pages(){
    global $post;
    
    $mypages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
    
    	foreach($mypages as $page)
    	{
    		$content = $page->post_content;
    		if(!$content) // Check for empty page
    			continue;
    
    		$content = apply_filters('the_excerpt',$content);
    	?>
    		<?php the_post_thumbnail(); ?><h2><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></h2>
    		<div class="entry"><?php echo $content ?></div>
    	<?php
    	}
    }
    add_action('thematic_belowpost','child_pages');

    I made a lot of revisions but what I have shown here is what makes the most sense to me. But neither of the two lines that I changed work lol... So rather than post all the possible variations I tried, I'm just going to ask for help with this one.

    First one, attempting to apply the_excerpt filter to $content - it is still showing the post in it's entirety. According to what I'm reading in the Codex, this should work. The only reason it may not be working, I'm thinking, is if my pages don't contain the required <!--more--> tag. Could that be the case? Or is my logic incorrect?

    Second one, adding the_post_thumbnail before the title (from the article soneal posted). Reading the codex my only thought was that maybe my theme didn't support it?
    http://codex.wordpress.org/Post_Thumbnails

    But then I found this in Thematic's functions.php so it must be ok, right?

    // Check for WordPress 2.9 add_theme_support()
    if ( apply_filters( 'thematic_post_thumbs', TRUE) ) {
    	if ( function_exists( 'add_theme_support' ) )
    	add_theme_support( 'post-thumbnails' );
    }
    Posted 2 years ago #
  7. vicky515
    Member

    Aha! Figured out the thumbnail...

    <?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?><h2><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></h2>
    		<div class="entry"><?php echo $content ?></div>

    Originally I posted a question asking how to stylize it but there's a super handy explanation in the codex so nevermind!
    http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail#Styling_Post_Thumbnails

    So now it really is just the excerpt that I'm stuck on.

    Posted 2 years ago #
  8. i think this is wrong:

    $content = apply_filters('the_excerpt',$content);

    i think that this (which will always get the full post to the "more" tag:
    $content = $page->post_content;

    needs to be different. maybe

    $content = $page->post_excerpt;

    but i'm not 100% sure what is in the post object- w/o booting up my local install and running a print_r($page) just to see.

    Posted 2 years ago #
  9. vicky515
    Member

    Oh! That's handy! :D And now I understand it a bit better.
    So it looks like it would, except that the post_excerpt key in the array is blank.

    [post_title] => This is a test page [post_excerpt] => [post_status] => publish

    Strange! And unfortunately I didn't see any other keys that contained the appropriate data other than post_content.

    For reference, here are all the keys:

    • [ID]
    • [post_author]
    • [post_date]
    • [post_date_gmt]
    • [post_content]
    • [post_title]
    • [post_excerpt]
    • [post_status]
    • [comment_status]
    • [ping_status]
    • [post_password]
    • [post_name]
    • [to_ping]
    • [pinged]
    • [post_modified]
    • [post_modified_gmt]
    • [post_content_filtered]
    • [post_parent]
    • [guid]
    • [menu_order]
    • [post_type]
    • [post_mime_type]
    • [comment_count]
    • [filter]
    Posted 2 years ago #
  10. try post_excerpt anyway.... if it doesn't have an explicitly set value it might pull the content trimmed to the right length?

    i wonder if you could do something similar to get_posts, use setup_postdata() http://codex.wordpress.org/Template_Tags/get_posts

    then you might be able to call the_excerpt()

    ?

    Posted 2 years ago #
  11. what about:

    function child_pages(){
    global $post;
    
    $mypages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
    
    	foreach($mypages as $post) :  setup_postdata($post);
    
    		$content = $post->post_content;
    		if(!$content) // Check for empty page
    			continue;
    
    		the_post_thumbnail(); ?>
    
    		<h2><a href="<?php the_permalink(); ?>"><?php echo the_title(); ?></a></h2>
    		<div class="entry"><?php echo the_excerpt(); ?></div>
    	<?php
    	endforeach;
    }
    add_action('thematic_belowpost','child_pages');
    Posted 2 years ago #
  12. vicky515
    Member

    Oh sorry, I probably should have said that I tried it first and nothing printed. That's when I tried print_r as per your suggestion and noticed it was blank in the array.

    OMG YOU'RE AWESOME!
    That works perfectly - except the thumbnail, it came out full-size on the page. I replaced it with what I had before which worked great. Is there a reason I should use "the_post_thumbnail" instead?

    <?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>

    Oh boy am I confused though, lol... I'm sorry if these are really basic questions. I had a good understanding when we used the array, but completely scrapping that has me a bit lost. I'm going through the codex so it might click soon...
    You have switched from a "get_" approach to a more direct call. So you're putting only the content of the post in the $content variable, but why does "echo the_excerpt();" work in this case? why couldn't we do this with the array?

    And this goes back, why did "apply_filters('the_excerpt', $content)" not work? I was thinking that made a lot of sense because the filter "the_excerpt" would truncate the results or something but that ended up not being the case. This question is more related to my incomplete grasp of how filters work.

    Thank you <3

    Posted 2 years ago #
  13. middlesister
    Member

    I think the reason that the array didn't work is that post_excerpt is the place for the manually written excerpt, and pages doesn't have them by default. Trying to call it from the array would just turn up an empty string. You can add the functionality if you like with

    function my_add_excerpts_to_pages() {
         add_post_type_support( 'page', 'excerpt' );
    }
    add_action( 'init', 'my_add_excerpts_to_pages' );

    Calling "the_exerpt()" in the loop will check for post_excerpt and when that is empty, generate an excerpt for you. The filters attached to the_excerpt are not the ones doing that. I don't think your grasp of filters is wrong, you were just not applying the one you needed. If you want to use filters I think "get_the_excerpt" would be the one, or call "wp_trim_excerpt($content)" directly. All in all, the "setup_postdata()" method is probably the best way to go, because then you would be guaranteed that your excerpt gets the same treatment and applied filters as all other excerpts. But since you asked... :-)

    Posted 2 years ago #
  14. that is weird about the thumbnail, b/c the small 'thumbnail' size is supposed to be the default if you don't pass any parameters. try

    the_post_thumbnail('thumbnail')

    and setup_postdata is what has allowed me to scrap the $content method that i was using before b/c it gives me access to all the functions that pull from the post's data. also :

    the_post_thumbnail() probably performs

    echo get_the_post_thumbnail($page->ID, 'thumbnail');

    in the backend. so they really aren't different. they're connected. the_post_thumbnail just echos out by default whereas get_the_post_thumbnail returns a variable. like the_excerpt() is just echo get_the_excerpt(). same deal.

    also good point to middle sister that page's don't have excerpts by default and you should add them via her code. but the_excerpt() will look preferentially for a custom excerpt and failing that trim the content to the appropriate length.

    i wish i could tell you why apply_filters('the_excerpt', $content) didn't work, but i honestly don't know.

    Posted 2 years ago #
  15. middlesister
    Member

    The filters attached to 'the_excerpt' are wptexturize, convert_smilies, convert_chars, wpautop and shortcode_unautop. None of these generate any excerpt. 'wp_trim_excerpt' is attached to 'get_the_excerpt', so probably apply_filters('get_the_excerpt', $content) would work.

    Posted 2 years ago #
  16. vicky515
    Member

    Hi Middle and Helga! Sorry about my brutal delay in responding - I was busy preparing for a conference.

    Thanks so much for that explanation, Middle, that makes perfect sense. I will definately be keeping this topic handy for future reference :)

    As for the thumbnail,
    the_post_thumbnail('thumbnail')
    worked exactly! I think I'm more comfortable using this approach, just because it's consistent with the rest of the code and easier to understand.

    Thank you so much Helga, again, for your amazing help.
    And middlesister, your explanations were extremely helpful! <3

    Posted 2 years ago #
  17. you're welcome. i'm glad you got it sorted.

    Posted 2 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.