WordPress.org

Support

Support » How-To and Troubleshooting » Pulling Date from a Specific Page

Pulling Date from a Specific Page

  • Here is the code i’m using, but i’m getting the date from the post next to it.

    <?php
    $post_id = 707; // substitute the actual post ID for "2"
    $my_post = get_post($post_id);
    
    echo "<div class='date'>";
    
    echo "<span class='month'>";
    the_time('M');
    echo "</span>";
    
    echo "<span class='day'>";
    the_date('d');
    echo "</div>";
    
    echo "<div class='blog_post_content'>";
    
    echo "<h2 class='title'>Case Study: $my_post->post_title</h2>";
    echo "<p>$my_post->post_excerpt</p>";
    
    echo "<a href='?page_id=707 ' class='read_more'>Continue reading...</a>";
    
    echo "</div>";
    
    ?>
Viewing 10 replies - 1 through 10 (of 10 total)
  • What’s the code that’s the creating the secondary loop ($my_post)?

    hmm. Thats all I have pertaining to $my_post. Below is everything above the query

    <?php $home_page_builder = $data['home_page_builder']['enabled'];
    if ($home_page_builder){
    
        foreach ($home_page_builder as $key=>$value) {
    
            switch($key) {
    
                case 'portfolio':
    
                    if( $data['portfolio_home_style'] == "style1" ){
                    ?>
    
                        <div class="portfolio_home">
    
                            <div class="container">
    
                                <?php if ( $data['portfolio_home_title'] || $data['portfolio_home_text']): ?>
                                <div class="page_desc">
                                    <?php
                                    if( $data['portfolio_home_title'] ) echo "<h2>".$data['portfolio_home_title']."</h2>";
                                    if( $data['portfolio_home_text'] ) echo "<p>".$data['portfolio_home_text']."</p>";
                                    ?>
                                </div>
                                <?php endif ?>
    
                                <ul class="clearfix"> 
    
                                    <?php
                                    $args=array(
                                        'post_type'      => 'portfolio',
                                        'post_status'    => 'publish',
                                        'posts_per_page' => $data['portfolio_home_posts_per_page'],
                                        'paged'          => $paged
                                    );
    
                                    $wp_query = new WP_Query($args);
                                    if($wp_query->have_posts()) : while($wp_query->have_posts()) : $wp_query->the_post();  
    
                                    if(has_post_thumbnail($post->ID, 'large')){$img_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');}
                                    $image = aq_resize( $img_url[0], 410, 300, true );
    
                                    $project_type = ( get_post_meta( $post->ID, '_bmd_project_type', true ) );
                                    ?>
    
                                    <li>
    
                                        <img src="<?php echo $image; ?>" alt="<?php echo get_the_title(); ?>"/>
    
                                        <?php
                                        if ( $project_type == 'image' || $project_type == 'slider' ) $p_class = "plus";
                                        if ( $project_type == 'vimeo' || $project_type == 'youtube' ) $p_class = "play";
                                        ?>
    
                                        <a href="<?php echo get_permalink(); ?>" title="<?php echo get_the_title(); ?>" class="<?php echo $p_class ?>"></a>
                                        <h2 class="title"><?php echo get_the_title(); ?></h2>
    
                                    </li>
    
                                    <?php endwhile; endif; ?>
    
                                </ul>
    
                            </div>
                        </div><!-- end .portfolio_home -->
                    <?php
                    }
    
                    if( $data['portfolio_home_style'] =="style2" ){
                    ?>
                        <div class="portfolio_home2">
                            <div class="container">
    
                                <?php if ( $data['portfolio_home_title'] || $data['portfolio_home_text']): ?>
    
                                <div class="page_desc">
                                    <?php
                                    if( $data['portfolio_home_title'] ) echo "<h2>".$data['portfolio_home_title']."</h2>";
                                    if( $data['portfolio_home_text'] ) echo "<p>".$data['portfolio_home_text']."</p>";
                                    ?>
                                </div>
                                <?php endif ?>
    
                                <div class="row">
                                    <ul  class="portfolio_post">
                                        <?php
    
                                        $args=array(
                                            'post_type'      => 'portfolio',
                                            'post_status'    => 'publish',
                                            'posts_per_page' => $data['portfolio_home_posts_per_page'],
                                            'paged'          => $paged,
                                        );
                                        $wp_query = new WP_Query($args);
    
                                        if($wp_query->have_posts()) : while($wp_query->have_posts()) : $wp_query->the_post();  
    
                                        if(has_post_thumbnail($post->ID, 'full')){$img_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');}
    
                                        if( $data['portfolio_home_col'] == 'col2' ) {
                                            $image = aq_resize( $img_url[0], 570, 400, true );
                                            $p_class = "span6";
                                        }
                                        if( $data['portfolio_home_col'] == 'col3' ) {
                                            $image = aq_resize( $img_url[0], 570, 400, true );
                                            $p_class = "span4";
                                        }
                                        if( $data['portfolio_home_col'] == 'col4' ) {
                                            $image = aq_resize( $img_url[0], 370, 300, true );
                                            $p_class = "span3";
                                        }
    
                                        $project_type = ( get_post_meta( $post->ID, '_bmd_project_type', true ) );
                                        ?>
    
                                        <li class="<?php echo $p_class ?>">
    
                                            <div class="portfolio_img">
                                                <img src="<?php echo $image; ?>" alt="<?php echo get_the_title(); ?>"/>
    
                                                <?php
                                                if ( $project_type == 'image' || $project_type == 'slider' ) $p_class = "plus";
                                                if ( $project_type == 'vimeo' || $project_type == 'youtube' ) $p_class = "play";
                                                ?>
    
                                                <a href="<?php echo get_permalink(); ?>" title="<?php echo get_the_title(); ?>" class="<?php echo $p_class ?>"></a>
                                            </div>
    
                                            <?php
                                            if( $data['portfolio_home_show_title'] == 'on' || $data['portfolio_home_show_cat'] == 'on' ){
    
                                                if ( $data['portfolio_home_show_cat'] == 'off') $h_class = "no_margin";
    
                                                echo '<div class="portfolio_desc">';
                                                if( $data['portfolio_home_show_title'] == 'on' ) echo '<h2 class="title '.$h_class.'"><a href="'.get_permalink().'">'.get_the_title().'</a></h2>';
                                                if( $data['portfolio_home_show_cat'] == 'on' ) {
                                                    echo '<span>';
                                                    echo get_the_term_list( $post->ID, 'portfolio-category', '', ' - ', '', 0 );
                                                    echo '</span>';
                                                };
                                                echo '</div>';
    
                                            } ?>
    
                                        </li>
    
                                        <?php endwhile; endif; ?>
                                    </ul><!-- end .portfolio_post -->
                                </div>
                            </div>
                        </div>
                    <?php
                    }
                break; 
    
                case 'blog':
                ?>
    
                            <?php if ( $data['blog_home_title'] || $data['blog_home_text']): ?>
                          <div class="page_desc">
                                <?php
                                if( $data['blog_home_title'] ) echo "<h2>".$data['blog_home_title']."</h2>";
                                if( $data['blog_home_text'] ) echo "<p>".$data['blog_home_text']."</p>";
                                ?>
                          </div>
                            <?php endif ?>
    
                            <div class="row">
    
                                <?php
                                $args=array(
                                    'post_type' => 'post',
                                    'post_status' => 'publish',
                                    'posts_per_page' => 2,
                                    'paged' => $paged,
                                );
                                $wp_query = new WP_Query($args);
                                if($wp_query->have_posts()) : while($wp_query->have_posts()) : $wp_query->the_post();
                                ?>
    
                                <article id="post-<?php the_ID(); ?>" <?php post_class('span4'); ?>>
                                    <?php get_template_part( 'content-home', get_post_format() ); ?>
                                </article>
    
                                <?php endwhile; endif; ?>
    
          <article id="post-678" class="post-678 post type-post status-publish format-standard hentry category-401k tag-50-employees tag-aca tag-fiscal-cliff tag-obamacare span4">                    
    
                             <?php
    $post_id = 707; // substitute the actual post ID for "2"
    $my_post = get_post($post_id);
    
    echo "<div class='date'>";
    
    echo "<span class='month'>";
    the_time('M');
    echo "</span>";
    
    echo "<span class='day'>";
    the_date('d');
    echo "</div>";
    
    echo "<div class='blog_post_content'>";
    
    echo "<h2 class='title'>Case Study: $my_post->post_title</h2>";
    echo "<p>$my_post->post_excerpt</p>";
    
    echo "<a href='?page_id=707 ' class='read_more'>Continue reading...</a>";
    
    echo "</div>";
    
    ?>

    Sorry, brainfart, it’s coming from the get_post().

    Where are you getting the 707? You’re saying that the template tags used after it are not retrieving information from the post with id 707?

    707 is the id of the page i’m pulling the excerpt, title and date from. But for some reason my code is pulling the date from the post/query next to it. Which are posts, this block has it’s own query because it’s pulling a particular page.

    Try putting a wp_reset_postdata(); above the $post_id = 707; line.

    Beyond that, I don’t know, I’ve never experienced any issues with template tags where they were pulling correct information some of the time and incorrect information other times.

    Thanks for sending that over. I gave that a try and it’s throwing the following error:

    Fatal error: Function name must be a string

    Try copy pasting the following over the relevant lines:

    <?php
    wp_reset_postdata();
    $post_id = 707; // substitute the actual post ID for "2"
    $my_post = get_post($post_id);

    strangely enough, that gave me the date previous to the one it was originally giving me.

    Soooo.. the right date?

    No, it’s still the wrong date, now it’s pulling from block number 1 instead of 2 when it should be pulling 3

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Pulling Date from a Specific Page’ is closed to new replies.
Skip to toolbar