博客

  • wordpress调用同分类随机文章

    wordpress调用同分类随机文章代码

    <?php
    
    $cat = get_the_category();
    
    foreach($cat as $key=>$category){
    
    $catid = $category->term_id;}
    
    $args = array('orderby' => 'rand','showposts' => 8,'cat' => $catid ); // 显示篇数
    
    $query_posts = new WP_Query();
    
    $query_posts->query($args);
    
    while ($query_posts->have_posts()) : $query_posts->the_post();?>
    
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    
    <?php endwhile;?>
    
    <?php wp_reset_query(); ?>
  • wordpress调用全站热门文章

    wordpress调用全站热门文章代码

    <?php
    
    $post_num = 10; // 显示篇数
    
    $args = array(
    
    ‘post_password’ => ”,
    
    ‘post_status’ => ‘publish’, // 只选公开的文章.
    
    ‘post__not_in’ => array($post->ID),//排除当前文章
    
    ‘caller_get_posts’ => 1, // 排除置顶文章.
    
    ‘orderby’ => ‘comment_count’, // 依评论数排序.
    
    ‘posts_per_page’ => $post_num
    
    );
    
    $query_posts = new WP_Query();
    
    $query_posts->query($args);
    
    while( $query_posts->have_posts() ) { $query_posts->the_post(); ?>
    
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    
    <?php } wp_reset_query();?>
  • WordPress调用指定分类文章

    wordpress按分类ID调用指定分类的文章代码

    <?php
        $args=array(
            'cat' => 1,   // 分类ID
            'posts_per_page' => 10, // 显示篇数
    
        );
        query_posts($args);
    
        if(have_posts()) : while (have_posts()) : the_post();
    ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> 
        </li>
    <?php  endwhile; endif; wp_reset_query(); ?>
  • wordpress调用最新文章

    wordpress调用最新文章的代码

    <?php query_posts('showposts=6&cat=-1'); ?>  // 显示篇数和排除分类
    <ul>  
    <?php while (have_posts()) : the_post(); ?>  
    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>  
    <?php endwhile;?>  
    </ul>  
  • wordpress怎么调用页面别名

    在制作wordpress主题时常常会用到调用页面的别名,下面这段代码可以完美解决,wordpress页面别名调用的问题。

    <?php if( is_page() ){$post_data = get_post($post->ID, ARRAY_A); echo $post_data['post_name'];} ?>

    把上面这么代码放在page.php页面中需要显示别名的位置即可。

  • wordpress搜索自字义字段内容

    有些网站需要根据自定义段字的内容来做为搜索项,比如,房产中介公司wordpress网站,需要搜索同一区域内容的楼盘,然后展示出内容。

    不废话了,在function.php直接加上代码

    add_action('posts_search', function($search, $query){
    	global $wpdb;
    
    	if ($query->is_main_query() && !empty($query->query['s'])) {
    
    		$sql    = " OR EXISTS (SELECT * FROM {$wpdb->postmeta} WHERE post_id={$wpdb->posts}.ID and meta_key = 'wodepress' and meta_value like %s)";
    		$like	= '%' . $wpdb->esc_like($query->query['s']) . '%';
    
    		$search	.= $wpdb->prepare($sql, $like);
    	}
    	return $search;
    },2,2);

    wodepress为自字义字段。

  • wordpress两个网站用同一个数据库

    wordpress两个网站用同一个数据库,同一个wordpress网站给绑定两个域名就可以。

    只需要在网站根目录的wp-config.php文件中的

    define( ‘WP_DEBUG’, false );

    下面添加这段代码就可以现实

    $domain = array("www.jianzhanpress.com", "www.wodepress.com");
    if(in_array($_SERVER['HTTP_HOST'], $domain)){
        define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST']);
        define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST']);
    }

    是不是很简单呢!

  • 获取wordpress某个栏目的内容数量

    <?php
    // 将以下 8 改成你的分类 ID 即可
    
    echo get_category(8)->count;
    
    ?>

    在制作wordpress模板时,有时会需要调用某个分类目录下的所有内容数量,通过这段简洁的代码就可以实现。

  • 给WordPress自定义字段加判断

    如果有自定义字段,显示自定义字段,如果没有自定义字段,不显示自定义字段。

    <?php if (get_field('wodepress')): ?>
    <?php the_field('wodepress'); ?>
    <?php endif; ?>
  • wordpress搜索结果排除某个分类

    从wordpress的搜索结果中排队某个分类的内容

    <?php if ( have_posts() ) : query_posts($query_string .'&cat=-1,-2,-3'); while ( have_posts() ) : the_post(); ?>
    //这是显示要调用的内容
    <?php endwhile; ?>
    
    <?php else : ?>
    no wodepress.com!
    <?php endif; ?>