• 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 - 1 through 15 (of 37 total)
  • Thread Starter abelthorne

    (@abelthorne)

    Well, I’ve added a header redirection in the header.php
    <?php if(isset($_GET['paged']) && !isset($_GET['page_id'])) header("location: ?page_id=2&paged=".$_GET['paged']); ?>

    It’s quite a ugly hack but it works. If someone has a better idea… ??

    Thread Starter abelthorne

    (@abelthorne)

    Thanks for your answer but it doesn’t seem to do what I need : the description says that with this plugin I can manually add posts in a static page with their ID.
    What I want is to automatically display all posts in some pages rather than adding them one by one. Basically, I want to blend the “blog” page inside the static homepage.

    On the static homepage, I have to display all the posts (divided in “subpages”) under the page content. On the second page, I have to display all the posts (divided in “subpages”) but only of a specific category (press releases).

    But maybe there exists a similar plugin that does what I need ?

    @abelthrone, thank you very much man! you’re the first who had exactly the same problem as i have… but unfortunately nobody has a good solution for it. =/

    i would use your solutions, but i give my theme away for free and page_id of the homepage can be way different… so for me it’s still not a solution i can use.

    anybody another solution or work around?

    thx….

    Can you provide a link to your site and a copy of your home page template? It looks like the code to set up the links in the homepage.php above is not correct. When I copied this line:

    <h3 id="post-<?php the_ID(); ?>"><a>" rel="bookmark" title="Permalink to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

    to single.php in my test site, the link was not formed correctly. I don’t know if that is your problem, or not. I had to use this to get the correct link:

    <h3 id=post-<?php the_ID(); ?>><a rel='bookmark' title='Permalink to <?php the_title_attribute(); ?>' href='<?php the_permalink() ?>'><?php the_title(); ?></a></h3>

    @vtxyzzy,

    here’s a link to a working demo: https://geikon.thomasveit.com/
    As you can see, when you click older entries on the startpage it goes to automatically to the Blog layout site.

    Here’s a zip with my portfolio.php which is set as homepage. https://geikon.thomasveit.com/wp-content/uploads/2010/02/portfolio.zip

    thank you for the help!

    vtxyzzy

    (@vtxyzzy)

    @tommy (hijacked thread!) I installed your theme on a test site and assigned the portfolio.php file as the template for the Home page. Everything works fine. I suspect the problem is in the way your permalink structure is set up. Try resetting to the default and see if the problem goes away. Please start your own thread if not.

    @abelthorne, sorry for the detour.

    Query your page with page/1/ and the same problem occurs…

    The reason i believe the content changes with paging is because a different template file is being read when a paged query is made, ie. site.com/page/X (x is a number).. as appose to site.com (which reads the portfolio).

    I think when you query for paged results, you’re requesting that data through the index.php of the theme, which clearly manages the content differently then that of your portfolio template..

    Generally the WordPress url plus query vars does do it’s stuff through the index.php unless.. “for example” you had a category.php, in which case queries like /?cat=5 would route to the category.php instead of index.php, the same will most likely ring true for other query vars to..

    I didn’t read the thread in-depth, so if i’m totally off the mark, just kindly ignore me… ??

    TommyHoliday

    (@tommyholiday)

    @vtxyzzy, no, i’m very sure the problem is not the way i set up the permalink structure! when i click on the startpage on “older entries” wordpress generates a link like this: https://geikon.thomasveit.com/?paged=2. as you can see, it’s using index.php to display the content like “t31os_” says correctly.

    and now just add the page_id from the homepage to the url like so:
    https://geikon.thomasveit.com/?page_id=143&paged=2

    and voila! it works.

    I think that behaviour is possibly to be expected .. did you want it to do something else?

    TommyHoliday

    (@tommyholiday)

    well, yes you are right. but this is a wrong behaviour, isn’t it?

    it should somehow redirect or add the page_id so that on the paged sites the right template is in use?! when i set portfolio.php as the homepage it should on the paged homepage sites also use portfolio.php and not index.php -.-

    Home really only means the home url and nothing more in my understanding, i don’t think it’s quite as versatile as an archive page ie. regular WP archival pages, category page, tag pages, regular index page(lists of posts).. but then i’m not entirely sure, i don’t use a “home” page setup of any kind myself..

    In short i think the home page is simply a one off placeholder for a home url, i’m not sure it’s intended to function like regular archive pages.

    I could be totally wrong though, i honestly don’t know for sure..

    TommyHoliday

    (@tommyholiday)

    okay thank you.

    the thing is, that i want to make the theme as flexible as possible because i want to give it away for free. the user can then choose from a range of portfolio page templates (Web Design, Graphic Design, Video, etc.) And every time the user have more portfolio items it goes automatically to the index.php template… =/

    mhh, the only thing i can do is to take out the pagination function out of the templates i think…

    or do you have even another good idea? ??

    I’d look at how other theme’s do it..

    Like i said, i honestly don’t know how it should behave… i could be totally wrong (would never learn anything from always being right).. but there’s alot of versatility in WordPress, and i’ve seen some really clever code in theme’s avaiable..

    Don’t worry about what theme’s look like, it’s what’s under the hood that you should give you the ideas or inspiration…. and don’t worry about looking at someone elses code, noone ever learnt by doing everything themself…

    Don’t comprimise a feature you wanted, just keep working at it, and looking at other code for examples of how to do things, you’ll get there… ??

    vtxyzzy

    (@vtxyzzy)

    @tommy,

    Thanks to the leads from t310s_, I think I have an answer, at least it works for me in one site. At the top of your template, grab the ID of the ‘Home’ page:

    global $post; $pageid = $post->ID;

    then, in your navigation, use code like this to insert the page_id and display the links:

    <?php $next_link = get_next_posts_link('&laquo; Older Entries');
    $next_link = preg_replace('/\?paged=/','?page_id=' . $pageid . '&paged=',$next_link);?>
    <div class="alignleft"><?php echo $next_link; ?></div>
Viewing 15 replies - 1 through 15 (of 37 total)
  • The topic ‘How do I add posts on a templated page used as home ?’ is closed to new replies.