Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Suppose that I have a blog with few posts per month, and no comments, and I don't want to show posts in single post pages, but always "in context" (say, together with the posts in its month - or perhaps a week?). To accomplish this, I'd need to use permalinks with fragment identifiers (#), among other things. I wonder if this is supported, or if someone can point me to some resource or discussion.

Edited: I posted in my own answer the details of my implementation.

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

There are two hooks named 'pre_post_link' and 'post_link'. Their first argument – which you can change – is the permalink. So …

add_filter( 'pre_post_link', 'wpse_42406_change_permalink', 10, 2 );

function wpse_42406_change_permalink( $permalink, $post )
{
    // change the permalink, let it point to an archive etc.
    return $permalink;
}

… will change the output.

In your loop you probably use something like …

<li id="post-<?php the_ID(); ?>">

Your permalink could then point to the archive page of your choice plus #post-$post->ID. For example:

http://example.com/2012/04/#post-42
share|improve this answer
1  
Toscho - this would prove problematic for paginated archives though right? –  Stephen Harris Feb 15 '12 at 12:12
    
Yes, you’d need to calculate the correct page … which is a moving target because new posts are added. So it is not exactly a permalink. :) –  toscho Feb 15 '12 at 12:21
    
It's a weakness inherent in the request though really - any use of '#' assumes a month's worth of posts all fit inside one page. So the number of posts on each archive page would have to be set 'sufficiently' high. Leonbloy- a simple application of pre_get_posts should do the trick :). –  Stephen Harris Feb 15 '12 at 12:28
    
With the above caveat +1 :). –  Stephen Harris Feb 15 '12 at 12:30
1  
Plug-ins could even undo your example :D. You'd just have to hook it really late. It's not fool-proof, but the other option is to have a non-permanent permalink. Or maybe interpret 2012/04/#post-42 and redirect it to the appropriate archive page with #post-42? –  Stephen Harris Feb 15 '12 at 12:51
show 4 more comments

Just open your theme files and remove the a tags from the title.

Example: In your index.php file if you had:

<h2>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
    <?php the_title(); ?></a>
</h2>

it would become:

<h2 class="entry-title"><?php the_title(); ?></h2>
share|improve this answer
add comment

Just in case some visitor find this info useful, I post my working implementation. It requires a little plugin.

In this implementation, the permalinks will show the monthly archive - but the url will still be "complete", i.e., include the post title - In this way, 1) I have more freedom if I wish to change this schema later, without breaking the permalinks, and 2) I have meaningful stats for my visits (I can know which individual posts was visited from my logs).

In the admin page I specify this permalinks structure :

/%year%/%monthnum%/%postname%  
eg: http://example.com/myblog/2012/01/sample_post

But the final permalink will be of the form:

http://example.com/myblog/2012/01/sample_post#post-234

The fragment identifier is added by the following hook in the plugin:

/* adds hash */
function hjg_change_permalink( $permalink, $post ) {
    //if(strpos($permalink,'#')) return $permalink;
    return $permalink . "#post-" . $post->ID;   
}

add_filter( 'post_link', 'hjg_change_permalink', 100, 2 );

I also hoooked the following in the plugin, so that the permalinks, despite looking as individual posts url, are interpreted as archives:

function hjg_parse_request( $wp ) {
   if(! isset($wp->query_vars)) return;
   if(! isset($wp->query_vars['year'])) return;
   $wp->query_vars['name']=''; // remove post name
}

add_action( 'parse_request', 'hjg_parse_request');

My theme is inherited from the standard twenty-eleven, which already ads a "post-NNN" id to each post, in content.php :

   <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

if you are using other theme, you'd need to add or adapt this.

Remeber that, for this to work, your monthly archives should not be paged: you need to have a maximum number of posts per month, and set that value in the "posts-per-page" in the admin page. (I also tweaked the index.php file in my theme so that the front page shows a smaller number of posts).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.