Tuesday, March 15, 2011

Filter post by categorie id in Wordpress

<h2><?php echo get_cat_name(1);?></h2> <!-- get_cat_name(1) in which 1 is cat id -->
<?php $oddentry = 'class="gray" '; ?>
<?php query_posts('cat=1&showposts=1'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div <?php echo $oddentry; ?>>
         <div>
<div align="center"><img src="<?php viva('botNewspro','5'); ?> " alt="<?php the_title(); ?>" width="128px" height="122px" border="0" /></div>
<a href="<?php the_permalink() ?>" rel="bookmark"><h8 class="otherNews"><b><?php the_title(); ?></b></h8></a>
<div align="right"><div style="padding-top:5px;">Posted on <?php the_time('M j, Y'); ?></div></div>
</div>
         </div>
<?php /* Changes every other post to a different class */ $oddentry = ( empty( $oddentry ) ) ? 'class="gray" ' : ''; ?>
<!-- End of Loop for middle column -->
<?php endwhile; else : ?>
<?php endif; ?>
<?php wp_reset_query(); ?>

Inserting a dropdownlist value from a database - PHP

<?php

$db_name="yourdatabasename";// Database name
$conn=mysql_connect("localhost","databaseroot","password") or die("Error");
$db=mysql_select_db($db_name,$conn) or die("Unable to connect");
$databasetable="databasetablename"; // Table name

mysql_select_db("wphm", $conn);
$resultcity = mysql_query ("SELECT DISTINCT city FROM $databasetable ORDER BY city asc");

if (mysql_num_rows ($resultcity)) {
echo "<label>City:</label>\n";
echo "<SELECT name=\"ddcity\" id=\"ddcity\" onchange=\"this.form.submit();\">\n";
echo "<OPTION value=\"\">--choose city--</OPTION>\n";
while ($s1 = mysql_fetch_array ($resultcity, MYSQL_ASSOC)) {
foreach($s1 as $i1){
if($i1 == $_POST['ddcity']){$sel1 = " SELECTED";}
else{$sel1 = "";}
/*echo "<option value='" . $i1 . "'" . $sel1 . ">";
for ($i1 = 1; $i1 <= $_POST['ddcity']; $i1++) echo "&nbsp;&nbsp;";
echo "$s1[city]</option>\n";*/
echo "<option value='" . $i1 . "'" . $sel1 . ">" . $i1 . "</option>";
}
}
echo "</SELECT>\n";
}
?>

To show selected dropdown value on php page submit

<select name="ddgrvgtm" id="ddgrvgtm" onchange="this.form.submit()">
<option <?php if ($_POST['ddgrvgtm'] == '--Select--' || $_POST['ddgrvgtm'] == '') print 'selected '; ?> value="--Select--">--Select--</option>
<option <?php if ($_POST['ddgrvgtm'] == 'My Name is Gaurav') print 'selected '; ?> value="My Name is Gaurav">My Name is Gaurav </option>
<option <?php if ($_POST['ddgrvgtm'] == 'I am from Varanasi') print 'selected '; ?> value="I am from Varanasi">I am from Varanasi</option>
<option <?php if ($_POST['ddgrvgtm'] == 'I have done graduation from Nagpur') print 'selected '; ?> value="I have done graduation from Nagpur">I have done graduation from Nagpur</option>
<option <?php if ($_POST['ddgrvgtm'] == 'Currently Working in Bangalore') print 'selected '; ?> value="Currently Working in Bangalore">Currently Working in Bangalore</option>
<option <?php if ($_POST['ddgrvgtm'] == 'I support AC Milan Fc') print 'selected '; ?> value="I support AC Milan Fc">I support AC Milan Fc</option>
<option <?php if ($_POST['ddgrvgtm'] == 'I donate all blog money in poor child study') print 'selected '; ?> value="I donate all blog money in poor child study">I donate all blog money in poor child study</option>
<option <?php if ($_POST['ddgrvgtm'] == 'Thank for reading this post') print 'selected '; ?> value="Thank for reading this post">Thank for reading this post</option>
</select>

Filter post by tag_id in Wordpress


<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;

$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'showposts'=>20, // Number of related posts that will be shown.
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<h2>Related Posts</h2><ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
}
echo '</ul>';
}
}
?>