建站教程

建站教程

Products

当前位置:首页 > 建站教程 >

wordpress前端主循环,调文章评论,分页,前台直接到后台编辑(WordPress主循环The Loop)

GG网络技术分享 2025-03-18 16:13 0


wordpress前端主循环,调文章评论,分页,前台直接到后台编辑

前端开发

index.php文件:

<!DOCTYPE html>

<html>

<head>

<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />

<title><?php wp_title(); ?></title>

</head>

<body>

<?phpif(have_posts()):while(have_posts()):the_post();?><!--主循环-->

<h3><?php the_title();?></h3><!--文章标题-->

<p><?php comments_popup_link(\'零条评论\',\'只有一条评论\',\'%条评论\',\'css\',\'不允许评论\');?></p>

<p><?php edit_post_link(\'修改文章\');?></p><!--从前台直接跑到后台编辑,需登录管理员账号-->

<div><?php the_content();?></div><!--文章内容-->

<?php endwhile;?>

<?php endif;?>

<!--分页-->

<div><!--下一页--><?php next_posts_link(\'下一页 》》\',3);?> <!--上一页--><?php previous_posts_link(\'《《 上一页\');?></div>

index.php

</body>

</html>


single文件:

<!DOCTYPE html>

<html>

<head>

<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />

<title><?php wp_title(); ?></title>

</head>

<body>

<?php

if(have_posts()):while(have_posts()):the_post();?><!--主循环-->

<h3><?php the_title();?></h3><h3><?php the_title();?></h3><!--文章标题-->

<p><?php comments_popup_link(\'零条评论\',\'太少了只有一条评论\',\'%条评论\',\'css\',\'不允许评论\');?></p>

<p><?php edit_post_link(\'修改文章\',\'before\',\'后面\');?></p> <!--前台跑到后台编辑-->

<div><?php the_content();?></div><!--文章内容-->

<!--分页-->

<div>下一篇文章:<?php next_post_link(\'%link >>\',\'%title\',true,99999,\'post_tag\');?></div>

<!-- next_post_link(\'%链接 >>\',\'%标题\',false,); -->

<div>上一篇文章:<?php previous_post_link(\'<< %link\',\'%title\');?></div>

<div><?php comments_template();?></div>

<?php endwhile;?>

<?php endif;?>

</body>

</html>

有没有资深wordpress建站高手?

走了很多弯路,好不容易找到一套wordpress视频教程,这是其中一节课的笔记。得到这套教程,有点相见恨晚的感觉。

上面的笔记,你能看懂多少?评论区留言。

wordpress CMS,全世界最流行的CMS。

wordpress

WordPress主循环The Loop

WordPress的主循环The Loop用来输出内容,例如:文章内容、页面内容、文章列表。一些WordPress函数规定必须在Loop循环中使用,这是因为它们需要获取文章的ID

例如这些函数:

  • the_title():输出标题;
  • the_time():输出文章发表时间;
  • the_category():输出文章的分类;
  • the_permalink():输出文章的链接;

WordPress函数的命名很科学,从字面上基本就能猜到函数作用,通常带有the的,都需要获取当前文章的ID,需要在主循环中使用。

下面来看看WordPress主循环The Loop相关代码:

<?php if( have_posts() ) : ?><?php while( have_posts() ) : the_post(); ?>这里是要输出的具体内容...<?php endwhile; ?><?php endif; ?>

以文章和页面为例,输出文章/页面的标题和内容:

<?php if( have_posts() ) : ?><?php while( have_posts() ) : the_post(); ?><article id=\\\"post-<?php the_ID(); ?>\\\" <?php post_class(); ?>><h1 ><?php the_title(); ?></h1><div ><?php the_content(); ?></div></article><?php endwhile; ?><?php endif; ?>

同样的,以分类或首页的文章列表输出为例:

<?php if( have_posts() ) : ?><?php while( have_posts() ) : the_post(); ?><article ><h1><a href=\\\"<?php the_permalink(); ?>\\\" rel=\\\"bookmark\\\"><?php the_title_attribute(); ?></a></h1><footer ><time  datetime=\\\"<?php the_time( \\\'c\\\' ); ?>\\\" pubdate><?php the_time( \\\'Y-m-d\\\' ); ?></time><?php if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) : ?><span >评论(<?php comments_popup_link( \\\'0\\\', \\\'1\\\', \\\'{5cc1b29162d549a8071384de182cc9fc6e6a0fd85e7907f22fd9e18cff4269c3}\\\' ); ?>)</span><?php endif; ?></footer><p ><?php echo mb_strimwidth(wp_strip_all_tags($post->post_content, true), 0, 200, \\\'...\\\' ); ?></p></article><?php endwhile; ?><?php endif; ?>

标签:

提交需求或反馈

Demand feedback