简体   繁体   English

您如何将整个文章打印到页面中(wordpress)

[英]How do you print an entire post into a page (wordpress)

Hi I've made a custom post type 'work_fields' that calls in information from yet another custom post type 'members' into the post, and now I'm trying to make a PAGE TEMPLATE that shows a list of the titles of custom post type 'work_fields', and when you click a title, the whole post('work_fields') will show up on a div called 'single-post-container' below the titles. 嗨,我制作了一个自定义帖子类型“ work_fields”,该信息从另一个自定义帖子类型“ members”中调用信息,现在,我试图制作一个显示自定义帖子标题列表的PAGE TEMPLATE键入“ work_fields”,然后单击标题,整个帖子(“ work_fields”)将显示在标题下方名为“ single-post-container”的div上。 right now I've got everything working fine, but I want to display a post in the div 'single-post-container' when the page loads. 现在我一切正常,但是我想在页面加载时在div“ single-post-container”中显示一个帖子。 (as of now, just the titles of the posts are displayed and there is nothing in the div). (到目前为止,仅显示帖子的标题,而div中没有​​任何内容)。 How do I get the div to display the most recent post of custom post type 'work_fields' on page load? 如何使div在页面加载时显示自定义帖子类型“ work_fields”的最新帖子? This is the code for the custom page template. 这是自定义页面模板的代码。

<div class="row">
  <div class="small-12 medium-10 large-offset-1 columns">
    <h2><?php the_title(); ?></h2>
  </div>
</div>  
<div class="row halfsection">
  <div class="small-12 medium-10 large-offset-1 columns">
    <div class="category_container">
    <?php
        $args = array('post_type' => 'work_fields',);
        $query = new WP_Query( $args );
    ?>

    <?php if ( $query->have_posts() ) : ?>
      <?php while ( $query->have_posts() ) : $query->the_post(); ?>  
        <p class="category_item"><a class="post-link" rel="<?php the_ID(); ?>" href="<?php the_permalink(); ?>"><?php  echo get_the_title(); ?></a></p> 
    <?php endwhile; endif; ?>
    </div>
  </div>
</div>
<div class="row">
  <div class="small-12 medium-10 large-offset-1 columns">
    <hr>
  </div>
</div>                      
<div id="single-post-container">
    //THIS IS WHERE THE POST CONTENTS SHOWS BUT I WANT THE MOST RECENT POST TO BE HERE ON PAGE LOAD, BEFORE I CLICK ANY OTHER POST//    
</div>

Thank you! 谢谢! Your help is much appreciated! 非常感谢您的帮助!

Just use the WP_query twice by getting recent posts in the arguments, 通过在参数中获取最新帖子,只需使用WP_query两次,

$args2 = array('post_type' => 'work_fields', 'orderby' => 'ID', 'order'=> 'DESC' , 'posts_per_page' => 5);
$query2 = new WP_Query( $args2 );
?><div id="single-post-container"><?php
// The Loop
if ( $query2->have_posts() ) {
    echo '<ul>';
    while ( $query2->have_posts() ) {
        $query2->the_post();
        echo '<li>' . get_the_content() . '</li>';
    }
    echo '</ul>';
    /* Restore original Post Data */
    wp_reset_postdata();
}
?></div><?php

Put the div outside loop. 将div外循环。 It will show the content of recent 5 posts. 它将显示最近5个帖子的内容。

If everything above the div single-post-container is working fine then for this specific div you can load most recent post by using code below. 如果div single-post-container上方的所有内容运行正常,那么对于此特定的div,您可以使用以下代码加载最新的帖子。 Be sure to reset previous post data using wp_reset_postdata() 确保使用wp_reset_postdata()重置以前的帖子数据

Codex Documentation. 食典文件。 https://codex.wordpress.org/Function_Reference/wp_get_recent_posts https://codex.wordpress.org/Function_Reference/wp_get_recent_posts

<?php
$args = array(
    'numberposts' => 1,
    'orderby' => 'post_date',
    'order' => 'DESC',
    'post_type' => 'work_fields',
    'post_status' => 'publish',
    'suppress_filters' => true
);

$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>

So I just recreated the post type markup on my page. 因此,我只是在页面上重新创建了帖子类型标记。 I'm sure there's a better way to do this but for times sake I had to at least make it work. 我敢肯定有更好的方法可以做到这一点,但有时我至少必须使其起作用。 I also tried using a jquery onclick function and just click the first title after everything loads, but there was an error that just kept pushing all of the titles so I pretty much gave up. 我还尝试使用jquery onclick函数,并在所有内容加载后单击第一个标题,但是出现了一个错误,就是不断推送所有标题,因此我几乎放弃了。 here's the code 这是代码

<div class="row">
  <div class="small-12 medium-10 large-offset-1 columns">
    <h2><?php the_title(); ?></h2>
  </div>
</div>  
<div class="row halfsection">
  <div class="small-12 medium-10 large-offset-1 columns">
    <div class="category_container">
      <?php
        $args = array(
            'post_type' => 'work_fields',
        );
        $query = new WP_Query( $args );
      ?>
      <?php if ( $query->have_posts() ) : ?>
      <?php while ( $query->have_posts() ) : $query->the_post(); ?>  
        <p class="category_item"><a class="post-link" rel="<?php the_ID(); ?>" href="<?php the_permalink(); ?>"><?php  echo get_the_title(); ?></a></p> 
      <?php endwhile; endif; ?>

    </div>
  </div>
</div>
<div class="row">
    <div class="small-12 medium-10 large-offset-1 columns">
        <hr>
    </div>
</div>                  
<div id="single-post-container">
  <?php
    $args2 = array(
    'orderby' => 'post_date',
    'order' => 'DESC',
    'post_type' => 'work_fields',
    'post_status' => 'publish',
    'posts_per_page' => 1,
    );
    $query2 = new WP_Query( $args2 );
        if ( $query2->have_posts() ) : ?>
        <?php $post = get_post($_POST['id']); ?>
        <div id="single-post post-<?php the_ID(); ?>">
        <?php while ( $query2->have_posts() ) : $query2->the_post(); ?>  
          <div class="row section">
            <div class="small-12 medium-7 large-offset-1 columns">
              <h2><?php the_title(); ?></h2>
              <h3>소개</h3>
              <p class="halfsection"><?php the_field('work_fields_intro'); ?></p>
              <h3>주요서비스</h3>
              <p class="halfsection"><?php the_field('work_fields_service'); ?></p>
              <h3>주요실적</h3>
              <p class="halfsection"><?php the_field('work_fields_accomplishment'); ?></p>
            </div>
        <?php endwhile; endif; ?>
            <div class="small-6 medium-3 large-2 columns large-offset-1 end">
              <?php 
                $posts = get_field('team_member');
                if( $posts ): ?>
                <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
                <?php setup_postdata($post); ?>
                <div class="member_container halfsection">
                    <div class="member"><?php the_post_thumbnail(); ?></div>
                        <p class="member_name"><?php the_title(); ?></p>
                            <ul class="members_info">
                                <li><?php the_field('members_position'); ?></li>
                                <li><?php the_field('members_e-mail'); ?></li>
                                <li><?php the_field('members_phone'); ?></li>
                            </ul>
                    </div>
        <?php endforeach; ?>
<?php endif; ?>
                </div>  
            </div>
          </div>
        </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM