如何在wordpress中显示最新更新的文章

如何在wordpress中显示最新更新的文章,第1张

在wordpress里有一个小工具,显示最新文章,不过这个是按最后发布的日期显示的。另外在wordpress循环中,也是按最后发布的日期显示的。但是对于有限项目中需要按最新更新的方式显示文章。对此,是必要的。能够有利于让用户更好的获取网站的第一手资讯。所以按最新更新文章显示是非常好的。下面提供一个函数:
把下面的代码放在functionsphp中:
[php]
function wpb_lastupdated_posts() {
// Query Arguments
$lastupdated_args = array(
‘orderby’ => ‘modified’,
‘ignore_sticky_posts’ => ’1′
);
//Loop to display 5 recently updated posts
$lastupdated_loop = new WP_Query( $lastupdated_args );
$counter = 1;
echo ‘<ul>’;
while( $lastupdated_loop->have_posts() && $counter < 5 ) : $lastupdated_loop->the_post();
echo ‘<li><a href="’ get_permalink( $lastupdated_loop->post->ID ) ‘"> ‘ get_the_title( $lastupdated_loop->post->ID ) ‘</a> ( ‘ get_the_modified_date() ’) </li>’;
$counter++;
endwhile;
echo ‘</ul>’;
wp_reset_postdata();
}
//add a shortcode
add_shortcode(‘lastupdated-posts’, ‘wpb_lastupdated_posts’);
[/php]
现在就可以在wordpress主题模版中使用下面的方式显示最新更新的文章了:
[php]
<php
if (function_exists(wpb_lastupdated_posts)) :
wpb_lastupdated_posts();
endif;
>
[/php]
如果不想修改wordpress主题模版文件,也可以直接在文章、页面、小工具里直接添加简码:
[lastupdated-posts]

1、层级较深的网站,面包屑导航适合层级较深的网站,如果只有一级分类的话,通过主导航就可以起到快速定位的作用。比如“豆瓣网”类型扁平构架的网站就没有使用面包屑导航的作用。

2、独立不交叉的网站机构,由于面包屑网站导航路径是线性结构的,因此网站内容必须划分的非常清晰,且不存在交叉;否则,面包屑导航的路径就不是唯一的,同一分类可能出现在不同的路径中,让用户感到困惑。

在wordpress中添加面包屑导航的话,可以直接使用插件来实现,不过很多站长都不喜欢用插件,还是认为如果能用代码解决的话是最理想的。我最近也找了一些关于面包屑导航的资料。下文中就有详细解决怎么用代码来实现wordpress面包屑导航:

一、在wordpress博客当前主题的functionsphp文件(没有就创建一个)中添加以下代码:

//面包屑导航

functionget_breadcrumbs()

{

global$wp_query;

if(!is_home()){

//StarttheUL

echo'<ulclass="breadcrumbs">';

//AddtheHomelink

echo'<li><ahref="'get_settings('home')'">'get_bloginfo('name')'</a></li>';

if(is_category())

{

$catTitle=single_cat_title("",false);

$cat=get_cat_ID($catTitle);

echo"<li>"get_category_parents($cat,TRUE,"")"</li>";

}

elseif(is_archive()&&!is_category())

{

echo"<li>Archives</li>";

}

elseif(is_search()){

echo"<li>SearchResults</li>";

}

elseif(is_404())

{

echo"<li>404NotFound</li>";

}

elseif(is_single())

{

$category=get_the_category();

$category_id=get_cat_ID($category[0]->cat_name);

echo'<li>'get_category_parents($category_id,TRUE,"");

echothe_title('','',FALSE)"</li>";

}

elseif(is_page())

{

$post=$wp_query->get_queried_object();

if($post->post_parent==0){

echo"<li>"the_title('','',FALSE)"</li>";

}else{

$title=the_title('','',FALSE);

$ancestors=array_reverse(get_post_ancestors($post->ID));

array_push($ancestors,$post->ID);

foreach($ancestorsas$ancestor){

if($ancestor!=end($ancestors)){

echo'<li><ahref="'get_permalink($ancestor)'">'strip_tags(apply_filters('single_post_title',get_the_title($ancestor)))'</a></li>';

}else{

echo'<li>'strip_tags(apply_filters('single_post_title',get_the_title($ancestor)))'</li>';

}

}

}

}

//EndtheUL

echo"</ul>";

}

}

二、在显示面包屑导航的位置添加以下调用代码:

<php

if(function_exists('get_breadcrumbs')){

get_breadcrumbs();

}

>

三、在主题的css样式文件中添加以下样式代码:

ulbreadcrumbs{list-style:none;font-size:12px;}

ulbreadcrumbsli{float:left;margin-right:5px;}

<div class="result"><ul>这个去掉
<php
while($row=mysql_fetch_array($result))
{
$j++;
if($j==1){
echo "<div class="result"><ul>";
}
>
<li><=$row["title"]></li>
<php
if($j==$i)
{
$j=0;
echo "</ul></div>";
}
}
for($k=$i-$j;$k>0;$k--)
{
echo "";
}
>
</ul></div>这个去掉

PHP语言遍历以数组形式的数据:

<ul>
    <php foreach($data => $v){ >
    <li><php echo $v['name'];></li>
    <php } >
</ul>

$data代表数据库查询出来的数据,$v代表自定义$data数组的名称,$v['name']代表该数组的name单元的值

循环的时候用一个变量来计数,每次都自增1,加上一个if条件,在循环5次之前用<ul class="list-news_2 splist">,5次以后用<ul class="list-news_2">就可以了。

我这里循环输出一个数组为例:

foreach($arr as $v){
$num = $num + 1;
if($num < 5){
    echo '<ul class="list-news_2 splist">';
}else{
    echo '<ul class="list-news_2">';
}
}

$sql="select from newscontent where newstypeid=$newstypeid"; 加上order by啊
不知道你字段,你可以按照改字段排序~
然后你再循环输出的时候就会得到自己想要的结果
或者,把查询的结果保存到数据,然后再对数组排序~,再输出

在循环时有一个变量,如$i,你判断他$i%2==0输入你想要的样式,不等于输出另外一种样式
if($i%2==0){echo"class='样式1'";}else{echo"class='样式2'";}

根据你的数据库和需求改一下就可以了
<style type="text/css">
ul {position:relative;
list-style-type: none;
padding:0px;
margin:0px;
font-size:12px;
}
li{
position:relative;
padding:0px;
margin:0px;
}
ul{
padding:0px;
margin:0px;
position:relative;
margin-left:8px;
width:100px;
display:none;

}
</style>
<script>
function open_close(id){

idchildrenitem(0)styledisplay='block';

}
</script>
<php
function xh($i){
if($i>1) {
echo "<ul class='ul'>";
for($j=1;$j<$i;$j++){
echo "<li onClick='open_close(this)'>├点我"$j;
xh($j);
echo "</li>";
}
echo "</ul>";
}
}

$i=0;
echo "<ul>";
while(true){

$i++;

echo "<li onClick='open_close(this)'>点我"$i;
xh($i);
echo "</li>";

if($i==10){break;}

}
echo "</ul>";
>


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/yw/13387435.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-26
下一篇 2023-07-26

发表评论

登录后才能评论

评论列表(0条)

保存