I have a WordPress site for a client. He owns a video store, and I made a site for him to update the list of movies, usually just the "new this week" movies.
I used PodCMS as an easy way for him to upload movies and then display them. No need for him to even create posts. Works really well, it's a great extension, I'm just having some issues.
The Pod has a field where you insert the release date. 2010-04-20
Then there is a Pod page/template combo that calls the movies with a certain release date like this:
$date = pods_url_variable('last');
He then just creates a blank WP page with the slug 2010-04-20
So when you open that page, the Pod page/template reads that slug and renders a list of the appropriate movies.
My problem:
I need these to be searchable. Is this possible.
I'm also open to suggestions on other ways to go about making this site work. I need it to be as simple as that. Uploads some movies and creates a new page. Then the rest is done automatically.
A PodsCMS search is nothing more than a mySQL table search for the search term. You can search the title, body, pretty much anything. Here's an example:
Note: I'm using "whatever" as the pod info. I'm also forming a string that goes into the $where position which comprises the various pods variables I want to search on. Also, I'm assuming pagination using the Pods pagination controls but I want that variable carried across the pages so I can offset.
<?php
$search_term = $_GET["s"];
$paged = get_query_var('paged');
$page_number = $_GET['pg'];
?>
<h1>
Results for "<?php echo $search_term; ?>"<?php if($page_number > 1){ ?> (Continued)<?php } ?><?php if($paged > 1){ ?> (Continued)<?php } ?>
</h1>
<?php if($paged <= 1){ ?>
<h2>Results in Whatever...</h2>
<?php
$whateverSentence = "(t.name LIKE '%" .$search_term. "%') || (t.whatever LIKE '%" .$search_term. "%')";
$whatever = new Pod('whatever');
$whatever->findRecords($orderby = 't.whatever DESC', $rows_per_page = 5, $where = $whateverSentence, $sql = null);
$total_whatever = $whatever->getTotalRows();
?>
<?php if( $total_whatever >0 ) : ?>
<?php while ( $whatever->fetchRecord() ) : ?>
<?php
// Set Variables
$whatever_ID = $whatever->get_field('id');
$whatever_Permalink = $whatever->get_field('slug');
$whatever_Name = $whatever->get_field('name');
?>
Code that echos the pods variables and represents the search result
<?php endwhile ?>
<?php else: ?>
<p>Sorry, no results found for that search.</p>
<?php endelse; endif ?>
<?php echo $whatever->getPagination($label = '<span class="pagination-text">Go to page:</span>'); ?>
<?php } ?>
So you need the WordPress page content - the list of movies - to be searchable?
WP doesn't search pages natively, but will with WordPress › Search Everything « WordPress Plugins and search better with WordPress › Relevanssi « WordPress Plugins
Related
Is it possible to only show the code below if there's a category description?
<h2>About <?php $cat = get_the_category(); echo $cat[0]->cat_name; ?></h2>
<?php $catID = get_the_category(); echo category_description ( $catID[0] ); ?>
I have these codes on my single posts in Wordpress but sometimes, I forgot to add descriptions on a new category that I added. So when a user visits the post, they will just see the word About Category and no description at all, making it looks like an incomplete article.
I'm not a developer and I'm also not familiar with PHP. I only added that code on the single.php to show the description. But I want it not to show when there's no description available.
Hope someone can give me the exact code to make it work.
Thanks!
Good practice to assign your values into variables at the top of your script before entering into HTML whenever possible. This will help prevent you making redundant calls and to debug your code better. Once you have your assigned values, you'll want to check if the value is empty. I am assuming the code you presented will always return some kind of value for index 0.
<?php
$cat = get_the_category();
$cat_0 = $cat[0];
$cat_0_name = $cat_0->cat_name;
$cat_0_desc = category_description($cat_0);
?>
<?php if(!empty($cat_0_desc)): ?>
<h2>About <?php echo $cat_0_name; ?></h2>
<?php echo $cat_0_desc; ?>
<?php endif; ?>
I should like to point out that I am choosing to use an alternative syntax for control structures versus the traditional brace control. This will make your code more readable and easily debugged when mixed in with HTML.
If your code is still throwing you errors, I would suggest you check your error logs as something would be happining during the get_the_cateory() call or that it's not returning any values resulting in error with $cat[0].
<?php if ($cat = get_the_category() && count($cat)>0) { ?>
<!-- <?php echo print_r($cat,1); ?> -->
<h2>About <?php echo $cat[0]->cat_name;?></h2>
<?php echo category_description ($cat[0]->cat_id); ?>
<?php } ?>
I would like to make a custom module with a list of tags. When a tag is clicked the visitor would be navigated to a category page that would show articles with that tag.
I am not a joomla expert, I was thinking about a solution like a hyperlink like this that I would add to the tags inside the module:
href="http://mywebsite.com/index.php/itemlist/tag/tokio%20city?category=places"
Is this possible? Or how could I achieve this result?
Thanks!
This is a bit more complicated than just a query string in the URL as you also need to tweak a template.
If you want to keep it as simple as possible, I'd would recommend creating a new K2 template using template overrides and editing the category template so that it would read the query string parameters and show only articles already filtered by the category and furthermore by the tag via a query string.
That's just a brief how-to, now with a lil bit more details:
1) Create a new K2 template using template overrides.
In your template, if it doesn't exist already, create a folder structure /templates/your_template/html/com_k2/templates/default. The "default" can be replaced with any name if you want to have more K2 templates, but you have to set the new template to each category you have manually.
Now take the content from "/components/com_k2/templates/default" and copy it to the new folder in your template. Now, K2 is using the templates from your /templates/your_template/html/com_k2/ folder. Feel free to google more details if you don't understand template overrides, it's pretty important thing when customizing a template.
2) Edit your category view file to accommodate the list to your query strings
The file that interests you now is in /templates/your_template/html/com_k2/templates/default/category.php. Open this file and try to understand what's important there:
Line 141
<?php foreach($this->leading as $key=>$item): ?>
Line 169
<?php foreach($this->primary as $key=>$item): ?>
Line 197
<?php foreach($this->secondary as $key=>$item): ?>
Line 226
<?php foreach($this->links as $key=>$item): ?>
This is what matters. In these four foreach loops, there are all the items. Then, you can wrap the content of each of these loops into an if-condition to check whether it has the desired tag that is specified in the URL.
To show you an example, this is the code for <div id="itemListPrimary">. You can replace this whole div in the category.php file with the following code and it will work flawlessly. I've just written and tested it.
<div id="itemListPrimary">
<?php foreach ($this->primary as $key=>$item): ?>
<?php
# Get the value of the "tag" query string
$jInput = JFactory::getApplication()->input;
$myTag = $jInput->get('tag', null, 'STRING'); // Joomla 1.6+
//$myTag = JRequest::getVar('tag'); // for Joomla 1.5
# If the tag is empty, the query string is not specified and we'll go standard way without any tag filter
if (empty($myTag)) {
// Define a CSS class for the last container on each row
if ((($key+1)%($this->params->get('num_secondary_columns'))==0) || count($this->secondary)<$this->params->get('num_secondary_columns'))
$lastContainer= ' itemContainerLast';
else
$lastContainer='';
?>
<div class="itemContainer<?php echo $lastContainer; ?>"<?php echo (count($this->secondary)==1) ? '' : ' style="width:'.number_format(100/$this->params->get('num_secondary_columns'), 1).'%;"'; ?>>
<?php
// Load category_item.php by default
$this->item=$item;
echo $this->loadTemplate('item');
?>
</div>
<?php if(($key+1)%($this->params->get('num_secondary_columns'))==0): ?>
<div class="clr"></div>
<?php endif;
# Otherwise the tag is set so we'll filter the articles by the tag
} else {
# Get an array of all the tags that the current article in the loop has
$articleTags = array();
foreach ($item->tags as $tag) {
$articleTags[] = $tag->name;
}
# Check if the article has the tag specified in the URL as a query string
if (in_array($myTag, $articleTags)) {
# Now the default content of the foreach loop comes as written in the default K2 category.php template
// Define a CSS class for the last container on each row
if ((($key+1)%($this->params->get('num_secondary_columns'))==0) || count($this->secondary)<$this->params->get('num_secondary_columns'))
$lastContainer= ' itemContainerLast';
else
$lastContainer='';
?>
<div class="itemContainer<?php echo $lastContainer; ?>"<?php echo (count($this->secondary)==1) ? '' : ' style="width:'.number_format(100/$this->params->get('num_secondary_columns'), 1).'%;"'; ?>>
<?php
// Load category_item.php by default
$this->item=$item;
echo $this->loadTemplate('item');
?>
</div>
<?php if(($key+1)%($this->params->get('num_secondary_columns'))==0): ?>
<div class="clr"></div>
<?php endif;
}
} ?>
<?php endforeach; ?>
</div>
3) Understand how the URLs will work
My typical category URL is:
http://mywebsite.com/category-name
To show only articles with a specified tag, use:
http://mywebsite.com/category-name?tag=your-tag
For instance, if you want to show only articles with the "Tokio City" tag, use:
http://mywebsite.com/category-name?tag=Tokio City
Done.
That's the basics of what you needs. It's all you need if you use primary articles only (no leading and secondary or links). Of course there are plenty more things you might want to take care of:
a notice if there is no article with the specified tag
no redundant code, I've written it like this for the sake of simplicity and readability
SEO - spaces and special characters in URLs
making sure no empty div will be printed
But that would be way more code and I wanted to keep it simple & readable for you. I think I gave you more than enough for a start, so go ahead and get it done, good luck :)
I am working on a portfolio website with multiple artists, and each artist will have their own blog.
For each artist to have their own blog I decided for them to 'tag' themselves in posts that they make, on their portfolio pages I will list the posts with said tags.
The name of the page, is their name, so initially I thought just using:
<?php
$args=array('posts_per_page'=>2, 'tag' => $post->post_title);
$wp_query = new WP_Query( $args );
?>
would work, but doesn't seem to pull up any results. Yet when I echo the post title and the tag they are both displayed exactly the same.
So my next thought was to match the tag to a reg expression. Something like:
<?php
if( preg_match("/tony/i",$post->post_title)){
echo "Tony";
}
?>
but I do not know how to work that into a wp query.
Any idea how to do this, or if there is a better way to get to the same end result?
I quite recently was baffled by the exact same issue. I was trying to display articles based on a combination of a first and last name, but that wasn't working by just using the tag argument. I actually ended up using tag_slug__and for mine. Here's what I came up with...
<?php
$original_query = $wp_query;
$args = array( 'tag_slug__and' => array(strtolower($first_name) . "-" . strtolower($last_name)) );
$wp_query = new WP_Query( $args );
if(have_posts()):
echo "<h3>News Tagged " . $first_name . " " . $last_name . "</h3>";
while(have_posts()) : the_post();
$title = get_the_title();
$content = get_the_content();
$date = get_the_date();
//use the vars for something suuhhhhweeeeet!
endwhile;
endif;
$wp_query = $original_query;
wp_reset_postdata();
?>
The reason I used the tag_slug__and was because the tag slug is the lowercase tag with spaces delimited by a -. This is probably not the ideal solution, and just feels hacky, but it does work.
I'm curious why you would use tags, and not make use of the author page template and the native WordPress author system? There are lots of ways to do this without tags. If you need multiple authors, check out the co-authors plus plugin.
http://wp.tutsplus.com/tutorials/how-to-create-a-wordpress-authors-page-template/
http://wordpress.org/extend/plugins/co-authors-plus/
I use the_tags to display my post tags for my Wordpress site, I use this code:
<?php the_tags('',' • ',''); ?>
I need to exclude/hide two tags named featured and billed from the printed list. What is the most efficient way of doing this? Please only suggest light solutions, no mental queries that will slow my site which contains thousands of tags and hourly page views.
This is the only way that is simpler and doesn't load the page I'm thinking of:
<?php
$links = array();
foreach(get_the_tags() as $this_tag) {
if ($this_tag->name != "featured" && $this_tag->name != "billed"){
$links[] = ''.$this_tag->name.'';
}
}
echo implode(' • ', $links);
?>
I'm creating my own wordpress theme which is a bit different because it will not have single pages (or atleast, no single page will be reachable). The whole website contains just the homepage (with the loop) and previous posts pages.
I want to link to individual posts inside the loop like site.com#post-124 or site.com/paged=5#post-214.
I already created a function that does this:
function getPermalink($id,$postsPerPage) {
$postNumber = Get_Post_Number($id);
//a function that get's the post number based on
//the chronical order of published posts.
$page = floor(($postNumber - 1) / $postsPerPage);
$url = get_option('home');
if($page > 0) {
$url .= '/?paged=' . ($page + (1 - floor($page / 5)));
}
$url .= '#post-' . $id;
return $url;
}
You can see it live here: http://mijnrealiteit.nl (the previous posts pages are replaced by an infite scroll plugin).
This works, however it breaks when I start adding posts because all the posts before will get shifted back to pages further away (this makes the link invalid).
The way I see it there are two possible solutions:
Change the permalinkstructure to display paging backwards (so x.com/paged=231 becomes the first 'previous' page. However this is not userfriendly.
Make links with just the ID and let wordpress handle custom redirection to the page at that current point in time.
Are there better alternatives? I'm sure this is already solved somewhere, I just couldn't find it.
I got pushed in the right direction by a friend, I build it quite easily using option 2:
The getPermalink function is now much simpler:
function getPermalink($id) {
return get_option('home') . '/?f=' . $id;
}
I didn't make any custom redirection, I just checked at the homepage for a an 'f' being passed in the GET request:
$perma = $_GET['f'];
if(isset($perma) && !is_paged()) {
$customposts = get_posts('p=' . $perma );
foreach( $customposts as $post ) :
setup_postdata($post); ?>
//load the post
<?php endforeach;
}?>
If that is true the post will be fetched using the get_posts function by Wordpress. I also check the (normal) loop for the post that already has been served:
<?php while (have_posts()) : the_post();
if(get_the_ID() != $perma) { ?>
//load the post
<?php } endwhile; ?>