Php code to list WordPress post in csv or excel format

Write this code in sidebar.php

You can also change the file path as required. The Default file location is your domain path.

<!–
<form action='<?php echo $_SERVER[“PHP_SELF”];?>’ method=’post’>

Import File :<input type=”text” name=”file” id=”file” />
<input type=’submit’ name=’submit1′ value=’submit1′ >

</form>
<?php
if(isset($_POST[‘submit1’]))
{
$fp=fopen(‘test.csv’,’a’);
$values = mysql_query(“SELECT ID, post_date, post_title, post_content,post_excerpt,post_status, post_name, post_type FROM $wpdb->posts”);
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<8;$j++) {
$csv_output .= “@”.$rowr[$j].”@,”;
}
$csv_output .= “\n”;
}

fwrite($fp,$csv_output);
exit;
}
?> –>

WordPress Custom post Templates

You can use a specific post template for single posts that you want to function differently. In a recent project i worked on, i didn’t want the posts in the news category to look or behave like the rest of the posts, i thought it would be better to have another template for the posts in the “news” category. I found a smart solution, you will need to put only the code below in your single.php file in your current theme folder:

<?php
$post = $wp_query->post;
if ( in_category('13') ) {
include(TEMPLATEPATH . '/single13.php');
} else {
include(TEMPLATEPATH . '/single1.php');
}
?>

Here we are telling WordPress: If the post is in category ID number 13, display single13.php. If not in category ID number 13, display single1.php. Now you just create 2 new files “single13.php” & “single1.php”, and do whatever you want to do. From this, you can make as many single post page looks as you want, as long as they are styled by their category.

WordPress Single Post Templates- this post goes through a simple technique to have post templates like page templates. Here’s the code to add to your theme’s functions.php file. (be sure you paste this code between tags):

add_filter('single_template', create_function('$t', 'foreach( (array) get_the_category() as $cat )
{ if ( file_exists(TEMPLATEPATH . "/single-{$cat->term_id}.php") )
return TEMPLATEPATH . "/single-{$cat->term_id}.php"; } return $t;' ));

This technique solve the multiple categories issue (a post is included in more than one category) since it cycles through all the categories in the array and checks to see which one of them has an associated post template. When it finds one, it uses the post template file, but if it doesn’t, then it falls back on the default single.php template.

Most Viewed post by category, tags and archives in wordpress

Using WP-PostViews plugin we can display most viewed post by category, tags and archives. It’s more useful to know the best one of that particular category, tags and archives. I am using this concept in my project (higginbothamsstore.com) to display most viewed books by category vice.

Code:

<div id=”sidebar”>

<?php if(is_category() || is_single()){?>

<h3 id=”widgettitle”>Most viewed Books in <?php single_cat_title(); ?> </h3>

<div id=”mainright” style=”width:99%;”>

<?php if (function_exists(‘get_least_viewed_category’)): ?>

<ul>

<?php get_least_viewed_category($cat_ID,’post’,5); ?>

</ul>

<?php endif; ?>

</div>

<?php }?>

</div>

WP-PostViews 1.50 For WordPress 2.8.x

preview

More Instructions

General Usage
  1. Open wp-content/themes/<YOUR THEME NAME>/index.phpYou may place it in archive.php, single.php, post.php or page.php also.
  2. Find:

    <?php while (have_posts()) : the_post(); ?>

  3. Add Anywhere Below It:

    <?php if(function_exists(‘the_views’)) { the_views(); } ?>

  4. Go to ‘WP-Admin -> Settings -> PostViews’ to configure the plugin.
View Stats (With Widgets)
  1. Go to ‘WP-Admin -> Appearance -> Widgets
  2. The widget name is Views.
View Stats (Outside WP Loop)
  • To Display Least Viewed Posts
  • Use:

    <?php if (function_exists(‘get_least_viewed’)): ?>
    <ul>
    <?php get_least_viewed(); ?>
    </ul>
    <?php endif; ?>

    The first value you pass in is what you want to get, ‘post’, ‘page’ or ‘both’.
    The second value you pass in is the maximum number of post you want to get.

    Default: get_least_viewed(‘both’, 10);

  • To Display Most Viewed Posts
  • Use:

    <?php if (function_exists(‘get_most_viewed’)): ?>
    <ul>
    <?php get_most_viewed(); ?>
    </ul>
    <?php endif; ?>

    The first value you pass in is what you want to get, ‘post’, ‘page’ or ‘both’.
    The second value you pass in is the maximum number of post you want to get.

    Default: get_most_viewed(‘both’, 10);

  • To Display Least Viewed Posts By Tag
  • Use:

    <?php if (function_exists(‘get_least_viewed_tag’)): ?>
    <ul>
    <?php get_least_viewed_tag(); ?>
    </ul>
    <?php endif; ?>

    The first value you pass in is the tag id.
    The second value you pass in is what you want to get, ‘post’, ‘page’ or ‘both’.
    The third value you pass in is the maximum number of post you want to get.

    Default: get_least_viewed_tag(1, ‘both’, 10);

  • To Display Most Viewed Posts By Tag
  • Use:

    <?php if (function_exists(‘get_most_viewed_tag’)): ?>
    <ul>
    <?php get_most_viewed_tag(); ?>
    </ul>
    <?php endif; ?>

    The first value you pass in is the tag id.
    The second value you pass in is what you want to get, ‘post’, ‘page’ or ‘both’.
    The third value you pass in is the maximum number of post you want to get.

    Default: get_most_viewed_tag(1, ‘both’, 10);

  • To Display Least Viewed Posts For A Category
  • Use:

    <?php if (function_exists(‘get_least_viewed_category’)): ?>
    <ul>
    <?php get_least_viewed_category(); ?>
    </ul>
    <?php endif; ?>

    The first value you pass in is the category id.
    The second value you pass in is what you want to get, ‘post’, ‘page’ or ‘both’.
    The third value you pass in is the maximum number of post you want to get.

    Default: get_least_viewed_category(1, ‘both’, 10);

  • To Display Most Viewed Posts For A Category
  • Use:

    <?php if (function_exists(‘get_most_viewed_category’)): ?>
    <ul>
    <?php get_most_viewed_category(); ?>
    </ul>
    <?php endif; ?>

    The first value you pass in is the category id.
    The second value you pass in is what you want to get, ‘post’, ‘page’ or ‘both’.
    The third value you pass in is the maximum number of post you want to get.

    Default: get_most_viewed_category(1, ‘both’, 10);

  • To Sort Most/Least Viewed Posts
  • You can use:

    <?php query_posts(‘v_sortby=views&v_orderby=desc’) ?>

    Or pass in the variables to the URL:

    http://yoursite.com/?v_sortby=views&v_orderby=desc

    You can replace desc with asc if you want the least viewed posts.

Jquery Featured Post Slideshow

Steps

  1. use wp_enqueue_script to load the Jquery that is packaged with WordPress.
  2. go over how Jquery Cycle works and make The Loop generate the necessary XHTML
  3. make a custom query to load only the posts we want
  4. configure Jquery Cycle to run the slideshow

Download preview

1. Enqueue Jquery

The first thing you want to do is to get Jquery to load WordPress comes with a version of Jquery included so its generally best to load that> The function to load javascript libraries and plugins is wp_enqueue-script. The code to load the jQuery and the jQuery Cycle plugin is below

<?php wp_enqueue_script('jquery.cycle.all.min','/wp-content/themes/upsidedowncity_v_3/jquery.cycle.all.min.js',array('jquery')); ?>
<?php wp_enqueue_script('jquery_script','/wp-content/themes/upsidedowncity_v_3/jquery_script.js'); ?>
<?php wp_head(); ?>


Place this code right above the “wp_head();” line your header.php file.
 Write the css code for featured post sideshow.


/*Featured Post Slide Show @asokkumar.t@gmail.com*/
#featured_posts{
width:960px;
margin-left:-10px;
height:140px;
}

#featured_posts h3{
font-size:12px;
width:255px;
}

