• Resolved Derrtass

    (@derrtass)


    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.]

Viewing 4 replies - 1 through 4 (of 4 total)
  • If I understood you correctly, you wanted something like this:

    Example:

    • You have two pages called Android and iPhone with the slugs android and iphone respectively. You also have two posts with the same titles and the same slugs.
    • You want to display post “iPhone” on somewhere on page “iPhone”. The same applies to post “Android”.

    If that’s the case, try this code:

    <?php
    /**
     * Plugin Name: Derrtass&rsquo; Posts for Pages Plugin
     */
    
    /* Hook our querying function into WordPress. */
    add_action( 'pre_get_posts', 'derrtass_pre_get_posts' );
    
    /**
     * Filter through the posts allowed to appear on pages by
     * comparing the current page slug to our blog posts and
     * display the matching post on the current page.
     *
     * As described on WordPress developer blog.
     *
     * @link https://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/
     * @link https://codex.www.ads-software.com/Class_Reference/WP_Query#Post_.26_Page_Parameters
     * @link https://codex.www.ads-software.com/Conditional_Tags#A_PAGE_Page
     */
    function derrtass_pre_get_posts( $query ) {
        global $post;
        $page_slug = $post->post_name;
    
        // Get a post with the same slug as this page
        if ( $query->is_page( $page_name ) ) {
            $query->set( 'name', $page_name );
            $query->set( 'posts_per_page', 1 );
        }
    }

    Use the above code as a plugin or in your theme’s functions.php, once you do this, use the normal WordPress loop to display the post you wanted on the target page.

    Example:

    <?php if ( have_posts() ) : ?>
    	<?php while ( have_posts() ) : the_post(); ?>
    		<h1 class="entry-title">
    			<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( 'Permalink to %s', the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
    		</h1>
    		<?php the_excerpt(); ?>
    	<?php endwhile; ?>
    <?php endif; ?>

    I didn’t test this code, but it should work.

    Thread Starter Derrtass

    (@derrtass)

    Hi

    Thanks for your reply this exactly what i want… however the code you have sent isn’t working..

    I have uploaded this to functions.php

    /* Hook our querying function into WordPress. */
    	add_action( 'pre_get_posts', 'derrtass_pre_get_posts' );
    
    	function derrtass_pre_get_posts( $query ) {
        global $post;
        $page_slug = $post->post_name;
    
        // Get a post with the same slug as this page
        if ( $query->is_page( $page_name ) ) {
            $query->set( 'name', $page_name );
            $query->set( 'posts_per_page', 1 );
        }
    }

    And used loop in template to display post….

    It’s not working I’m afraid

    Well, that was my bad. pre_get_posts is used to modify the main query, but since you wanted to modify a secondary loop on the page, we’ll use to good old WP_Query.

    Forget whatever I said in my previous post, and put the following loop where you wanted your posts to appear.

    <?php 
    
    if ( is_page() ) {
    	global $post;  
    
    	$page_slug = $post->post_name;
    
    	$args = array(
    		'post_type' => 'post',
    		'name' => $page_slug,
    		'posts_per_page' => 1
    	);
    	$My_Query = new WP_Query( $args ); 
    
    	?>
    
    	<?php if ( $My_Query->have_posts() ) : ?>
    		<?php while ( $My_Query->have_posts() ) : $My_Query->the_post(); ?>
    			<h1 class="entry-title">
    				<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( 'Permalink to %s', the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
    			</h1>
    			<?php the_excerpt(); ?>
    		<?php endwhile; ?>
    	<?php else:
    	// Your fallback content here
    	?>
    	<?php endif; ?>
    	<?php wp_reset_postdata(); ?>
    
    <?php } ?>

    It should work this time.

    Thread Starter Derrtass

    (@derrtass)

    Hey Mate it works just fine ??
    You have made my day thanks a lot!!!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Display post on page with this same name as page name’ is closed to new replies.