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;
}
?> –>

Advertisements

Advanced PHP code for wordpress

Content Only For The Home Page
<?php if ( is_home() ) { include (‘example.php’); } ?>
This snippet will include the file specified, only if the user is on the home page of the site. Place this code in the index.php file.
Styling Different Categories
<?php if ( is_category(’15’) ) {
<link rel=”stylesheet” href=”<?php bloginfo(‘template_url’); ?>/cat-15.css”
type=”text/css” media=”screen” />;
<?php } else { ?>
<link rel=”stylesheet” href=”<?php bloginfo(‘stylesheet_url’); ?>”
type=”text/css” media=”screen” />
<?php } ?>
This snippet assigns a specific stylesheet (category-15.css) to category 15 and will assign the rest of the site the default stylesheet (style.css). Place this code in the <head> area.
Unique Images For Different Categories
<?php if (is_category(‘7’) ):
<img src='<?php bloginfo(‘template_url’); ?>/images/cat7.jpg’ alt=” />
<?php } elseif (is_category(‘8’) ):
<img src='<?php bloginfo(‘template_url’); ?>/images/cat8.jpg’ alt=” />
<?php endif; ?>
This snippet assigns an image (cat7.jpg) next to each post title in category 7 and an image (cat8.jpg) next to each post title in category 8. Place this code in the category.php file.
Styling Individual Posts
<div id=”post-<?php the_ID(); ?>”>
This snippet will assign the post ID to the DIV. For example, if the ID for the post is 8, that line will echo as
<div id=”post-8”></div>. Now you can style that individual post in the CSS as #post-8. Place this code
within the loop.
Previous & Next Posts Links
<?php next_posts_link(‘Next Entries &raquo;’) ?>
<?php previous_post_link(‘&laquo; Older Entries’); ?>
The first snippet will echo “Next Entries »” with a link to the next set of posts. The second snippet will echo “« Previous Entries” with a link to the previous set of posts. Place this code outside the loop.
Site Page Links
<ul>
<li<?php if(is_home()) { ?> class=”current_page_item”<?php } ?>><a href=”
<?php bloginfo(‘home’); ?>”>home</a></li>
<?php wp_list_pages(‘sort_column=menu_order&depth=1&title_li=’); ?>
</ul>
This snippet will first echo the text “home” with a link to the home page. Next, it will echo the WordPress pages links in a list, in order defined by your settings, excluding the child pages, and excluding a title header for the list. If one of the pages in the list is active, the link for that page will be assigned the class “current_page_item”, which can now be styled in your CSS. Place this code in the template files.
Dynamic Page Titles
<?php
if (is_home()) { echo bloginfo(‘name’); } elseif (is_404()) { echo ‘ 404’; } elseif
(is_search()) { echo ‘ Search Results’; } else { echo ‘WPCandy » ‘; wp_title(”); }
?>
If the home page is active, the title will display the name of the site. If the 404 page is active, the title will echo ‘ 404’. If the Search Results page is active, the title will echo ‘ Search Results’. If any other page on the site is active, the title will display ‘WPCandy » [page name]’. Place this code in the <head> area.
Query Posts
<?php query_posts(‘cat=2&showposts=5’); ?>
This snippet will display the 5 latest posts from only category 2. Place this code right before the loop.
Unique Templates For Categories
<?php $post = $wp_query- >post;
if ( in_category(‘3’) ) {
include(TEMPLATEPATH . ‘/cat3.php’);
} elseif ( in_category(‘4’) ) {
include(TEMPLATEPATH . ‘/cat4.php’);
} else {
include(TEMPLATEPATH . ‘/cat.php’);
}?>
This snippet will assign a unique template to certain categories. In this case, ‘cat3.php’ will be assigned to ‘Category 3’, ‘cat4.php’ will be assigned to Category 4’, and the rest of the categories will be assigned ‘cat.php’. Place this code at the top of category.php.
Tag Cloud
<?php wp_tag_cloud(‘smallest=1&largest=9&’); ?>
This snippet will create a tag cloud, in alphabetical order, with the smallest text at 1 pt and the largest text at 9 pts.

PHP Code for WordPress

Grab Category Name

This PHP snippet will get the category of the current post and place it in line where the code is inserted. It would be useful to add a heading or phrase that relates to the category.

<? $cat = get_the_category(); $cat = $cat[0]; echo $cat->cat_name;?>

PHP Snippets for Header
<?php bloginfo(‘name’); ?> Title of the site
<?php wp_title(); ?> Title of the specific post or page
<?php bloginfo(‘stylesheet_url’); ?> The style.css file’s location
<?php bloginfo(‘pingback_url’); ?> Pingback URL for the site
<?php bloginfo(‘template_url’); ?> Location for the site’s theme files
<?php bloginfo(‘version’); ?> WordPress version for the site
<?php bloginfo(‘atom_url’); ?> Atom URL for the site
<?php bloginfo(‘rss2_url’); ?> RSS2 URL for the site
<?php bloginfo(‘url’); ?> Exact URL for the site
<?php bloginfo(‘name’); ?> Name of the site
<?php bloginfo(‘html_type’); ?> HTML version of the site
<?php bloginfo(‘charset’); ?> Charset parameter of the site
PHP Snippets for Templates
<?php the_content(); ?> Content of the posts
<?php if(have_posts()) : ?> Checks if there are posts
<?php while(have_posts()) : the_post(); ?> Shows posts if posts are available
<?php endwhile; ?> Closes the ‘while’ PHP function
<?php endif; ?> Closes the ‘if’ PHP function
<?php get_header(); ?> Header.php file’s content
<?php get_sidebar(); ?> Sidebar.php file’s content
<?php get_footer(); ?> Footer.php file’s content
<?php the_time(‘m-d-y’) ?> The date in ’08-18-07′ format
<?php comments_popup_link(); ?> Link for the comments on the post
<?php the_title(); ?> Title of a specific post or page
<?php the_permalink() ?> Url of a specific post or page
<?php the_category(‘, ‘) ?> Category of a specific post or page
<?php the_author(); ?> Author of a specific post or page
<?php the_ID(); ?> ID of a specific post or page
<?php edit_post_link(); ?> Link to edit a specific post or page
<?php get_links_list(); ?> Links from the blogroll
<?php comments_template(); ?> Comment.php file’s content
<?php wp_list_pages(); ?> List of pages of the
<?php wp_list_cats(); ?> List of categories for the site
<?php next_post_link(‘ %link ‘) ?> Url to the next post
<?php previous_post_link(‘%link’) ?> Url to the previous post
<?php get_calendar(); ?> The built-in calendar
<?php wp_get_archives() ?> List of archives for the site
<?php posts_nav_link(); ?> Next and previous post link
<?php bloginfo(’description’); ?> Site’s description