#featured_posts ul{

}

#featured_posts ul li{
float:left;
margin:0 9px 0;
}

.slide_cont{
width:835px;
clear:none;
float:left;
margin:0 17px;
}

.slide_cont ul li h3 a, .slide_cont ul li h3 a:hover{
color:#fff;
}

.prev{
display:block;
width:35px;
height:75px;
background: url('images/left_arrow.png') no-repeat center;
cursor:pointer;
float:left;
margin:35px 0 0 10px;
}

.next{
display:block;
width:35px;
height:75px;
background: url('images/right_arrow.png') no-repeat center;
cursor:pointer;
float:right;
margin:35px 10px 0 0;
}

h1, h2, h3, h4, h5, h6{
color:#fff;
background:#000;
font-family:"courier new",courier;
padding:3px 0 3px 5px;
}

add the given php code at any place in your templete

/* PHP code*/

<div id="featured_posts">
<a></a>

<div>

<?php 
$containter = 1; 
$display = 1;
?>

  <?php $my_query = new WP_Query('category_name=featured');
  while ($my_query->have_posts()) : $my_query->the_post(); ?>

<?php
if (($containter == 1) and ($display >= 4)) {
        echo "<ul class='slides' style='display:none;'>";
        } elseif  ($containter == 1) {
        echo "<ul class='slides'>"; 
        } else {}
?>

<li>
        <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
         <a href="<?php the_permalink() ?>"><?php getImage('1'); ?> </a>
</li>

<?php 
$containter++; 
$display++;
?>

<?php
if ($containter >= 4) {
echo "</ul>"; $containter = 1;} else {}
?>

                <?php endwhile; ?>
</div> <!-- end #slide_cont -->

<a></a>
</div> <!-- end #featured_posts -->

Download preview

Post Thumbnails without custom field

I encounter situations where i need to automatically display a thumbnail or image of Books that will link to posts. Now i found the solution to display Thumbnails without custom field.  Here’s an example of this kind of situation:

Write the given code in functions.php

function getImage($num) {
global $more;
$more = 1;
$link = get_permalink();
$content = get_the_content();
$count = substr_count($content, ‘<img’);
$start = 0;
for($i=1;$i<=$count;$i++) {
$imgBeg = strpos($content, ‘<img’, $start);
$post = substr($content, $imgBeg);
$imgEnd = strpos($post, ‘>’);
$postOutput = substr($post, 0, $imgEnd+1);
$result = preg_match(‘/width=”([0-9]*)” height=”([0-9]*)”/’, $postOutput, $matches);
if ($result) {
$pagestring = $matches[0];
$image[$i] = str_replace($pagestring, “”, $postOutput);
} else {
$image[$i] = $postOutput;
}
$start=$imgEnd+1;
}
if(stristr($image[$num],'<img’)) { echo ‘<a href=”‘.$link.'”>’.$image[$num].”</a>”; }
$more = 0;
}

Code for calling thumb img

<?php getImage(‘1’); ?>

(“1” 1st image in the post…)