使用wordpress仿站的php代码 2019年1月15日

最近帮朋友仿站,对wordpress的代码调用做一个记录,以备后面使用。

仿站准备:

  1. 使用仿站小工具 V8.2对目标网站进行页面下载
  2. 在wordpress的wp-content\themes目录下新建文件夹
  3. 创建一些php文件。

wordpress代码以及释义如下:

  1. 模板目录了路径
  2. 网站名
  3. 样式表路径
  4. 菜单的导航列表,这个列表是后台里面:外观-》菜单-》菜单结构里面的内容,这个列表包含所有的子列表

使用wordpress仿站的php代码 2019年1月15日_第1张图片
5.
显示分类目录下面的ID 为 3,5,9,16的目录,包括子目录也会有下级目录,这个目录就是后台的:文章-》分类目录
使用wordpress仿站的php代码 2019年1月15日_第2张图片
6. 这个就把5的“分类目录”几个字去掉了
在这里插入图片描述
7. //show_count=1显示文章数量;//use_desc_for_title=0不显示标题描述
//child_of=9显示ID为9的子类//&title_li=产品显示分类目录的名称为产品使用wordpress仿站的php代码 2019年1月15日_第3张图片
8. 调用某个页面的链接并且链接的名称为空(为空自动将页面名称链接显示)


在这里插入图片描述
10. 以下为产品分类的导航

  • 由于php wp_list_categories 的child_of只能是子目录的链接,所以加上ul li以及wp_list_categories(‘include=9&title_li=&style=’); 将主链接调取出来

    1. 显示目录id对用的链接以及目录名
    
     
    
    1. 根据分类别名显示链接和名称
    $cat_id = get_category_by_slug('products');
    echo get_category_link($cat_id->term_id);
    
    1. 根据页面ID获取页面相关内容
    guid;
    			$pageTitle = $page_data->post_title;
    			$pageContent = $page_data->post_content;
    			?>
    
    1. 文章标题截断20个字符
    
    
    1. 根据news目录别名获取文章
    term_id;
    			$args=array(
    					'category' => $cat_id,
    					'numberposts' => '4',
    					'order'=>'date'
    			);
    					$posts = get_posts( $args ); 
                        if( $posts ) : 
                        foreach( $posts as $post ) : setup_postdata( $post ); ?>
    			     
    			     
    			     
    
    1. 在目录分类页面下,直接用$cat就可以知道该分类的id号!
    2. 推荐:直接分类的内容列表,并进行分页处理
    
    
    
    
    
     3,
    'prev_text' =>'上一页',
    'next_text' =>'下一页',
    'before_page_number' => '',
    'after_page_number' => '',
    ) ); ?>
    					
    
    1. 进行条件查询后要将条件清除,否则在single.php里面调用等都不会正确,清除的方法见下:
    term_id;
       $args=array(
            					'category' => $cat_id,
            					'numberposts' => '4'
            					);
       $posts = get_posts( $args ); 
       if( $posts ) :  foreach( $posts as $post ) : setup_postdata( $post ); ?>
    
    
    
    
    1. single.php就是默认调用的文章详情页,在用户点击文章链接后进入,要想获得具体文章的内容或者其他,直接调用如下函数,不需要设置条件就可以直接调用,调用不成功可能是上面我说的查询干扰
    调用文章标题:
     
    调用文章内容:
     
    调用文章摘要:
     
    调用作者姓名:
     
    调用文章发布时间:
    
    1. 获取分类以及子分类,这个是一连串显示二级目录,三级目录,不能区分,直接看下面一个改进型的,可以用于sidebar
     'name',
        'order' => 'ASC'
    );
    $categories=get_categories($args);
    foreach($categories as $category) {
        echo '

    Category: name ) . '" ' . '>' . $category->name.'

    '; echo '

    Description:'. $category->description . '

    '; echo '

    Post Count: '. $category->count . '

    '; } ?>
    1. 可以直接用于sidebar的分类,可以遍历父子分类并用双循环将父子目录关系呈现,推荐用这个,虽然有些复杂。。。
    term_id;
    //echo $cat_id;
    $args=array(
        
    	'child_of'=> $cat_id
    );
    $categories=get_categories($args);
    foreach($categories as $category) {
    	//echo $category->category_parent.'
    '; if($category->category_parent == $cat_id){ //如果他的父类为product 产品,那么他就是二级分类 ?>
    term_id);?>
    term_id; $child_args = array('child_of'=>$childfather_id); $childs = get_categories($child_args); foreach($childs as $child) {?>

    term_id);?>

    1. 关于目录文章列表的展示,还是用get_posts()函数比较好,不容易错,见代码,以下代码包含分类目录
     $cat,
    				'orderby'=>'name',
    				'numberposts' => '12'
    				);
    				$posts = get_posts($args); 
                    if( $posts ) : 
                    foreach( $posts as $post ) : setup_postdata( $post ); 
    				//echo the_permalink();
    				?>
    		            
  • '12', 'prev_text' =>'PRE', 'next_text' =>'NEXT', 'before_page_number' => ' ', 'after_page_number' => ' ', ) ); ?>
    1. 添加第一张图片,缩略图,在function.php里面添加如下代码
    /i', 			  $post->post_content, $matches);
         	$first_img = $matches [1] [0];
        	if(empty($first_img)){ //Defines a default image
           $first_img = bloginfo('template_url') .   "/static/images/default.jpg";
         };
         return $first_img;
         }
     ?>
    

    再在目录列表中调用

    
    

    显示第一张图片

    你可能感兴趣的:(Wordpress)