Support » Fixing WordPress » my_postid

  • Resolved GaryKay

    (@garykay)


    Hi,

    In the statement below, is it possible to dynamically generate the $my_postid ?

    <?php
    $my_postid = 17;//This is page id or post id
    $content_post = get_post($my_postid);
    $content = $content_post->post_content;
    $content = apply_filters(‘the_content’, $content);
    $content = str_replace(‘]]>’, ‘]]>’, $content);
    echo $content;

    ?>

Viewing 12 replies - 1 through 12 (of 12 total)
  • ‘dynamically generate’ from where?

    For the current page you would include this

    global $post;
    $content = $post->post_content;
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]>', $content);
    echo $content;

    Depends what you mean by dynamically generate.

    Thread Starter GaryKay

    (@garykay)

    Hi Lee, I have a custom post type which I have populated with entries. I want to display the single entry in a modal window and not a single page(If that make sense?). I will try your suggestion and revert back to you. Thank you for your response.

    Thread Starter GaryKay

    (@garykay)

    <?php
    /*
    Template Name: Property Flo
    */
    get_header(); ?>
    
    <div class="container">
    
    <div class="row">
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <h2 class="page_title"><?php the_title(); ?></h2>
    
    </div>
    
    </div>
    
    <div class="container">
    
    <div class="row">
    <?php 
    
    // WP_Query arguments
    $args = array (
    'post_type' => 'property',
    'pagination'             => true,
    'posts_per_page'         => '10',
    'offset'				=> 0,
    'tax_query' => array(
    array(
    'taxonomy' => 'category',
    'field' => 'slug',
    'terms' => 'uncategorized'
    )
    )
    );
    
    // The Query
    $property = new WP_Query( $args );
    
    // The Loop
    if ( $property->have_posts() ) {
    while ( $property->have_posts() ) {
    $property->the_post(); ?>
    
    <?php if (in_category(array('sold'))) { ?>
    	<div id="p<?php the_title(); ?>" class="sold">
    	  <img src="<?php the_field( "property_sold" ); ?>">
    	</div>
    <?php } else { ?>
    <a href="#dialog" name="modal">
    	<div id="<?php $myid = the_ID(); ?>"  class="available">
    	  <img src="<?php the_field( "property_available" ); ?>">
    	</div>
    </a>
    
    <div id="boxes">
    
    <div id="dialog" class="window">
    <?php
    				$my_postid = $myid;//This is page id or post id
    				$content_post = get_post($my_postid);
    				$content = $content_post->post_content;
    				$content = apply_filters('the_content', $content);
    				$content = str_replace(']]>', ']]>', $content);
    				echo $content;
    ?>
    <a href="#"class="close"/>Close it</a>
    </div>
    
    <!-- Mask to cover the whole screen -->
      <div id="mask"></div>
    </div>
    
    <?php } $myid = the_ID(); echo $myid;?>
    
    <!-- If I echo the $myid from the id attribute it does not echo but if I echo from between the above delimiters it does echo. So, the $myid in the id attribute does not do any thing. I also changed the id attribute to reflect the ID of the post. The $myid below there also does nothing, if I replace it with the actual interger it works.-->
    
      <?php
    }
    }
    ?>
    
    </div>	
    
    </div>
    
    <?php // comments_template(); ?>
    
    <?php endwhile; endif; ?>
    
    </div>
    
    <!-- MODAL WINDOW -->
    
    <!-- END OF MODAL WINDOW -->
    
    </div>
    
    <?php get_footer(); ?>

    This is my complete script. I’m trying to get the variable $myid to collect the id of the post and display that post in a modal window. If you can help I would appreciate it

    Hi,

    Couple of things that might help. I’ll need more info of the problems etc if you need more help than this.

    the_id(); is a self echoing function. use get_the_id() if you don’t need to echo it. (if you want to assign it a variable for example)

    Also you are getting 10 posts (will be when it works) — I presume you’ll be using jquery or css to manipulate them to your display needs – a problem that you might have is that you are using the same id for each post in the loop… set an incremnting variable and append that to the id’s (or use classes if appropriate)

    As a start set $myid = ”; out of the loop.
    Then at the top of the loop set it by doing this $myid = get_the_id();
    and then use $myid wherever you need it after that.

    Thread Starter GaryKay

    (@garykay)

    Hi Lee,

    Thanks for the reply. The 10 posts are displayed on a page, when you click on the post it would normally take you to a single page. I would like it to rather display in a modal window (clients request). If I inspect the page in Chrome, I can see that each post has a different id, which is great. All I’m trying to do is display that single post in a modal window and not a single page. I was hoping that the variable $myid would carry the post id into the script I have located the the modal window (I have jQuery set in the header.php). The modal window display correctly, but with the last post and not the post that has been selected, clicked on. I appreciated your help this far.

    You’ll have repeating id’s for sure – you should only ever have 1 id the same on a page #boxes #dialog and #mask will be repeated 10 times.

    Hard to say from — save this into a new tempalte file and apply it to a test page on your site. tell me what happens.

    <?php
    /*
    Template Name: Property Flo-1
    */
    		get_header();
    ?>
    	<div class="container">
    
    		<div class="row">
    <?php
    		// WP_Query arguments
    		$args = array
    		(
    			'post_type' => 'property',
    			'pagination' => true,
    			'posts_per_page' => '10',
    			'offset' => 0,
    			'tax_query' => array
    			(
    				array
    				(
    					'taxonomy' => 'category',
    					'field' => 'slug',
    					'terms' => 'uncategorized'
    				)
    			)
    		);
    
    		// The Query
    		$property = new WP_Query( $args );
    
    		// The Loop
    		if ( $property->have_posts() )
    		{
    			$myid = '';
    			while ( $property->have_posts() )
    			{
    				$property->the_post();
    
    				$myid = get_the_id();
    
    				if (in_category(array('sold')))
    				{
    ?>					<div id="p_<?php the_title(); ?>" class="sold">
    						<img src="<?php the_field( "property_sold" ); ?>">
    					</div>
    <?php 			}
    				else
    				{
    ?>					<a href="#dialog" name="modal">
    						<div id="<?php echo $myid; ?>"  class="available">
    						  <img src="<?php the_field( "property_available" ); ?>">
    						</div>
    					</a>
    
    					<div id="boxes">
    
    						<div id="dialog" class="window">
    <?php
    							$my_postid = $myid;//This is page id or post id
    							$content_post = get_post($my_postid);
    							$content = $content_post->post_content;
    							$content = apply_filters('the_content', $content);
    							$content = str_replace(']]>', ']]>', $content);
    							echo $content;
    ?>
    							<a href="#"class="close"/>Close it</a>
    						</div>
    
    						<!-- Mask to cover the whole screen -->
    						<div id="mask"></div>
    					</div>
    
    <?php			}
    			}
    			wp_reset_postdata();
    		}
    ?>
    		</div>
    	</div>
    
    <?php
    		get_footer();
    ?>
    Thread Starter GaryKay

    (@garykay)

    HI Lee,

    Thank you for the above script. I created a new template and inserted the code above and got the same result. I think I will need to go another route for now but will keep on working on this and if I find a solution I will share.

    Thanks for your time

    yeah it’s one of those thing that a ard to test if you can’t see the html output, if you need more help link me to a page that uses the code.

    Thread Starter GaryKay

    (@garykay)

    It’s installed on a local test site, I will create a dev site and share the link with you.

    Thread Starter GaryKay

    (@garykay)

    Hi Lee,

    This is what I eventually did and it works as needed.

    <?php
    /*
    Template Name: Property 1
    */
    get_header(); ?>
    
     <div id="content" class="col-full">
    
          <div id="main-sidebar-container">    
    
                <!-- #main Starts -->
                <?php woo_main_before(); ?>
                <div id="main">
    				<?php 
    
    							// WP_Query arguments
    						$args = array (
    							'post_type' => 'property',
    							'pagination'             => true,
    							'posts_per_page'         => '6',
    							'offset'				=> '4',
    							'orderby' => 'date',
                                'order' => 'ASC',
    							'tax_query' => array(
    							array(
    								'taxonomy' => 'category',
    								'field' => 'slug',
    								'terms' => 'uncategorized'
    							)
    						)
    						);
    
    						// The Query
    						$property = new WP_Query( $args );
    
    						// The Loop
    						if ( $property->have_posts() ) {
    							while ( $property->have_posts() ) {
    								$property->the_post(); ?>
    
    								<?php if (in_category(array('sold'))) { ?>
    											    <div id="u<?php the_title(); ?>" class="sold">
    											      <img src="<?php the_field( "property_sold" ); ?>">
    											    </div>
    								<?php } else { ?>
    											<a href="#<?php the_ID(); ?>" id="u<?php the_title(); ?>"  name="modal" class="available">
    											      <img src="<?php the_field( "property_available" ); ?>">
    											</a>
    									<?php } ?>
    
    									<div id="boxes">
    
    									<div id="<?php the_ID(); ?>" class="window">
    										<div class="window_left">
    											<h2>Unit <?php the_title(); ?></h2>
    											<p><?php the_field( "floor" ); ?></p>
    											<p><strong>Beds:</strong> <?php the_field( "beds" ); ?></p>
    											<p><strong>Baths:</strong> <?php the_field( "baths" ); ?></p>
    											<p><strong>Estimated Levy:</strong> R<?php the_field( "levy" ); ?></p>
    											<p><strong>Internal Area:</strong> <?php the_field( "area" ); ?>m&sup2;</p>
    											<p><strong>Patio Area:</strong> <?php the_field( "patio" ); ?>m&sup2;</p>
    											<p><strong>Courtyard Area:</strong> <?php the_field( "courtyard" ); ?>m&sup2;</p>
    											<p><strong>Total Area:</strong> <?php the_field( "total_area" ); ?>m&sup2;</p>
    											<hr>
    											<p><strong>Price:</strong> R<?php the_field( "price" ); ?></p>
    											<hr>
    											<a href="<?php the_field( "unit_pdf" ); ?>" target="_blank" class="pdf_download">Unit layout.pdf</a>
    
    											<img src="<?php the_field( "position" ); ?>">
    										</div>
    										<div class="window_right">
    											<img src="<?php the_field( "unit_plan" ); ?>">
    										</div>
    									<a href="#"class="close"/>X</a>
    									</div>
    
    									<!-- Mask to cover the whole screen -->
    									  <div id="mask"></div>
    									</div>
    
    											  <?php
    						}
    					}
    				?>
    
    <div id="floorplan_bg">
    
    		<img src="<?php bloginfo('template_url'); ?>/../canvas-child/imgs/upton_first.jpg">
    
    </div>
    </div><!-- /#main -->
                <?php woo_main_after(); ?>
    
                <?php get_sidebar(); ?>
    
        </div><!-- /#main-sidebar-container -->         
    
        <?php get_sidebar( 'alt' ); ?>
    
        </div><!-- /#content -->
    
    <?php get_footer(); ?>

    http://ashburn.co.za , check out the first floor ect.

    Thank you so much for your input

    cool glad you got it sorted – looks good.

    Try and avoid using id’s for elements in loops, In this case you get away with it but it’s good practice not to – you end up with duplicate id’s – for example you have 6 id=boxes you also have divs and href’s that share identical id’s, either append something to them or use classes.

    One other last little tip, you are using bloginfo(‘template_url’); — this is the parent themes folder – use something like bloginfo(‘stylesheet_directory’) instead. That will get you straight into the child theme., so instead of…

    src=”<?php bloginfo(‘template_url’); ?>/../canvas-child/imgs/upton_first.jpg”

    you could just do…

    src=”<?php bloginfo(‘stylesheet_directory’); ?>/imgs/upton_first.jpg”

    Again looks great.

    Lee.

    Thread Starter GaryKay

    (@garykay)

    Thanks Lee,

    Will take that advise and implement it

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘my_postid’ is closed to new replies.