How to Display Nice URL for Wordpress Category Page? - wordpress

I have a site whose home page shows a single Wordpress category page. I'm using a custom theme and the index.php file there does a 301 redirect to "mysitename.com/?cat=3". This page is generated with an event calendar plugin and a lot of custom code. It works, but the displayed URL is "mysitename.com/?cat=3" which I'd prefer my visitors not see; I'd rather have the URL be just "mysitename.com", "mysitename.com/index.php", or even "mysitename.com/calendar".
The visitor can view "prev month" and "next month", which result in URLs like "mysitename.com?cat=3&eventDate=2010-07&bt=a" -- still within the same category but with additional parameters in the URL. (I am OK having a more convoluted URL like this shown to my visitors on previous/next month; it's just the initial page URL I'd like to be nicer.)
I've messed around with the permalinks but can't get nice URLs to work correctly that way. Is there an .htaccess way to do this?

In the "permalinks" section under the "Settings" tab, you can add a category base to the categories.
Thus if you have a category base of "category" and a category name "Cool Stuff" with a slug of "cool_stuff"
Your category link would link like this depending on your permalink settings.
http://yoursite.com/category/cool_stuff/
Okay. My Last answer was not base on your "edited question".
If you remove the 301 redirect to "mysitename.com/?cat=3"
you can edit the index.php file.
This will most likely take care of your url issue.
You can edit the loop to show only categories from id: 3
Thus, you will make a querypost for category 3 as follow:
<?php query_posts('showposts=9'); ?>
See Query Post # Wordpress for more info.
Put this before the loop.
The coding may look like this:
<?php query_posts('cat=3'); ?>
<?php if (have_posts()) {?>
<?php while (have_posts()) : the_post(); ?>
<li>
<?php the_title(); ?>
<span style="float: right;"><?php the_time('m/d/y'); ?></span>
</li>
<?php endwhile; }?>

You could use .htaccess to map mywebsite.com/calendar to mywebsite/?cat=3, but WordPress will still look at the original url to find out which category to use, so this will not work.
I suggest you look in the code of the plugin to find out how it gets the current category, and maybe trick it (by an extra call to query_posts('cat=3')) to think we are looking at the url for category 3 (you don't need to display the posts, just do the query). You can even do this on your homepage (but maybe use home.php, not index.php, since the latter is a fallback for other template pages), so you don't need a redirect. But the key is that you need to look at the code of your plugin (can you tell us the name?) to find out how it works.

Related

Wordpress website with blog page structure

I've built a website (locally) that works just fine and, I've written the home page content in the Index page as I'm accustomed to and, different templates (page1.php, page2.php) for pages that required different layout for the 'About' and 'Contact' pages etc. Since deciding to add a blog though it has me stumped. I've written a blog page and it looks just fine but when I try to view a post I'm just directed back to index.php. I know it's down to the structure but it's confusing. An index page is mandatory, and you should probably have a front-page.php too. If that's the case..which one should I write my home page on? Index page doesn't show in the reading list and choosing "front page' as a static page works but still the blog links go back to index. If I was to put the content from the index page into front-page.php, what would I have in the index page?
Although the Wordpress docs are good I can't really find anything to suggest which way round all this stuff would be.
Your issue is that WordPress by default uses the index.php file for the blog archive (as well as other archives, etc.) and you need to use front-page.php for a dedicated home page with your custom content.
Read https://developer.wordpress.org/themes/basics/template-hierarchy/ on how to construct a standard WordPress theme with template files that follow a standard hierarchy.
The front-page.php template file is used to render your site’s front
page, whether the front page displays the blog posts index (mentioned
above) or a static page. The front page template takes precedence over
the blog posts index (home.php) template. If the front-page.php file
does not exist, WordPress will either use the home.php or page.php
files depending on the setup in Settings → Reading. If neither of
those files exist, it will use the index.php file.
Duplicate index.php and call it front-page.php. Edit index.php to be a basic template with a standard loop (and none of your home page content) so that it can function as a blog template that displays the_excerpt or the_content, something like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endwhile; else: ?>
<?php _e( 'Sorry, no posts matched your criteria.', 'textdomain' ); ?>
<?php endif; ?>
See https://developer.wordpress.org/themes/basics/the-loop/

Wordpress: Pages work, Posts don't

First of all, I'm totally new to WordPress so please be patient :)
Anyway, I have created a custom theme with a bunch of pages, a menu and a startpage (index.php), and everything is working fine. I have also created a few posts and those are not working at all. I have created a small loop that display the 5 latest posts on the startpage as links with date, and they show up as intended, but if I click on one of the links the startpage just reloads. As the page has reloaded it displays the correct permalink in the browser address bar, but I'm still stuck on the startpage. Same if I try to preview my posts from WP-admin, it just display my start page.
I have no idea what I'm doing wrong here and I'm no php coder. This is the code I have in my page.php file, maybe there is something wrong with it?
<?php get_header(); ?>
<div class="content">
<?php while ( have_posts() ) : the_post(); the_content(); endwhile; // THE LOOP ?>
</div>
<?php get_footer(); ?>
there is a difference between posts and pages. Try to put this code into a file called index.php so it will work for posts too.
If wordpress cant find a page.php (this is for pages) or single.php (this is for posts) it will look into the index.php as a fallback.
those are the files - enjoy :)
1. style.css: your page styling goes in here.
2. index.php: acts as fallback if the specific php files are not found.
3. single.php: single posts template
4. page.php: single page styling goes here
5. archive.php: acts as archives template (by month, author,...)
6. searchform.php: style and configure your searchform here
... there are many more. here is a cheat sheet with page types / theme structure .If you need further information google for types followed by tutorial
example: wordpress single.php tutorial
all the best
fab

