• I found this code in somewhere. The purpose of this code is listing a list post in the same series; like when reading a book we have chapter 1, 2. 3….

    Chapter 1 will be posted in Post type
    Chapter 2 will be posted in Story type
    Chapter 3 will be posted in Story type
    Chapter 4 will be posted in Story type
    Chapter 5 will be posted in Story type

    What I want is when we are reading chapter 1 there will be a list of all other chapter. When we are reading chapter 2 there will also be a list of on other chapter. So far, this code can only do listing post for Post type but unable to make a list for Story type. Hope you can help me to solve it. ( show list post in the same series in story type)

    Create a custom post type.

    <?php
    add_action( 'init', 'create_post_type' );
    function create_post_type() {
    register_post_type( 'story',
    array(
    'labels' => array(
    'name' => 'Story',
    'singular_name' => 'Story'
    ),
    'show_ui' => true,
    'public' => true,
    'has_archive' => true,
    'taxonomies'    => array('post_tag'), // n?u các b?n mu?n tag cho các ch??ng c?a mình
    'rewrite' => array('slug' => 'story', 'with_front' => FALSE),
    'supports' => array( 'title', 'editor', 'author', 'excerpt', 'comments' )
    )
    );
    }
    ?>

    Link story to post

    <?php
    add_action( 'edit_form_after_title', 'mystoryparrent' );
    function mystoryparrent( $post_data = false ) {
    $scr = get_current_screen();
    $value = '';
    if ( $post_data ) {
    $t = get_post($post_data);
    $a = get_post($t->post_parent);
    $value = $a->post_title;
    }
    if ($scr->id == 'story')
    echo '<label>Thu?c truy?n: <input type="text" name="parent" value="'.$value.'" /></label> (Tên c?a cu?n truy?n g?c)<br /><br />';
    }
    ?>

    Get value for story from post

    <?php
    add_action( 'save_post', 'save_mystory' );
    function save_mystory( $post_id ) {
    $story = isset( $_POST['parent'] ) ? get_page_by_title($_POST['parent'], 'OBJECT', 'post') : false ;
    if ( ! wp_is_post_revision( $post_id ) && $story ){
    remove_action('save_post', 'save_mystory');
    $postdata = array(
    'ID' => $_POST['ID'],
    'post_parent' => $story->ID
    );
    wp_update_post( $postdata );
    add_action('save_post', 'save_mystory');
    }
    }
    ?>

    Function to show list post in single.php
    `<?php
    function get_dropdown_part( $id ) {
    global $post, $wpdb;
    $query = $wpdb->get_results(sprintf(“select * from %s where post_type = ‘%s’ and post_parent = %d and post_status = ‘%s'”, $wpdb->posts, ‘story’, $id, ‘publish’));
    if ($query) {
    echo ‘

      ‘;
      foreach ( $query as $k ) {
      $uri = get_permalink($k->ID);
      if ( ! preg_match(‘/.*page-[0-9].*/’, $uri))
      echo ‘

    • ‘.$k->post_title .’
    • ‘;
      }
      echo ‘

    ‘;
    }
    }
    ?>’
    Demo: https://truyenmoi.mobi/bi-mat-kinh-hoang-trong-quan-net/

  • The topic ‘Chapter for Reading story online’ is closed to new replies.