WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Embedding posts into page (7 posts)

  1. Mohamed Mounir
    Member
    Posted 1 year ago #

    Hey all geeks out there,

    I just went through some topics discussing how to embed posts from specific category to a simple page, so that I can benefit from the custom sidebar function, body content of the page and posts styling.

    Below are the original code for my page.php category.php and the custom template page I created using the original template page-cat.php

    page.php

    <div class="inner">
        <div class="container">
        <div class="main">
    	<?php the_breadcrumb(); ?>
        <div class="box_outer">
    	<article class="cat_article">
    	        <h1 class="cat_article_title page_title"><?php the_title(); ?></h1>
        <div class="single_article_content">
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <?php the_content(); ?>
    	<div class="clear"></div>
    	<?php wp_link_pages(); ?>
    	<?php endwhile; else: ?>
    	<?php endif; ?>
         <?php wp_reset_query(); ?>
       </div> <!--Single Article content-->
       <?php if(get_post_meta($post->ID, 'mom_page_comments', true)) { ?>
        <div class="dots"></div>
        <?php comments_template(); ?>
       <?php } ?>
        </article> <!--End Single Article-->
        </div> <!--Box Outer-->
        </div> <!--End Main-->

    category.php

    <div class="inner">
        <div class="container">
       <?php if(of_get_option('feature_cat_style') == 'full') { ?>
    <?php if(of_get_option('cat_feature_on')) { ?>
       <?php include TEMPLATEPATH . '/feature-full.php'; ?>
    <?php } ?>
       <?php } ?>
    
        <div class="main">
    <?php the_breadcrumb(); ?>
       <?php if(of_get_option('feature_cat_style') == 'default') { ?>
    <?php if(of_get_option('cat_feature_on')) { ?>
       <?php include TEMPLATEPATH . '/feature.php'; ?>
    <?php } ?>
       <?php } ?>
    <?php if(of_get_option('cat_lv_on')) { ?>
        <?php include TEMPLATEPATH . '/latest-video.php'; ?>
    <?php } ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php cat_article(); ?>
    <?php endwhile; ?>
    <?php  else:  ?>
    <!-- Else in here -->
    <?php  endif; ?>
    <?php mom_pagination(); ?>
    <?php wp_reset_query(); ?>
        </div> <!--End Main-->

    page-cat.php

    <div class="inner">
        <div class="container">
        <div class="main">
    	<?php the_breadcrumb(); ?>
        <div class="box_outer">
    
    <?php query_posts('cat=5&showposts=4'); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php cat_article(); ?>
    <?php endwhile; ?>
    <?php  else:  ?>
    
    <!-- Else in here -->
    <?php  endif; ?>
    <?php mom_pagination(); ?>
    <?php wp_reset_query(); ?>
    </div>
        </div> <!--End Main-->

    It seems that everything is going well except for the pagination, in the page 2 or 3 or whatever it keeps showing the same first 4 posts.

    Also the page contents are not appearing before the posts. I used <?php the_content(); ?> but I didn't work out.

    Any recommendations ?

  2. bythegram
    Member
    Posted 1 year ago #

    For pagination:
    First thing I would try is change
    <?php query_posts('cat=5&showposts=4'); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    to

    <?php query_posts('cat=5&posts_per_page=4'); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    "showposts" is deprecated as of version 2.1 (replaced by posts_per_page).

    I'd look into whatever that mom_pagination function is doing as well.

    For the content that seems to also be a unique function "cat_article" so it would have something to do with your theme.

    I know that might not have helped too much be it'll give you something to look into.

  3. Mohamed Mounir
    Member
    Posted 1 year ago #

    Thank you very much @bythegram well explained I will give it a try and change showposts to posts_per_page as soon as I return back my room

    For the content issue I found that calling <?php the_content(); ?> outside the posts loop make it works

    <div class="box_outer">
    
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <?php the_content(); ?>
    	<div class="clear"></div>
    	<?php wp_link_pages(); ?>
    	<?php endwhile; else: ?>
    	<?php endif; ?>
    <?php wp_reset_query(); ?>
    <?php query_posts('cat=10'); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php cat_article(); ?>
    <?php endwhile; ?>
    <?php  else:  ?>
    <?php  endif; ?>

    Will give you feedback about the results ... I appreciate your reply :)

  4. Mohamed Mounir
    Member
    Posted 1 year ago #

    @bythegram I did try posts_per_page instead of showposts but it didn't work out, I don't think <?php mom_pagination(); ?> function is the problem as it works fine in the category page

  5. bythegram
    Member
    Posted 1 year ago #

    ahh I think I got it.

    change your query to this:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts('cat=5&posts_per_page=4&paged=' . $paged);
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    ?>

    See this page for more tips

  6. Mohamed Mounir
    Member
    Posted 1 year ago #

    WOW !!! it works like charm (Y) well done. Thanks for the great help. Could you please explain ?

  7. bythegram
    Member
    Posted 1 year ago #

    To be honest I just looked into the codex for a little more detail on how the WP_Query class uses pagination.

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $query = new WP_Query( 'paged=' . $paged );

    Display posts from the current page and set the 'paged' parameter to 1 when the query variable is not set (first page).

    more infor here

Topic Closed

This topic has been closed to new replies.

About this Topic