点击缩略图获取帖子ID

时间:2013-08-03 作者:BDGapps

我有点迷路了,所以如果这个问题含糊不清,没有组织好,我很抱歉。我目前有一个通过Wordpress填充的缩略图网格。我通过获取一个类别中的所有帖子并获取它们的缩略图来检索它们。我现在想要它,所以当你点击缩略图时,它会在一个文本框中拉出帖子的正文,我在与网格相同的页面上。我正在寻找一个方向,告诉我单击帖子时如何获取帖子id,然后如何在文本框中加载来自该id的正文文本。

缩略图网格:

foreach  ( $categories as $category ) {
  $i = -1;
  echo \'<div class="grid-row"><h2>\' . $category->name . \'</h2></div>\';
  $args = array( \'posts_per_page\' => -1, \'cat\' => $category->term_id );
  $cat_posts = new WP_Query($args);
  if ( $cat_posts->have_posts() ) : while ( $cat_posts->have_posts() ) :
    $i++;
    $cat_posts->the_post();
    $face = get_field( \'face\' );
    $name = get_field( \'fullname\' );
    if ( $i % 6 == 0 ) echo \'<div class="grid-row">\';
    echo \'<div class="obj">\';
    echo \'<div class="faceThumb">\';
    echo wp_get_attachment_image($face);
    echo \'</div><div class="name">\' . $name . \'</div></div>\';
    if ( ($i % 6 == 5) || $i == ($cat_posts->post_count - 1) ) echo \'</div>\';
  endwhile; endif;
}
wp_reset_postdata();
?>
UPDATED FOR POST BELOW//指数

        <div id="PersonBio">this is my bio</div>
          <div id="mainTextTitle">
                                      <?php the_title(); ?>

          </div>
              <div id="mainText">

                <ul class="faces">



<?php
$categories = get_categories( \'child_of=2\' ); 

foreach  ( $categories as $category ) {
  $i = -1;
  echo \'<div class="grid-row"><h2>\' . $category->name . \'</h2></div>\';
  $args = array( \'posts_per_page\' => -1, \'cat\' => $category->term_id );
  $cat_posts = new WP_Query($args);
  if ( $cat_posts->have_posts() ) : while ( $cat_posts->have_posts() ) :
    $i++;
    $cat_posts->the_post();
    $face = get_field( \'face\' );
    $name = get_field( \'fullname\' );
    if ( $i % 6 == 0 ) echo \'<div class="grid-row">\';
    echo \'<div class="obj" data-id="<?php the_ID(); ?>">\';
    echo \'<div class="faceThumb">\';
    echo wp_get_attachment_image($face);
    echo \'</div><div class="name">\' . $name . \'</div></div>\';
    if ( ($i % 6 == 5) || $i == ($cat_posts->post_count - 1) ) echo \'</div>\';
  endwhile; endif;
}
wp_reset_postdata();
?>    </ul>




  </div>
    </div>
  </div>
</div>
//函数

add_action(\'wp_ajax_select_face_post\', \'select_face_post\');
add_action(\'wp_ajax_nopriv_select_face_post\', \'select_face_post\');
add_action(\'wp_enqueue_scripts\', \'faces_posts_scripts\');

function select_face_post () {
  error_reporting(0);
  if ( ! isset($_POST[\'postid\']) || ! intval($_POST[\'postid\']) ) die();
  if ( ! isset($_POST[\'nonce\']) || ! wp_verify_nonce($_POST[\'nonce\'],\'postface\') ) die();
  $post = get_post($_POST[\'postid\']);
  if ( empty($post) || ! is_object($post) ) die();   
  header(\'Cache-Control: no-cache, must-revalidate\');
  header(\'Expires: Mon, 26 Jul 1997 05:00:00 GMT\');
  header(\'Content-type: application/json\');
  echo json_encode( $post );
  die();
}

function faces_posts_scripts() {
  // replace \'all_faces\' with the slug of page in which resides the code you posted
  if ( is_page(\'team\') ) { 
    wp_register_script( \'postface\', get_template_directory_uri . \'Team/postface.js\', array(\'jquery\'), NULL, true );     
    $url = admin_url(\'admin-ajax.php\');
    $nonce = wp_create_nonce(\'postface\');
    wp_localize_script( \'postface\', \'postface_vars\', array(\'ajaxurl\' => $url, \'nonce\' => $nonce) );
    wp_enqueue_script( \'postface\' );
  }
}
//js

// postface.js
jQuery(document).ready(function($) {
  $(document).on(\'click\', \'div.obj\', function() {
    var selectedpost = $(this).data(\'id\');
    if ( selectedpost > 0) {
       $.ajax({
        type: "POST",
        url: postface_vars.ajaxurl,
        dataType: \'json\',
        data: { action: \'select_face_post\', nonce: postface_vars.nonce, postid: selectedpost }
       }).done( function(data) {
        if (data.post_content) {
          // var id =  data.ID;
          // var title = data.post_title;
          $(\'#PersonBio\').html(data.post_content);
        }
      });
    }
  });
});
enter image description here

1 个回复
最合适的回答,由SO网友:gmazzap 整理而成

您需要ajax。

html:

<div id="PersonBio"></div>
<div id="mainTextTitle"></div>

<div id="mainText">
  <ul class="faces">
  <?php
  $categories = get_categories();
  foreach  ( $categories as $category ) {
    $i = -1;
    echo \'<div class="grid-row"><h2>\' . $category->name . \'</h2></div>\';
    $args = array( \'posts_per_page\' => -1, \'cat\' => $category->term_id );
    $cat_posts = new WP_Query($args);
    if ( $cat_posts->have_posts() ) : while ( $cat_posts->have_posts() ) :
      $i++;
      $cat_posts->the_post();
      $face = get_field( \'face\' );
      $name = get_field( \'fullname\' );
      if ( $i % 6 == 0 ) echo \'<div class="grid-row">\';
      echo \'<div class="obj" data-id="\' . get_the_ID() . \'">\';
      echo \'<div class="faceThumb">\';
      echo wp_get_attachment_image($face);
      echo \'</div><div class="name">\' . $name . \'</div></div>\';
      if ( ($i % 6 == 5) || $i == ($cat_posts->post_count - 1) ) echo \'</div>\';
    endwhile; endif;
  }
  wp_reset_postdata();
  ?>
  </ul>
</div>

英寸functions.php

add_action(\'wp_ajax_select_face_post\', \'select_face_post\');
add_action(\'wp_ajax_nopriv_select_face_post\', \'select_face_post\');
add_action(\'wp_enqueue_scripts\', \'faces_posts_scripts\');

function select_face_post () {
  error_reporting(0);
  if ( ! isset($_POST[\'postid\']) || ! intval($_POST[\'postid\']) ) die();
  if ( ! isset($_POST[\'nonce\']) || ! wp_verify_nonce($_POST[\'nonce\'],\'postface\') ) die();
  $post = get_post($_POST[\'postid\']);
  $face = get_field( \'face\',  $post->ID );
  $name = get_field( \'fullname\',  $post->ID );
  $post->face_field = $face;
  $post->name_field = $name;
  if ( empty($post) || ! is_object($post) ) die();   
  header(\'Cache-Control: no-cache, must-revalidate\');
  header(\'Expires: Mon, 26 Jul 1997 05:00:00 GMT\');
  header(\'Content-type: application/json\');
  echo json_encode( $post );
  die();
}

function faces_posts_scripts() {
  if ( is_page(\'team\') ) { 
    wp_register_script( \'postface\', get_template_directory_uri() . \'/Team/postface.js\', array(\'jquery\'), NULL, true );     
    $url = admin_url(\'admin-ajax.php\');
    $nonce = wp_create_nonce(\'postface\');
    wp_localize_script( \'postface\', \'postface_vars\', array(\'ajaxurl\' => $url, \'nonce\' => $nonce) );
    wp_enqueue_script( \'postface\' );
  }
}
然后创建一个名为“postface”的文件。js’并将以下代码放在那里
jQuery(document).ready(function($) {
  $(document).on(\'click\', \'div.obj\', function() {
    var selectedpost = $(this).data(\'id\');
    if ( selectedpost > 0) {
       $.ajax({
        type: "POST",
        url: postface_vars.ajaxurl,
        dataType: \'json\',
        data: { action: \'select_face_post\', nonce: postface_vars.nonce, postid: selectedpost }
       }).done( function(data) {
        if (data) {
          // var id =  data.ID;
          $(\'#mainTextTitle\').html(data.name_field);
          $(\'#PersonBio\').html(data.face_field + data.post_content);
        }
      });
    }
  });
});
有关信息,请参见This very useful Smashing Magazine tutorial
  • Codex about ajax in wordpress
  • Codex for wp_ajax_{action} hook
  • Codex for wp_localize_script
  • 结束

    相关推荐

    AJAX-使用一个PHP函数返回两个JSON对象

    如何使用一个AJAX调用和PHP函数返回两个JSON对象?非常感谢您的帮助!下面是PHP函数:function get_ldap_attr() { header(\"Content-type: application/json\"); $lan = $_POST[\'lan\']; $dn = get_site_option ( \"ldapServerOU\" ); $usr = get_site_option ( \"ldapServer