WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display both post and page excerpts in the loop (12 posts)

  1. Krokodyl
    Member
    Posted 2 years ago #

    I enabled in my WordPress adding excerpts for pages. Now I want to display on the home page excerpts only of those posts/pages that have an excerpt.

    I'm using this code for this:

    $args=array(
      'order' =>'desc',
      'orderby' => 'date',
      'post_type' => array('post', 'page')
      );
    $page_query = new WP_Query($args);
    
        if (have_posts()) : while ($page_query->have_posts()) : $page_query->the_post();
    	if (has_excerpt()) {...

    But this doesn't work. When I use "array('page')", only excerpts of pages are displayed. When I use array('post'), only excerpts of posts are displayed. This is correct.

    But when I use "array('post', 'page')", displayed are only one post and one page excerpt.

    Any ideas how to fix this?

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    How did you enable excerpts for pages? With this in your theme's functions.php?

    add_post_type_support( 'page', 'excerpt' );

    Or some other way?

    Try it without the new WP_Query($args); in your index.php and with a normal loop like this:

    if ( have_posts() ) : while ( have_posts() ) : the_post();

    And with this in your theme's functions.php: http://pastebin.com/qUDgAUA7

    Only posts or pages that have an excerpt will be shown on the home page

  3. Krokodyl
    Member
    Posted 2 years ago #

    It works! Thanks a lot. I lost the whole day on this, but I don't think I would ever solve it on my own.

    P.S.
    Yes I used the line add_post_type_support( 'page', 'excerpt' ); to enable excerpts

  4. keesiemeijer
    moderator
    Posted 2 years ago #

    You're welcome. Querying the loop is always a tricky thing. I'm glad you got it resolved.

  5. Krokodyl
    Member
    Posted 2 years ago #

    Actually I've got another problem, but this is something different although connected with the previous one (is this understandable :)

    I made several static pages with custom templates. On these pages I display excerpts of posts assigned to appropriate categories.
    But I'd also like to display on these pages excerpts of pages assigned to these same categories.

    E.g. I have category Programs. I also have a page called My Programs. When I create a new page or post and assign it to the category Programs, its excerpt should show up on the page My Programs (as well as on home page, which is already settled :)).

    I use the plugin Map Categories to Pages.

  6. keesiemeijer
    moderator
    Posted 2 years ago #

    This will be even trickier. Can you paste and submit the full code of the custom Page template (My Programs) into a pastebin.com and post the link to it here? see the Forum Rules for posting code and using the pastebin.

  7. Krokodyl
    Member
    Posted 2 years ago #

    The code is very simple, so I think I can post it here (of course this is only the core, I omitted the structural markup etc.):

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    		$args= array('category_name' => "Category name",
    		'paged' => $paged
    		);
    		query_posts($args);

    However I think the problem might be connected with the plugin itself or my theme. Pages are added to categories, and the counter of items in a category gets incremented, but when I open the category page, there are only posts there.

    You can see it here: http://www.bt4.pl/
    If you scroll to the bottom, you'll see category (under Kategorie) TĹ‚umaczenia informatyczne with number 3 next to it. But when you open this category, there are only two items.

  8. keesiemeijer
    moderator
    Posted 2 years ago #

    Do you use that code on category.php (or archive.php) or on another template file?

  9. Krokodyl
    Member
    Posted 2 years ago #

    On another template file.

  10. keesiemeijer
    moderator
    Posted 2 years ago #

    I ask this because the url is like this: http://www.bt4.pl/category/tlum-inf/ and the body_class doesn't indicate this is a custom Page template file.

    change it to this in your functions.php: http://pastebin.com/4mSF72EB

  11. Krokodyl
    Member
    Posted 2 years ago #

    This works. Thanks :) Sorry for my little knowldge of WordPress internals :(

    Only one more question. The is_category() tag can be used inside the Loop. But I also want to display the excerpts from the category on one template page whis is not a category.

    I copied the site to another address so that I can play with it without problems. Now the category page http://bt7.pl/category/tlum-inf/ works very well.

    But I also want to display items assigned to the same category on the page (custom template) http://bt7.pl/lingwistyka/tlumaczenia-informatyczne/.

    I tried to use is_page and is_page_template but these didn't work for me (I suppose because they cannot be used inside the Loop).

  12. Krokodyl
    Member
    Posted 2 years ago #

    I solved the problem myself. Only a small addition was needed.

    In the code:
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    		$args= array('category_name' => "Category name",
    		'paged' => $paged
    		);
    		query_posts($args);

    I had to add page as a post typu (by default the parameter post_type has the value only post):

    $args= array('category_name' => "$cat_name",
    		'paged' => $paged,
    		'post_type' => array('post', 'page')
    		);

    Thanks again for your help. If it wasn't for you I would be still at the beginning :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.