• Hello,
    I have a wordpress website built with pages, like a classic website. It looks like the following :
    – page named “home” (page_id=2) uses a template homepage.php and is set as homepage in WP prefs
    – page named “misc” (page_id=3) uses a template misc.php

    When I go to my web site, I get the content of page “home” with an url like “www.mysite.com/”. I can browse to the “misc” page and the url will be “www.mysiste.com/?page_id=3”. Note the fact that page_id is not set or displayed for the homepage as it is the cause of my problem.

    Let’s say I want to display posts in each page. I add some PHP code to each template, using posts_query to retrieve the posts I want (i.e. “home” will display all posts while “misc” will display only one category). All works well until I want to page the posts displayed : e.g. I have 10 posts and I want to display 4 per page.

    It works as expected on the “misc” page which is not set as homepage (I get urls like mysite.com?page_id=3&paged=2) but it doesn’t work on the “home” page. As the page_id in the url is not set (mysite.com/?paged=2 instead of mysite.com/?page_id=2&paged=2) : when displaying the previous posts I get the index.php page instead of my homepage.php template.

    Any idea of how to display paged posts on a templated page set as homepage ?

    Here is the code of my homepage.php template, if it can help to point out a problem :

    <?php
    /**
     * @package WordPress
     * @subpackage Default_Theme
     */
    /*
    Template Name: Homepage
    */
    ?>
    
    <?php get_header(); ?>
    
    <div id="content" class="narrowcolumn">
    
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    		<div class="post" id="post-<?php the_ID(); ?>">
    			<div class="entry">
    				<div id="homepage_text">
    					<?php the_content(); ?>
    				</div>
    			</div>
    		</div>
    
    		<?php edit_post_link('Edit this page.', '<p>', '</p>'); ?>
    	 <?php endwhile; endif; ?>
    
    	<div id="blog">
    		<?php query_posts('showposts=4'.'&paged='.$paged);
    
    		global $more;
    		// set $more to 0 in order to only get the first part of the post
    		$more = 0; 
    
    		while (have_posts()) : the_post(); ?>
    			<div <?php post_class() ?>>
    				<small class="blog_date"><?php the_time('l j F Y') ?></small>
    				<h3 id="post-<?php the_ID(); ?>"><a>" rel="bookmark" title="Permalink to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
    				<div class="entry">
    					<?php the_content('Read the full article &raquo;'); ?>
    				</div>
    				<p class="postmetadata">
    				<?php the_tags('Keywords&nbsp;: ', ', ', ''); ?> Published in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No comment ?', '1 comment ?', '% comments ?', 'comments-link', 'Commentaires are closed'); ?>
    				</p>
    			</div>
    
    		<?php endwhile; ?>
    
    		<div class="navigation">
    			<div class="alignleft"><?php next_posts_link('&laquo; Older articles') ?></div>
    			<div class="alignright"><?php previous_posts_link('Newer articles &raquo;') ?></div>
    		</div>
    		<?php wp_reset_query(); ?>
    
    	</div>
    </div>
    
    <?php /* get_sidebar(); */ ?>
    <?php get_footer(); ?>

