Display post on page with this same name as page name
-
Hi
I’m building a website with many pages where the right hand side of each page should display different post… So i need to use 1 different post per page… And i want to use just one template so i need the code to display post with this same name as page name (slug).
I know how to do it with categories: Displaying posts from this same category as page name.
<?php $pageslug = $post->post_name; // name of the page (slug) $catslug = get_category_by_slug($pageslug); // get category of the same slug $catid = $catslug->term_id; // get id of this category $query= 'cat=' . $catid. ''; query_posts($query); the_post(); // run the query ?>
However as i have only 1 post in category it is pointless to create so many different categories and have only one post in it…. So on page i want to display post with this same name as page name… I have tried to change the code above, however is not working i’m rookie with PHP so please help …….
<?php $pageslug = $post->post_name; // name of the page (slug) $catslug = get_post_by_slug($pageslug); // get post of the same slug $catid = $catslug->term_id; // get id of this post $query= 'p=' . $catid. ''; query_posts($query); the_post(); // run the query ?>
[Moderator Note: Please post code or markup snippets between backticks or use the code button. As it stands, your code may now have been permanently damaged/corrupted by the forum’s parser.]
- The topic ‘Display post on page with this same name as page name’ is closed to new replies.