PHP - Display Category Title Once
Similar TutorialsHi Guys (& Ladies of course) I'm having a problem trying to add post category to the H1 title, I have managed to butcher my theme so that the single post now removes the H1 from the title in the header & instead adds it to the post title. But what I'd like to do now is keep the H1 blog title on all pages including home page, but I'd like to ammend the title with the addition of the post category, as follows; currently shows "Top Rated Gadgets" on all pages. would like it to show "Top Rated (category name)" when viewing a single post. So if the category of the post was Asus Laptops it would show "Top Rated Asus Laptops" instead of the usual "Top Rated Gadgets" The closest I have got to it so far is with Code: [Select] <?php the_category(', '); ?> but this returns a hyperlink ! Is there any method I can use that will simply return the category name as plain text..? Any help would be much appreciated Thanks Hi all. This seems like a pretty basic function but I can't work it out. I'm developing a WordPress site and want to list posts (products) by category. I'm using Advanced Custom Fields and have set up the taxonomy of 'category'. I just want to produce a heading for that category and then list all of the posts with that category assigned. Please see below functions.php excerpt and section-archive.php excerpt. Can anyone please help??? Massive good karma if you can. ----------------------- functions.php -------------------------------------- //Assign Category to Products
function taxonomy ()
$args = array (
}; ?> ------------------------- section-archive.php ------------------------------------------------ <?php if(have_posts()):while(have_posts()): the_post();?>
****** if product category= this ***** Category heading ****** Posts ******* Else ****** if product category= this2 ***** Category heading ****** Posts **** etc
<div class="prod-listing">
friends my url is like this Category=Love&ID=1 now in url if i do something like this Category4637=Hate19203&ID=1 the details are still showing, how can i fix this? if someone type Capegory instead of Category and a wrong category name which do not exist with that id in db send them to index.php? Hello, I have been searching and trying all sorts of ways to get my sub categories drop down to show the correct info after selecting a category from the main drop down but im getting stuck. It's pulling the categories and sub categories from the mysql database fine but no matter which main category i pick it keeps showing the same result in the sub category. My brother said i need to use some sort of onchange do this so the sub category list refreshs but everything i have tried doesn't work.. PLEASE could some one help. MANY MANY THANKS This is the category form Code: [Select] <select name="job_cats" style="width:165px"> <?PHP $query1 = "SELECT * FROM job_cats"; $result1 = mysql_query($query1) or die ("query 1 failed"); $count1 = mysql_num_rows($result1); for ($i = 0; $i < $count1; $i++) { $row1 = mysql_fetch_array($result1); $job_cats_title1 = $row1['title']; $job_cats_id1 = $row1['id']; echo '<option value="'.$job_cats_id1.'">'.$job_cats_title1.'</option>'; echo $job_cats_id1; } ?> </select> this would echo this in the main category list: cat1 cat2 cat3 cat4 this is the sub category form Code: [Select] <select name="job_sub_cats" style="width:165px"> <?PHP $query1 = "SELECT * FROM job_sub_cats WHERE job_cats_id = $job_cats_id1"; $result1 = mysql_query($query1) or die ("query 1 failed"); $count1 = mysql_num_rows($result1); for ($i = 0; $i < $count1; $i++) { $row1 = mysql_fetch_array($result1); $sub_cats_id1 = $row1['id']; $sub_cats_title1 = $row1['subTitle']; $cats_id1 = $row1['job_cats_id']; echo '<option value="'.$sub_cats_id1.'">'.$cats_id1.'</option>'; } ?> </select> this should echo this: 1 2 3 4 but it keeps echoing just the number 4. THANKS PLEASE HELP ricky Hi guys, I'm pretty much stuck and require someones help, I've got this script: Code: [Select] <?PHP $con = mysql_connect("localhost","****","*****"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("samdb", $con); /* check to see if an artist has been selected */ if(!$_GET['artist']) { $artist = 0; }else{ $artist = $_GET['artist']; } /* if artist is set display the artist */ if($artist>0) { $query = "SELECT * FROM songlist WHERE artist='$artist' ORDER BY id DESC"; $result = mysql_query($query); $row = mysql_fetch_array($result); $artist = $row['artist']; $song = $row['title']; ?><p> <b><font size="5"><?PHP echo $artist; ?></font> </b><br /> Songs: <?PHP echo $song; ?></p> <br> <br> <a href="artist.php">Back to artist listings</a> <?PHP }else{ /* create query */ $query = "SELECT * FROM songlist GROUP BY artist ORDER BY artist DESC LIMIT 10"; /* execute the query */ $result = mysql_query($query); while($row=mysql_fetch_array($result)) { $artist = $row['artist']; $song = $row['title']; ?><p> <b><font size="5"><?PHP echo $artist; ?></font></b> <br> <a href="artist.php?artist=<?PHP echo $row['artist']; ?>">See more</a></p> <?PHP } } ?> It lists the artist right and thats great, however when i click on a artist's name it shows the same page does not go anywhere? I've tired everything, it might be so simple its infront of me.. any help will be great! Thanks J Hi guys! How do I display different code depending on the title/url? This is the code for the form that brings you from "http://www.xxx.uk/index.php" to "http://www.xxx.uk/index.php?name=ro": Code: [Select] <form name="input" action="http://www.xxx.uk/index.php?name=ro" method="post"> Enter student number:<input type="text" name="sid"/> <input type="submit" value="Submit"> Now when you are on "http://www.xxx.uk/index.php?name=ro" I would like another content(but written in the same php file). I tried with _GET, but the problem is that I don't simply want to add a word but a lot of content. With _GET, that whole content would have to be displayed in the title and I don't want that. Hi there, I'm trying to output the form data into the email title, how I've attempted to do this is below: <?php if(isset($_POST['submit'])) { $to = 'message@website.com' ; $subject = '$song - $message - $name; //THIS BIT HERE I NEED WORKING... $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $message = "<table> <tr><td>Name</td><td>".$_POST['name']."</td></tr> <tr><td>Message</td><td>".$_POST['message']."</td></tr> <tr><td>Song</td><td>".$_POST['song']."</td></tr> </tr></table>" ; mail($to, $subject, $message, $headers); header('Location: song-requests-success.php'); } ?> I keep getting errors. Many thanks An array of "categories" is given: $categories = array( array("id" => 1, "title" => "Pants", "children" => array( array("id" => 2, "title" => "Boots", "children" => array( array("id" => 3, "title" => "Leather"), array("id" => 4, "title" => "Textile"), ), ), array("id" => 5, "title" => "Sneakers"), ), ), array( "id" => 6, "title" => "Sort", "children" => array( array( "id" => 7, "title" => "Balls", ), ), ), );
I need to write a function searchCategory ($categories, $id) that returns the name of the category by the category ID. So I am having some difficulties with a category system. I want my code to search the database an unlimited amount of times to the very last directory and then display it in tree view on my screen. I'm having some trouble because I am having to specify how many depths the code will search. How can I get it to do this on it's own? Currently my code does this... <?php // Establish SQL Connection. $mysqli = mysqli_connect( $sql['host'], $sql['user'], $sql['pass'], $sql['db'] ) or die( "DARN"); $query = $mysqli->query( "SELECT * FROM folders ORDER BY name ASC" ); $num_rows = $query->num_rows; echo "Found <b>" . $num_rows . "</b> row(s) in the database!<br>"; while ( $row = mysqli_fetch_array( $query ) ) { // DO NOT REMOVE $id = $row['id']; $name = $row['name']; $parent = $row['parent']; /***************************************************************************************************/ if ( $parent == 0 ) { echo $id . " " . $name . " " . $parent . "<br>"; $query2 = $mysqli->query( "SELECT * FROM folders WHERE parent = " . $id . " ORDER BY name ASC" ); while ( $row2 = mysqli_fetch_array( $query2 ) ) { echo " " . $row2['name'] . "<br>"; } } } ?>I know it's messy, doing the best I can. DB looks a little like this... +----+---------+--------+ Hope I am in the right area. I am working on a project which is sort of a business directory. I have a category table, a sub-category table then the actual account table. So a user clicks on the category, then selects the sub-category to view information of accounts in that category, and then be able to click to see the full profile. Here's where I am getting stuck. A user may be listed in more than one sub. As an example, let's say a parent is Cars, then subs might be New, Used, Parts, etc. Now a particular user might be listed under all three subs. How can I link them? I have my cat/sub linked via parent/child id's. However, how can I get the user to be linked to multiple subs? Would I need to create a separate table to do similar to parent/child of the categories? If so, then how would I insert the multiple id's into the table? I assume at this point that I would use drop downs for the subs. I am having a real hard time wrapping my head around this so any help is greatly appreciated. I have the following result that I am getting from a source. Code: [Select] SimpleXMLElement Object ( [CategoryID] => 1 [Name] => Other [Order] => 0 [link] => http://www.link.com?sub=1 ) SimpleXMLElement Object ( [CategoryID] => 1233916011 [Name] => ceramic [Order] => 1 [ChildCategory] => SimpleXMLElement Object ( [CategoryID] => 1234016011 [Name] => dish [Order] => 1 ) [link] => http://www.link.com?sub=13476 ) SimpleXMLElement Object ( [CategoryID] => 1233917011 [Name] => laminate [Order] => 2 [link] => http://www.link.com?sub=98247 ) I need to get it into a database with the following structure Code: [Select] id categoryID cName cOrder clink cparent By using Code: [Select] foreach($this->resp->Store->CustomCategories->CustomCategory as $cat) { $sub = $cat->CategoryID; $ord = $cat->Order; $linkto = "$stlink?fsub=$fsub"; $nm = htmlspecialchars($cat->Name); $par=$cat->ChildCategory->Order; $query = "INSERT INTO Cat (categoryID,cName,cOrder,clink,cparent) VALUES ('$sub','$nm','$ord','$linkto','$par')"; $result = @mysql_query ($query); // Run the query. } I can get Code: [Select] id categoryID cName cOrder clink cparent 1 1 Other 0 http://www.link.com?sub=1 0 2 1233916011 ceramic 1 http://www.link.com?sub=13476 0 2 1233917011 laminate 2 http://www.link.com?sub=98247 0 But I need to get the ChildCategory in there w/ cparent like Code: [Select] id categoryID cName cOrder clink cparent 1 1 Other 0 http://www.link.com?sub=1 0 2 1233916011 ceramic 1 http://www.link.com?sub=13476 0 3 1234016011 dish 1 1 <--- like this 4 1233917011 laminate 2 http://www.link.com?sub=98247 0 any help appreciated... Hi, I have been working on a directory/cms script (first large script to build from scratch). I am stuck on a small part of it where is shows users a list of the listings they have put in the directory. Below is the code that generates the table that shows the list of listings which is pulled from the 'listings' table in my database. Code: [Select] <?php ### List of listings user has posted ### if(isset($_SESSION['userlogged'])) { $user_id = $_SESSION['userlogged']; $sqlCommand = "SELECT listing_id, cat_id, listing_title, listing_date, showing FROM listings WHERE showing='0' AND user_id='$user_id' ORDER BY listing_id ASC"; $query = mysql_query($sqlCommand,$con) or die (mysql_error()); $ListingDisplay = ''; while ($row = mysql_fetch_array($query)) { $listing_id = $row["listing_id"]; $cat_id = $row['cat_id']; if ($cat_id == '1') { $cat_name = "Carpenters"; } if ($cat_id == '2') { $cat_name = "Stone Masons"; } if ($cat_id == '3') { $cat_name = "Plumbers"; } if ($cat_id == '4') { $cat_name = "Landscapers"; } $listing_title = $row["listing_title"]; $listing_date = $row["listing_date"]; $ListingDisplay .= ' <tr class="listingaccount"> <td><a href="edit_listing.php?lid=' . $listing_id . '">' . $listing_id . '</a></td> <td>' . $cat_name . '</td> <td>' . $listing_title . '</td> <td>' . $listing_date . '</td> <td><a class="hoverBtn" href="edit_listing.php?lid=' . $listing_id . '"><img src="images/btnedit.png" border="0"></a></td> <td><a class="hoverBtn" href="remove_listing.php?lid=' . $listing_id . '"><img src="images/btndelete.png" border="0"></a></td> </tr>'; } mysql_free_result($query); ?> <table width="680" border="0" align="right" cellpadding="0" cellspacing="0"> <tr> <td colspan="2"><h2>My Listings</h2></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td><a href="account_add_listing.php">Add New Listing</a></td> <td> </td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td colspan="2" valign="top"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td>Listing Code</td> <td>Category</td> <td>Title</td> <td>Date Entered</td> <td> </td> </tr> <?php echo $ListingDisplay; ?> </table> </td> </tr> </table> <?php } ?> That all works just fine, but I would like to be able to generate the categories automatically. I am thinking some sort of array, but im not quite sure how to go about it and was hoping maybe someone could give me a starting point I could work from. My categories table is like this: -------------------------- | cat_id | cat_name | -------------------------- Many thanks Eoin hi i hv built a small posting system, i hv built few categories like this in mysql i hv these tables Categories table: id catname Post Table: id postbody posttile date catid so i am getting posts with catid for related posts like http://localhost/site/index.php?action=category&cat=1 so where $_GET['cat'] is 1 it display posts from that category 1, now what i need is when i post a new post i hv choice to assign multiple categories to one post, so a post could be in cat1 and in cat2 as well . please any idea how can i do that? i was thinking to make a new rows in posts table for each category and set it to 0 or 1 , and when i submit new post it will set 1 to those categories which i select upon writing post. but if i have like 20 categories i need to add more 20 rows in posts table. is there any other better way i can accomplish this? Thanks for help Hi Guys, I've been using PHP for a few years now but for some reason I can never seem to get my head around arrays, basically I have a blog site that I want to show all posts that have been posted in public categories but ignore those in the private categories, I had the idea to put all the private category ID's into an array and use that to check against the posts, however my array isn't working out the way I'd hoped. This is what I have: $isPrivate = mysql_query("SELECT categoryID FROM mod_cat WHERE isPrivate =1") or die (mysql_error()); while ($isPrivateList = mysql_fetch_array($isPrivate, MYSQL_NUM)) { $ignoreList = $isPrivateList; } However this isn't working at all. I know its wrong I just can't work out how to make it right. This is the mySQL I want to use it with: mysql_query("SELECT COUNT(*) AS total_entries FROM mod_posts WHERE articlePosted =1 AND articleCat != $ignorelist") Could anyone please help me? Thanks in advance. I need some help with the category part on a old project . The problem what I have at this moment that, when I click on the category all categories are showing up on the category.php page itself. What whI wanne have when I click Uncategorised that in the category.php page only post are showing that have been made in Uncategorised category function get_posts() { $sql = "SELECT `blog`.`post_id` AS `id`, `blog`.`post_title` AS `title`, LEFT(`blog`.`post_body`, 512) AS `preview`, `blog`.`post_user` AS `user`, `blog_categories`.`id` AS `category_id`, `blog_categories`.`name` AS `category_name`, DATE_FORMAT(`blog`.`post_date`, '%d-%m-%Y %H:%i') AS `date`, `blog_comments`.`total_comments`, DATE_FORMAT(`blog_comments`.`last_comment`, '%d-%m-%Y %H:%i') AS `last_comment` FROM `blog` INNER JOIN `blog_categories` ON `blog_categories`.`id` = `blog`.`cat_id` LEFT JOIN ( SELECT `post_id`, COUNT(`comment_id`) AS `total_comments`, MAX(`comment_date`) AS `last_comment` FROM `blog_comments` GROUP BY `post_id` ) AS `blog_comments` ON `blog`.`post_id` = `blog_comments`.`post_id` ORDER BY `blog`.`post_date` DESC"; $posts = mysql_query($sql); $rows = array(); while (($row = mysql_fetch_assoc($posts)) !== false) { $rows[]= array( 'id' => $row['id'], 'title' => $row['title'], 'preview' => $row['preview'], 'user' => $row['user'], 'date' => $row['date'], 'category_id' => $row['category_id'], 'category_name' => $row['category_name'], 'total_comments' => ($row['total_comments'] === null) ? 0 : $row['total_comments'], 'last_comments' => ($row['last_comment'] === null) ? 'never' : $row['last_comment'] ); } return $rows; } function get_post($pid) { $pid = (int)$pid; $sql = "SELECT `post_title` AS `title`, `post_body` AS `body`, `post_user` AS `user`, `blog_categories`.`id` AS `category_id`, `blog_categories`.`name` AS `category_name`, DATE_FORMAT(`post_date`, '%d-%m-%Y %H:%i') AS `date` FROM `blog` INNER JOIN `blog_categories` ON `blog_categories`.`id` = `blog`.`cat_id` WHERE `post_id` = '$pid'"; $post = mysql_query($sql); $post = mysql_fetch_assoc($post); $post['blog_comments'] = get_comments($pid); return $post; }
The old way was like this if ( isset($cat_id)) { $cat_id = (int) $cat_id; $query .= " WHERE `blog_categories`.`id` = $cat_id"; } Database blog_categories id | name | ----------- 4 Uncategorised 1 Youtube blog post_id | cat_id | post_title | post_body | post_user | post_date ----------------------------------------------------------------------------------- 4 | 1 | Youtube | Youtube link | Admin | 2020-09-13 16:10:53 Web server Apache/2.2.21 (Win64) PHP/5.3.10 MySQL-client versie: mysqlnd 5.0.8-dev - 20102224 - $Revision: 321634 $ PHP uitbreiding: mysqli Documentatie phpMyAdmin Versie informatie: 3.4.10.1, meest recente versie: 5.0.2 I hope someone can help me with this problem Hello everyone, I am currently working on the script taken from www.phpwebcommerce.com I am only a beginner and have been using it in a little project for an online game just to help my mates be able to trade items and such. I have managed to get almost everything sorted apart from one thing. The function which gets the categories for a drop down select box: Code: [Select] /* Generate combo box options containing the categories we have. if $catId is set then that category is selected */ function buildCategoryOptions($catId = 0) { $sql = "SELECT cat_id, cat_parent_id, cat_name FROM tbl_category ORDER BY cat_id"; $result = dbQuery($sql) or die('Cannot get Product. ' . mysql_error()); $categories = array(); while($row = dbFetchArray($result)) { list($id, $parentId, $name) = $row; if ($parentId == 0) { // we create a new array for each top level categories $categories[$id] = array('name' => $name, 'children' => array()); } else { // the child categories are put int the parent category's array $categories[$parentId]['children'][] = array('id' => $id, 'name' => $name); } } // build combo box options $list = ''; foreach ($categories as $key => $value) { $name = $value['name']; $children = $value['children']; $list .= "<optgroup label=\"$name\">"; foreach ($children as $child) { $list .= "<option value=\"{$child['id']}\""; if ($child['id'] == $catId) { $list.= " selected"; } $list .= ">{$child['name']}</option>\r\n"; } $list .= "</optgroup>"; } return $list; } Basically the problem is that it only creates a category list of two levels e.g. Parent Child and I wish to be able to have more sub categories e.g. Parent Child Child Child Is there anyway of adapting this code so it can do something like that? Many thanks for any help. I'm not sure why but I copied the correct syntax from the wp codex page for it to display the date and title which it displays the titles for the categories however it only displays the date for the headlines category and don't know why. Any answers? <?php get_header(); ?> <div id="left"> <div id="col1"> <img id="lp" src="../images/lp.jpg" alt="Latest Promo"> <img class="newslogos" src="../images/headlines.jpg" alt="DW Headlines"> <div class="mininews"> <?php //The Query query_posts('category_name=headlines'); //The Loop while (have_posts()) : the_post(); ?> <h3 class="date"><?php the_date(); ?></h3> <h4 class="title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4> <?php endwhile; //Reset Query wp_reset_query(); ?> </div> <!-- End of main news div (headlines) --> <img class="newslogos" src="../images/rumors.jpg" alt="DW Rumors"> <div class="mininews"> <?php //The Query query_posts('category_name=rumors'); //The Loop while (have_posts()) : the_post(); ?> <h3 class="date"><?php the_date(); ?></h3> <h4 class="title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4> <?php endwhile; //Reset Query wp_reset_query(); ?> </div> <!-- End of main news div (rumors) --> <img class="newslogos" src="../images/columns.jpg" alt="DW Columns"> <div class="mininews"> <?php //The Query query_posts('category_name=columns'); //The Loop while (have_posts()) : the_post(); ?> <h3 class="date"><?php the_date(); ?></h3> <h4 class="title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4> <?php endwhile; //Reset Query wp_reset_query(); ?> </div> <!-- End of main news div (columns) --> </div> <!-- End of col1 --> </div> <!-- End of left --> <div id="right"> </div> <?php get_footer(); ?> Hello, I am trying create a news article, but I have few category in it. Can you help me to create an "add article form" which has dropdown list of category that I can choose, so when I select "Announcement" it will save the data on my mysql database under announcement table. sample html form below. <form id="form1" name="form1" method="post" action=""> <label> Category : <select name="category" id="category"> <option>--- choose category ---</option> <option>News</option> <option>Announcement</option> <option>Sports</option> </select> </label> <br /> Date: <br /> Title: <label> <input type="text" name="title" id="title" /> </label> <br /> Body: <label> <textarea name="body" id="body" cols="45" rows="5"></textarea> </label> <br /> <label> <input type="submit" name="Submit" id="Submit" value="Submit" /> </label> <br /> <br /> </form> Can you tell me how to create the php code which support the above html. Also, I would like to have a date and time on my article, but I want it automatically get the time and date from the computer of the client, so the user will not put it manually. can you point me to the correct code? Thanks and hope you can help me! |