Monday, 14 December 2020

115. How to create custom post type in wordpress?

 // Our custom post type function

function create_posttype() {
 
    register_post_type( 'movies',
    // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Movies' ),
                'singular_name' => __( 'Movie' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'movies'),
            'show_in_rest' => true,
 
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );

114.How to Display wordpress category name with the related posts?

<?php
$categories = get_terms( array( 'taxonomy' => 'category' ) );
foreach( $categories as $cat ) :
    $posts = new WP_Query( array(
        'post_type'     => 'dt_portfolio',
        'showposts'     => -1,
        'tax_query'     => array(
                               array(
                                   'taxonomy' => 'category',
                                   'terms'    => array( $cat->term_id ),
                                   'field'   => 'term_id'
                               )

                          )
    ) ); ?>

    <h3><?php echo $cat->name; ?></h3>

    <ul>
        <?php while( $posts->have_posts() ) : $posts->the_post(); ?>
            <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
        <?php endwhile; wp_reset_postdata(); ?>
    </ul>
 

<?php endforeach; ?>  

Wednesday, 9 December 2020

113.how to create custom post type and meta field in wordpress?

  function create_posttype() {


    register_post_type'about',
    // CPT Options
    array(
      'labels' => array(
       'name' => __'About' ),
       'singular_name' => __'About' )
       
      ),
     // 'supports' => array('title', 'editor', 'thumbnail'),
      'public' => true,
      'has_archive' => false,
      'rewrite' => array('slug' => 'about'),
     )
    );
   
    }
    // Hooking up our function to theme setup
    add_action'init''create_posttype' );


    add_theme_support('post-thumbnails');
    add_post_type_support'about''thumbnail' );


    function add_description_meta_boxes() {
      add_meta_box("about_description_meta""Description""add_description_meta_box""about""normal""low");
    }
    
    function add_description_meta_box()
    {
      global $post;
      $custom = get_post_custom$post->ID );
     
      ?>
      <style>.width99 {width:99%;}</style>
      <p>
        <label>Description:</label><br />
        <textarea rows="5" name="description" class="width99"><?= @$custom["description"][0?></textarea>
      </p>
    
      <?php
    }
    
    function save_description_custom_fields(){
      global $post;
     
      if ( $post )
      {
        update_post_meta($post->ID"description", @$_POST["description"]);
      }
    }
    add_action'admin_init''add_description_meta_boxes' );
    add_action'save_post''save_description_custom_fields' );