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

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

仿站准备:

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

wordpress代码以及释义如下:

  1. <?php bloginfo('template_directory'); ?> 模板目录了路径
  2. <?php bloginfo('name'); ?>网站名
  3. <?php bloginfo( 'stylesheet_url' ); ?>样式表路径
  4. <?php wp_nav_menu(array() );?>菜单的导航列表,这个列表是后台里面:外观-》菜单-》菜单结构里面的内容,这个列表包含所有的子列表

在这里插入图片描述
5. <?php wp_list_categories('orderby=name&include=3,5,9,16'); ?>
显示分类目录下面的ID 为 3,5,9,16的目录,包括子目录也会有下级目录,这个目录就是后台的:文章-》分类目录
在这里插入图片描述
6. <?php wp_list_categories('orderby=name&include=3,5,9,16&title_li='); ?> 这个就把5的“分类目录”几个字去掉了
在这里插入图片描述
7. <?php wp_list_categories('orderby=id&show_count=1&use_desc_for_title=0&child_of=9&title_li=产品'); ?>//show_count=1显示文章数量;//use_desc_for_title=0不显示标题描述
//child_of=9显示ID为9的子类//&title_li=产品显示分类目录的名称为产品在这里插入图片描述
8. 调用某个页面的链接并且链接的名称为空(为空自动将页面名称链接显示)

<?php wp_list_pages('include=51&title_li=' ); ?>

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

<li>
                       <?php wp_list_categories('include=9&title_li=&style='); ?>	
                  	     <ul>
                  			    <?php wp_list_categories('child_of=9&title_li=');  //'orderby=id&use_desc_for_title=1&child_of=9&title_li=产品'?>
                  		</ul>
                 </li>

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

  1. 显示目录id对用的链接以及目录名
<a href="<?php echo get_category_link($cat_id);?>">
<?php echo get_cat_name($cat_id);?></a> 
  1. 根据分类别名显示链接和名称
$cat_id = get_category_by_slug('products');
echo get_category_link($cat_id->term_id);
  1. 根据页面ID获取页面相关内容
<?php 
			$page_id=143;
			$page_data = get_page($page_id);
			$pageUrl = $page_data->guid;
			$pageTitle = $page_data->post_title;
			$pageContent = $page_data->post_content;
			?>
  1. 文章标题截断20个字符
<?php
    get_posts( $args ); 
    echo wp_trim_words( get_the_title(), 20 );
?>
  1. 根据news目录别名获取文章