How to use the Wordpress Shortlink (having post ID only) in tweets

Scenario: The urls and titles of the posts on my blog are usually very long. Whenever I or anyone else tweets the post, I don't find any space to add the hashtags or quoted retweet text. I don't want to shorten the titles of the posts, so the other option is to shorten the url. For this purpose I want to grab the shortlink from wordpress post in this format
http://mydomain.com/?p=(post ID)
Can you please tell me how to do it?
Use wp get shortlink
Returns URL based on numerical ID, i.e. http://example.com/?p=1234
https://codex.wordpress.org/Function_Reference/wp_get_shortlink
Usage
<?php $short_link = wp_get_shortlink(); ?>
or
<?php echo wp_get_shortlink(); ?>
Source File
wp_get_shortlink() is located in wp-includes/link-template.php.
You can go to Settings -> Permalinks and set it to 'default' (or 'numeric' if you prefer) and then call
<?php the_permalink() ?>
in your “tweet this” link.
or you can use this: Twitter Friendly Links WordPress Plugin

Wordpress search results on external page

I have a custom search engine on a non-wordpress page. This search engine searches a database for a city (specified by a text input) and then returns the relevant information about that city and displays it on the page.
What I want to do now, is to below those results, display the results of a wordpress search of the same term on a blog that resides on the same server/domain. So basically I want to show the same results that a wordpress search of that keyword would return, but I want to display them on a non-wordpress page, not in the blog's theme.
What I have:
a variable holding a search term, the search term has already been shampooed and conditioned to be search engine friendly.
What I don't want to do
I don't want to use an iframe and have the blog template be displayed on the page.
I can't have a secondary search box. I need to somehow use the value of the variable that I have, I can't ask the user to submit a second form or anything.
My ideas so far
Is there a way to run the wordpress search function and grab the data that it returns? I've gotten as far as including wp_blog_header.php on my static page so that I can make use of the wordpress functions.
Or would it be better to write a function that duplicates what the wordpress search does on the wordpress databases, but returns the data in a way that I can use?
Or is there a different approach that I should take for this that I've overlooked? Thanks!
You must first include the Wordpress files
<?php define('WP_USE_THEMES', false);
require('/server/path/to/your/wordpress/site/htdocs/blog/wp-blog-header.php');
then use query_posts and the loop
query_posts('s='.keyword);
while (have_posts()): the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<p>Read more...</p><?php
endwhile;

adding single.php page to wordpress or if condition for main page or post detail page

I use Barecity Theme for WordPress. i built everything, but now i need to add a single.php file to theme. now it displays all post content at homepage, I created a short_desc Custom Field. and I call it from code with;
<?php //get_post_meta($post->ID, 'short_desc', true); ?>
it is fine. but i need to display this short desc at home page listing, and the main content at details page. how can do that?
I appreciate helps!!
It sounds like what you are trying to do is show a custom field that you have set on a post on the index page (which lists all the posts).
To do that you'll need to modify index.php by adding your snippet where you would like to have the short description.
<?php echo get_post_meta($post->ID, 'short_desc', true); ?>
You need to use echo to display the results from the get_post_meta function.
Depending on how your posts are setup you can also use the More button when you write your posts. This will cut off your post at a certain point that you decide and only show that short part on the index and archive pages.
Another option would be to use
<?php the_excerpt(); ?>
Which shows the first 55 words (this can be adjusted though) of the post.
Hope that helps,
Paul

Resources