Felix
Felix

Reputation: 1583

Anchor list of blog posts (Wordpress)

I want to create an anchor list of all my blog posts. The list is supposed to be on the top of the blog page. When I add a new post, the anchor link should be added automatacly.

Like this: I got a page with all my blog posts and I want a list of all the posts at the top. "First post" anchor link should link to the heading of the First post on the same page. And so on.

I've tried various plugins but they seem to only create an anchor list inside a blog post or on a page. Do you have any idea how to solve this?

Upvotes: 1

Views: 849

Answers (2)

bingjie2680
bingjie2680

Reputation: 7773

try this:

$posts=get_posts(array('numberposts' => -1));
//list post titles 
echo '<ul>';
foreach($posts as $post){ 
    $post_title = $post->post_title;
    echo "<li><a href='#$post_title' title='$post_title'>$post_title</a></li>";
}
echo '</ul>';

//now list post contents to be linked on the same page
echo '<div>';
foreach($posts as $post){
    $post_title = $post->post_title;
    $content = $post->post_content;
    //echo "<h3 id='$post_title' title='$post_title'>$post_title</h3>";
    echo "<p id='$post_title'> $content </p>";
}
echo '</div>';

Upvotes: 1

Hal
Hal

Reputation: 1009

Try this:

echo '<ul>';
foreach(get_posts(array('numberposts' => -1)) as $mypost) //Gets all posts
{
    $post_url = get_permalink($mypost->ID);
    $post_title = $mypost->post_title;
    echo "<li><a href='$post_url' title='$post_title'>$post_title</a></li>";
}
echo '</ul>';

Insert this in your theme file wherever you want to generate your post list. Setting the numberposts option on get_posts to -1 returns all of your blog's posts.

Upvotes: 1

Related Questions