<?php 
			$cat_id = get_category_by_slug('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 ); ?>
			     <?php echo the_permalink(); ?>
			     <?php echo wp_trim_words( get_the_title(), 20 );?></a></li>
			     <?php endforeach; 
					endif; ?>
  1. 在目录分类页面下,直接用$cat就可以知道该分类的id号!
  2. 推荐:直接分类的内容列表,并进行分页处理
<?php 
 if( have_posts() ) : while( have_posts() ) : the_post(); 
?>

<?php echo the_permalink(); ?>

<?php endwhile; ?>
<?php the_posts_pagination( array(
'mid_size' => 3,
'prev_text' =>'上一页',
'next_text' =>'下一页',
'before_page_number' => '<span class="meta-nav screen-reader-text">第 </span>',
'after_page_number' => '<span class="meta-nav screen-reader-text"> 页</span>',
) ); ?>
					<?php endif; ?>
  1. 进行条件查询后要将条件清除,否则在single.php里面调用<?php the_title(); ?>等都不会正确,清除的方法<?php wp_reset_query();?>见下:
<?php 
   $cat_id = get_category_by_slug('hot-products')->term_id;
   $args=array(
        					'category' => $cat_id,
        					'numberposts' => '4'
        					);
   $posts = get_posts( $args ); 
   if( $posts ) :  foreach( $posts as $post ) : setup_postdata( $post ); ?>
<?php echo the_permalink(); ?>
<?php endforeach; endif; ?>
<?php wp_reset_query();?>
  1. single.php就是默认调用的文章详情页,在用户点击文章链接后进入,要想获得具体文章的内容或者其他,直接调用如下函数,不需要设置条件就可以直接调用,调用不成功可能是上面我说的查询干扰
调用文章标题:<?php the_title(); ?>
 
调用文章内容:<?php the_content(); ?>
 
调用文章摘要:<?php the_excerpt(); ?>
 
调用作者姓名:<?php the_author(); ?>
 
调用文章发布时间:<?php the_time(); ?>
  1. 获取分类以及子分类,这个是一连串显示二级目录,三级目录,不能区分,直接看下面一个改进型的,可以用于sidebar
<?php
$args=array(
    'orderby' => 'name',
    'order' => 'ASC'
);
$categories=get_categories($args);
foreach($categories as $category) {
    echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
    echo '<p> Description:'. $category->description . '</p>';
    echo '<p> Post Count: '. $category->count . '</p>';
}
?>
  1. 可以直接用于sidebar的分类,可以遍历父子分类并用双循环将父子目录关系呈现,推荐用这个,虽然有些复杂。。。
<?php
$cat_id = get_category_by_slug('products')->term_id;
//echo $cat_id;
$args=array(
    
	'child_of'=> $cat_id
);
$categories=get_categories($args);
foreach($categories as $category) {
	//echo $category->category_parent.'<br>';
	if($category->category_parent == $cat_id){ 
	
	//如果他的父类为product 产品,那么他就是二级分类
		?>
		<dt class="part2" id="part1-id4"><a style="cursor:pointer;"><?php echo get_cat_name($category->term_id);?>
		</a></dt>
		<dd class="part3dom" style="padding-left:0px; margin-left:0px;">
		<?php 
		$childfather_id =  $category->term_id;
		$child_args = array('child_of'=>$childfather_id);
		$childs = get_categories($child_args);
		foreach($childs as $child) {?>
		
			<h4 class="part3 dd" id="part2-id42"><a href="<?php echo get_category_link($child->term_id);?>">
			<?php echo get_cat_name($child->term_id);?></a></h4>
			<?php
		}
		?>
		</dd><?php
		
				}
}
		?>
  1. 关于目录文章列表的展示,还是用get_posts()函数比较好,不容易错,见代码,以下代码包含分类目录
<?php 
		//echo $cat;
				$args=array(
				'category' => $cat,
				'orderby'=>'name',
				'numberposts' => '12'
				);
				$posts = get_posts($args); 
                if( $posts ) : 
                foreach( $posts as $post ) : setup_postdata( $post ); 
				//echo the_permalink();
				?>
		            <li data-id="id-1" class="grid_3 prolist"  data-type="webdesign">
            <!-- portfolio figure with animation start-->
            <figure class="img-styled sliding">
                <div class="img-container">                   
                    <!-- img start -->
                    <img src="<?php echo get_first_image(); ?>"  alt="<?php echo wp_trim_words( get_the_title(), 20 );?>"/><!-- img end -->

                    <!-- img hover effect and lightbox start -->
                    <ul class="img-hover">
                        <li class="title">
                            <a href="<?php echo the_permalink(); ?>"><?php echo wp_trim_words( get_the_title(), 20 );?></a>
                        </li>
                        <li class="portfolio-single">
                            <a  href="<?php echo the_permalink(); ?>">single</a>
                        </li>
                    </ul><!-- img hover effect and lightbox end -->
                </div>
                <!-- portfolio image description start -->
                <figcaption>
                    <p><?php echo wp_trim_words( get_the_title(), 20 );?></p>
                </figcaption><!-- portfolio image description end -->
            </figure>
        </li>
		           <?php endforeach;  ?>
				   <div class="clear"></div>
				   <div><center><div class="msdn">
				   <?php the_posts_pagination( array(			   
						'mid_size' => '12',
						'prev_text' =>'PRE',
						'next_text' =>'NEXT',
						'before_page_number' => '<span class="meta-nav screen-reader-text"> </span>',
						'after_page_number' => '<span class="meta-nav screen-reader-text"> </span>',
	) ); ?>
					</div> </center></div>
				<?php endif; ?>
  1. 添加第一张图片,缩略图,在function.php里面添加如下代码
<?php 
    	function get_first_image() {
     	global $post;
     	$first_img = '';
    	ob_start();
     	ob_end_clean();
     	$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/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;
     }
 ?>

再在目录列表中调用

<?php echo get_first_image(); ?>

显示第一张图片

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值