Viewing 15 replies - 16 through 30 (of 37 total)
  • TommyHoliday

    (@tommyholiday)

    @vtxyzzy,

    whiiiihaaaa!! thank you very much man!! it works just beautifully! perfect! ??
    you made my day! ??

    thanks again!
    Tommy

    vtxyzzy

    (@vtxyzzy)

    You are welcome. I have not tried it when using pretty permalinks, so you probably need to experiment with that before releasing it.

    TommyHoliday

    (@tommyholiday)

    yes, this i what i tried yet. unfortunately it’s not working with pretty permalinks. and i’m not very familiar with PHP, so i don’t know how i can fix this. do you have an idea?

    vtxyzzy

    (@vtxyzzy)

    Possibly you can use code similar to that found here.

    vtxyzzy

    (@vtxyzzy)

    This has been quite an experience, but I believe that I now have it working for pretty permalinks. I basically used my own pagination.

    Put this in your template before the loop (take out ‘posts_per_page’ to use the Reading option):

    <?php
       global $post; $pageid = $post->ID;
       if (isset($_GET['mypaged'])) {
          $paged = $_GET['mypaged'];
       } else {
          $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       }
       $args = array(
          'paged' => $paged,
          'posts_per_page' => 3,
          'caller_get_posts' => 1,
       );
       query_posts($args);
       ?>

    and use this for the pagination:

    <div class="navigation">
          <?php
             $hrefpat = '/href=[\"\']([^\"\']+)[\"\']/';
             $next_link = get_next_posts_link('&laquo; Older Entries');
             if (preg_match($hrefpat,$next_link,$matches)) {
                $link = $matches[0];
                $href = $matches[1];
                $parts = explode('?',$href);
                if (preg_match('/\/page\/(\d+)\/?/',$parts[0],$matches)) {
                   $next_page = $matches[1];
                   $parts[0] = preg_replace('/\/page\/\d+\/?/','/',$parts[0]);
                   $count = 0;
                   $parts[1] = preg_replace('/mypaged=\d+/',"mypaged=$next_page",$parts[1],-1,$count);
                   if (!$count) {
                      if ($parts[1]) {
                         $parts[1] = "mypaged=$next_page&{$parts[1]}" ;
                      } else {
                         $parts[1] .= "mypaged=$next_page";
                      }
                   }
                } elseif (!preg_match('/page_id=/',$parts[1])) {
                      $parts[1] = "page_id=$pageid&{$parts[1]}";
                }
                $next_link = preg_replace($hrefpat,"href='{$parts[0]}?{$parts[1]}'",$next_link);
             }
             $prev_link = get_previous_posts_link('Newer Entries &raquo;');
             if (preg_match($hrefpat,$prev_link,$matches)) {
                $link = $matches[0];
                $href = $matches[1];
                $parts = explode('?',$href);
                if (preg_match('/\/page\/(\d+)\/?/',$parts[0],$matches)) {
                   $prev_page = $matches[1];
                   $parts[0] = preg_replace('/\/page\/\d+\/?/','/',$parts[0]);
                   $count = 0;
                   $parts[1] = preg_replace('/mypaged=\d+/',"mypaged=$prev_page",$parts[1],-1,$count);
                   if (!$count) {
                      if ($parts[1]) {
                         $parts[1] = "mypaged=$prev_page&{$parts[1]}" ;
                      } else {
                         $parts[1] = "mypaged=$prev_page";
                      }
                   }
                } else {
                   $prevpg = $paged - 1;
                   $mypaged = ($prevpg > 1) ? 'mypaged=' . $prevpg : '';
                   if (preg_match('/mypaged=/',$parts[1])) {
                      $parts[1] = preg_replace('/mypaged=\d+/',$mypaged,$parts[1]);
                   } else {
                      $parts[1] = "$mypaged{$parts[1]}";
                   }
                   if (!preg_match('/page_id=/',$parts[1])) {
                      $parts[1] = ($parts[1]) ? "page_id=$pageid&{$parts[1]}" : "page_id=$pageid";
                   }
                }
                $prev_link = preg_replace($hrefpat,"href='{$parts[0]}?{$parts[1]}'",$prev_link);
             }
             ?>
             <div class="alignleft"><?php echo $next_link; ?></div>
             <div class="alignright"><?php echo $prev_link; ?></div>
             <div class='clear'></div>
          </div>

    Have you looked to see if there are any filters available? , might lighten the script if you can use a few hooks, who knows you might not need all the regex if the right hooks exist..

    Just a suggestion, because all the regex might become taxing later on..

    TommyHoliday

    (@tommyholiday)

    wow man! thank you very much for the time to code this!

    ???hm, is there a way to put this code in functions.php? i don’t want all the code in my page template…

    vtxyzzy

    (@vtxyzzy)

    Sure. Make two functions like this (UNTESTED!):

    <?php function my_next_post_link_filter($next_link = '') {
       $hrefpat = '/href=[\"\']([^\"\']+)[\"\']/';
       if (preg_match($hrefpat,$next_link,$matches)) {
          // Lots of code here
          $next_link = preg_replace($hrefpat,"href='{$parts[0]}?{$parts[1]}'",$next_link);
       }
       return $next_link;
    }?>

    and this:

    <?php function my_prev_post_link_filter($prev_link = '') {
       $hrefpat = '/href=[\"\']([^\"\']+)[\"\']/';
       if (preg_match($hrefpat,$prev_link,$matches)) {
          // Lots of code here
          $prev_link = preg_replace($hrefpat,"href='{$parts[0]}?{$parts[1]}'",$prev_link);
       }
       return $prev_link;
    }?>

    Then, call like this:

    $next_link = my_next_post_link_filter(get_next_posts_link('&laquo; Older Entries'));
    <div class="alignleft"><?php echo $next_link; ?></div>
    $prev_link = my_prev_post_link_filter(get_previous_posts_link('Newer Entries &raquo;'));
    <div class="alignright"><?php echo $prev_link; ?></div>
    vtxyzzy

    (@vtxyzzy)

    OOPS – you will need to pass $pageid to both functions:

    $next_link = my_next_post_link_filter(get_next_posts_link('&laquo; Older Entries'),$pageid);

    and modify the function defs:

    <?php function my_next_post_link_filter($next_link = '',$pageid = null) {

    vtxyzzy awesome fix, i used your first post, but for your 2nd maybe u can explain to people exactly what you did, it looks like you have 2 separate functions and 1 call back, if u explain this a little bit more in depth like whether all three of these go in the template file etc or if the 2 functions go in functions.php

    you could even write a blog post or something, youd prob get a lot of hits, this is a pretty common thing people would want ??

    i would love to know wtf this does btw, it just makes me confused (i just got off work and am tired tho…) $hrefpat = ‘/href=[\”\’]([^\”\’]+)[\”\’]/’;

    Sorry for the confusion on the call-back. I just gave the first one as an example – you need to have one for get_previous_posts_link and one for get_next_posts_link.

    The functions can either go in the template file, or in functions.php. The callbacks replace the normal calls to previous/next_posts_link in the template.

    As for the $hrefpat, it says:

    • Start the pattern: ‘/’
    • Find ‘href=’: ‘href=’
    • Followed by either a single or double quote: ‘[\”\’]’
    • Followed by a string that does not contain either quote, and save it (the parens): ‘([^\”\’]+)
    • Followed by a single or double quote: ‘[\”\’]’
    • End the pattern: ‘/’

    In a string like this:

    <a href='https://mysite.com/?anyparams=abc' >My Site</a>

    it will match https://mysite.com/?anyparams=abc and save it in $matches[0].

    The pattern could be improved to make it more general, but this seems to work for its purpose.

    awesome, ty ty, this has worked perfectly, did you have any other plugins have problems when putting posts on a page other than the homepage. I installed a plugin that works perfectly (shadowbox) on individual post pages and when they were displaying on the homepage but when i am on my custom template page where i have only comic posts or the blog page where only blog posts the pictures do not get cropped correctly and get all resized funny, i am assuming it is a similar issue.

    I have not seen that, so I’m afraid I can’t help with it.

    ok so i did some more troubleshooting, all plugins that blow up pictures nice and pretty (i.e.) shadowbox, thickbox, lightbox, etc, they will work fine on the homepage if there is a list of posts, they will work fine on the post page, but if you have a page that lists posts all of them will crop the image weird. i look at the html for that particular image and it looks the same, they both call the right class, this has got to be a super similar issue!

    for me i have a menu
    HOME BLOG ABOUT COMICS VIDEOS PORTAL CONTACT-US
    (https://www.capsoffplease.com/2.0)

    so all posts that are ‘featured’ show up under Home
    posts marked category BLOG show up under blog
    posts marked category COMICS show up under Comics
    and so on

    on the comics post i want to list 3, that works with a query and then i have like medium sized images that will link to the full-size image.

    This is where the problem will work, no image (plugin) will work correct, it like crops it incorrectly, it looks like it moves it left like 200 px….) WEIRD! any ideas?

Viewing 15 replies - 16 through 30 (of 37 total)
  • The topic ‘How do I add posts on a templated page used as home ?’ is closed to new replies.