Code cơ bản của wordpress

Chào các bạn mình xin giới thiệu 1 số code cơ bản của wordpress
Về header
<?php wp_head(); ?> // Khai báo
 <?php get_header();?> // Gọi header


Về footer 
<?php wp_footer(); ?> // Khai báo
<?php get_footer(); ?> // Gọi footer

Lấy đường dẫn
<?php echo get_template_directory_uri() ?> // Lấy đường dẫn thư mục
<?php echo get_site_url(); ?> // Lấy đường dẫn gốc của site
<a href="<?php echo get_site_url(); ?>/?page_id=1"></a> // Đường dẫn page
<?php echo ( get_the_ID() == 1 ) ? 'active':''  ?> // Gọi class active
Hàm cần có trong functions
add_filter( 'wp_default_editor', create_function('', 'return "html";') );
function get_rid_of_wpautop(){
  if(!is_singular('post')){
    remove_filter ('the_content', 'wpautop');
    remove_filter ('the_excerpt', 'wpautop');
  }
}
add_action( 'template_redirect', 'get_rid_of_wpautop' );

Gọi post và bài mặc định
// Gọi bài post mặc định
<?php $args=array('post_type' => 'post', 'orderby' => 'ID', 'order'   => 'DESC', 'posts_per_page'=>5);$query = new WP_Query( $args); ?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post();?>
// Start loop news
 <span class="date"><?php the_time('Y.m.d') ; ?></span><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
// End loop news
<?php endwhile; ?>
<?php  wp_reset_query(); ?>
<?php  endif; ?>


// Gọi chi tiết nội dung bài post
<?php if ( have_posts() ) :  ?>
<?php while ( have_posts() ) : the_post(); ?>
 
        <span class="title">
        <?php the_title(); ?>
        </span>
     
        <span class="date">
        <?php the_time('Y.m.d') ; ?>
        </span>
     
        <span class="content">
        <?php the_content();  ?>
        </span>
     
    <?php endwhile; ?>
    <?php  wp_reset_query(); ?>
<?php  endif; ?>

// Gọi bài post theo category
<?php $args=array('category_name' => 'news', 'orderby' => 'ID', 'order'   => 'DESC', 'posts_per_page'=>3);$query = new WP_Query( $args); ?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post();?>
// Start loop news
 <span class="date"><?php the_time('Y.m.d') ; ?></span><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
// End loop news
<?php endwhile; ?>
<?php  wp_reset_query(); ?>
<?php  endif; ?>


// Gọi chi tiết nội dung bài post theo category
// Tạo single.php, trong file có code này
<?php $post = $wp_query->post;
if ( in_category( 'news') ) {
 include( TEMPLATEPATH.'/single-news.php' );
}
else if ( in_category( 'category_other') ) {
include( TEMPLATEPATH.'/single-category_other.php' );
}
?>

// Tạo file single-news.php rồi bỏ code này vào
<?php if ( have_posts() ) :  ?>
<?php while ( have_posts() ) : the_post(); ?>
        <span class="title">
        <?php the_title(); ?>
        </span>
        <span class="date">
        <?php the_time('Y.m.d') ; ?>
        </span>
        <a href="<?php the_permalink(); ?>">
<?php $cat = get_the_category(get_the_ID()); $name_cat = $cat[0]->cat_name; ?> // Gọi tên category  nếu cần
</a>
        <span class="content">
        <?php the_content();  ?>
        </span>
    <?php endwhile; ?>
    <?php  wp_reset_query(); ?>
<?php  endif; ?>



cắt content
// Khai báo function này trong file function
function cuttring($str, $length)
{
$str = trim($str);
$sections = preg_split("/[n]+/", $str);
$kq="";
foreach( $sections as $value )
{
$value = trim($value);
if( $value !='' )
if(!preg_match('#(?<=<)w+(?=[^<]*?>)#', $value)){
$kq .= $value;
break;
}    
}
return mb_substr($kq,0, $length,'utf-8')."...";
}

// Gọi content và giới hạn số kí tự xuất hiện
$content_blog = get_the_content();
echo cuttring( $content_blog, 80);
SHARE

trungthan

  • Image
  • Image
  • Image
  • Image
  • Image

0 nhận xét:

Đăng nhận xét