跳到主要内容

网站开发常用调用代码

网站开发常用调用代码
1、获取页面分类列表

<?php query_posts('showposts=6&cat=54');
//cat=1为调用ID为1的分类下文章
while(have_posts()) : the_post(); ?>
    <li>
    <a  href="<?php%20echo%20the_permalink()%20?>"  title="<?php the_title(); ?>"  ><?php the_title(); ?></a>
<?php the_time('Y-m-d'); ?>
    </li>
<?php endwhile; ?>

2、获取文章图片链接

<?php $url = wp_get_attachment_url(get_post_thumbnail_id($post->ID)); echo $url; ?>

3、获取网站根域名

<?php echo site_url(); ?>

4、获取文章内容single页面
(1)根据文章分类选择页面模板

<?php
if ( in_category(array( 44,45,58,47,48,49 )) ) {
    get_template_part('single1' );
} elseif ( in_category( 10 )) {
    get_template_part('single1' );
} elseif ( in_category( 20 )) {
    get_template_part('single3' );
} else {
    get_template_part('single2' );
}
?>

(2)内容页调用

<?php if (have_posts()) : the_post(); ?>
标题:<?php the_title();?>
作者:<?php the_author();?>
发布时间:<?php the_time("Y年m月d日 H:i:s"); ?>
浏览数:<?php if(function_exists('the_views')) { the_views('次浏览', true); } ?>
内容  :<?php the_content(); ?>
<?php endif; ?>

5、设置网站相对路径
6、修改图片上传自动改名
7、获取自定义字段

p>=字段名称:<?php echo get_post_meta($post -> ID, "ziduan", true); ?></p>

8、获取分类页列表目录
9、文章内容页获取上一条、下一条代码

<div class="shangxia">
      <div class="shang"><?php if (get_previous_post()) { previous_post_link('上一条: %link');} else {echo "没有了,已经是最后文章";} ?></div>
      <div class="xia"><?php if (get_next_post()) { next_post_link('下一条: %link');} else {echo "没有了,已经是最新文章";} ?></div>
</div>

10、获取分类列表
获取Page分类列表

<?php wp_list_pages('title_li=&sort_column=menu_order&child_of=333'); ?>
获取cat分类列表
<?php wp_list_cats("child_of=" . get_query_var("cat") . "&depth=1&hide_empty=0&orderby=order&child_of=59"); ?>
<!--li开始-->
<?php
if(get_category_children(get_query_var("cat")) != ""){
wp_list_cats("child_of=" . get_query_var("cat") . "&depth=1&hide_empty=0&orderby=order");
//获取当前列表分类
}else{
$catID = get_query_var('cat'); // 当前分类ID
$thisCat = get_category($catID);
$parentCat = get_category($thisCat->parent);
// 输出父分类的链接
//echo get_category_link($parentCat->term_id);
$cat = get_category($parentCat->term_id);
echo '<a href="'.get_category_link($parentCat->term_id).'">';
echo $cat->name;
echo '</a>';
}
?>
<!--li结束-->

11、获取文章标题

<?php $id=13;//这里是文章的ID
                            $title = get_post($id)->post_title;
                            echo $title;//输出文章的 标题
                            ?>

12、获取固定页面的标题和内容

<?php
    $page_id = 498;
    $page_data = get_page($page_id);
    echo $page_data -> post_title;
    // 标题
?>
<?php
            $page_id = 501;
            $page_data = get_page($page_id);
            echo apply_filters('the_content', $page_data -> post_content);
            //内容
        ?>

13、根据分类ID获取标题

<?php
    $cat_ID = 72;
    //这里是文章的ID
    $cat_name = get_cat_name($cat_ID);
    echo $cat_name;
    //输出文章的 标题
?>

14、获取导航NAV

<?php wp_nav_menu(
    array(
    'container'       => 'div', //最外层容器标签名
    'container_class' => 'nav', //最外层容器class名
    'menu' => '缔造者菜单',
    'theme_location' => 'primary',
    'menu_id' => '')
    );
?>

15、调用NAV分类描述
在functions加以下代码!

//menu
class description_walker extends Walker_Nav_Menu
{
     function start_el(&$output, $item, $depth=0, $args=array(),$id=0)
    {
        global $wp_query;
        $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
        $class_names = $value = '';
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
        $class_names = ' class="'. esc_attr( $class_names ) . '"';
        $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
        $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
        $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
        $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
        $attributes .= ! empty( $item->url )        ? ' href="'%20%20%20.%20esc_attr(%20$item->url%20%20%20%20%20%20%20%20)%20.'"' : '';
        $prepend = '';
        $append = '';
        $description  = ! empty( $item->description ) ? '<div class="m-desc">'.'<a'. $attributes .'>'.esc_attr( $item->description ).'</a></div>' : '';
        if($depth != 0)
        {
            $description = $append = $prepend = "";
        }
        $item_output = $args->before;
        $item_output .= '<a'. $attributes .'>';
        $item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
        $item_output .= '</a>';
        $item_output .= $args->after;
        $item_output .= $description.$args->link_after;
        $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
}

16、获取当前分类列表名称

<?php $thiscat = get_category($cat); echo $thiscat ->name;?>

17、回去当前分类列表描述

<?php $thiscat = get_category($cat); echo $thiscat ->description;?>

18、获取当前文章下的分类列表

<?php if ( is_single() ) :
global $post;
$categories = get_the_category();
foreach ($categories as $category) :
    ?>
    <li class="widget widget_recent_entries" id="<?php $category -> term_id; ?>-posts">
        <h2 class="widgettitle"><?php echo $category -> name; ?></h2>
        <ul>
        <?php
        $posts = get_posts('numberposts=5&category='. $category->term_id);
        foreach($posts as $post) :
        ?>
            <li>
            <a href="<?php%20the_permalink();%20?>"><?php the_title(); ?></a>
            </li>
        <?php endforeach; ?>
        </ul>
    </li>
<?php
endforeach; endif ;
 ?>

19、通过别名获取分类信息

<?php $cat=get_category_by_slug('test11');  echo $cat->name; ?>
//可以var_dump查看所有分类信息,通过对象方式获取!

 
版权声明:本文为CSDN博主「蓝迪网络」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 原文链接:blog.csdn.net/fengqiangjun/article/details/51884211

返回顶部