I am new to CodeIgniter and I am creating a personal blog. I have done the UI part and I am displaying 260 chars from the article-content.
Where I'm stuck:
When the user click on the article's title, it should go to the page with the URL (E.g.: example.com/title-one). But, I don't know how to fetch the corresponding Content of that title. Both title and content are in the same table coupled with ID.
How can I pass the ID and use that to fetch Content?
When you are creating a post, you can add a field for your slug. Some varchar column would fit. Every slug has to be unique, so you can find the article in your database.
For creating slugs with CI, I would recommend this:
In the url helper you have a method called url_title. Just pass the articles title:
$slug = url_title($title, '_', TRUE);
Then save it with your additional data to the database.
When you are viewing an article. Do something like this:
$slug = $this->uri->segment(n);//change it to fit;
In your fetching model method for viewing an article, just find the article by the given slug.
$this->db->get_where('your_articles_table', array('slug' => $slug));
Also you can add some additional routing, to perfectly fit your needs.
Related
Hey Guys I am new to Laravel and I am currently trying to pass a slugged url into my controller, but I am having issues with it.
I have a table for categories and I have all the categories looped through and displayed on a view page (/posts). I listed all the categories on this view, so that when a user clicks on a category, they will be taken to a different page showing all posts belonging to that category(e.g, /posts/category/{categoryID}). I know I can get this done by passing the category id to the controller and finding the id to pass back to that view. However, I am trying to do something different.
I am looking to pass the category name instead of the id to the controller. E.g, /posts/category/{category name}. But due to the fact that some categories are more than a word (e.g, Dry Cleaning), it has become a challenge for me. I had earlier slugged the URL for SEO purpose (e.g, /posts/category/{Str::str(categoryname)}) and from the web.php file, I created a get route.
Now, I'd like to fetch all posts relating to the category name passed in. The issue is that the category names are slugged.
Is there a way to remove the slug("-" or hyphen) from the name passed into the controller? So, instead of "Dry-cleaning", it will come back as "Dry cleaning" which matches the name in the database.
I am trying to avoid passing id to the url.
Thanks.
Good question.
There are a couple of approaches you can use to tackle this problem. I'll run through them for you.
1. Store the slug in the database
One option is to store the slug name within the database. Personally this is my favourite option as you don't need to do any string transformations. With this approach I would put a slug column in your categories table. Then you can use the Category eloquent model to search for a category with that slug.
2. Convert the slug to title case
The second option is to convert the slug to title case. You can do this by using Laravel's string class. For example:
$categoryName = Str::title($slug);
However there are a few issues with this approach. Firstly, when creating a slug the url will remove characters that are not safe, for example an apostrophe. This means if you have a pluralised category name it won't match when converting the slug back to title case.
Secondly, by using code to transform strings in order to match a record in the database you are at the mercy of the service class. While the functionality of Str::title is unlikely to change, it could in theory which could then mean you have to change code later on down the line.
Conclusion
Overall, I would recommend going with the first approach. You have much better data integrity and less chance of things going wrong further down the line. So if I were you I would create a new migration to add a slug column to your categories table and then populate each category with its own slug. Then search for a category based on the slug column when querying your eloquent model.
I am using the Post Type Builder for Wordpress to create Custom Post Types.
I would like to get the name of a meta field in the front-end. I am able to get the values, but I would like the names. How could I do this?
For example, I have made a custom post type for a machine selling website.
The user is able to add a new machine (post type), add the building year, dimensions, weight and so (meta data).
I would like to display: Year: 2001. How can I get "Year"?
Thank you in advance!
The best way would be to use the Post Type Builder template editor (https://themify.me/docs/post-type-builder-plugin-documentation#templates).
If not that, then the simple answer is that you already know what the name of the field is since you're passing the key in. You can just hard-code that into your page template.
The final method would be to unserialze and parse out the ptb_plugin_options item from the wp_options table. This is one big blob that holds all of the config info for the plugin. You would retrieve it something like:
$ptb_plugin_options = unserialize(get_option('ptb_plugin_options'));
echo $ptb_plugin_options['cpt']['POSTTYPE']['meta_boxes']['text_1']['name'];
Replacing POSTTYPE with the actual posttype you're using and text_1 with the meta_key of the box in Post Type Builder.
So I built a site (In the wrong way which I can see now).
Users can post ads, they are not stored as a post, I just store the information in a table in the database.
I use a page template with parameters passed in - For example www.site/ad/?id=1&cid=2
id = id from database
cid = category id from database
I then use a wordpress page, pull params from url and load all information from DB relating to the id and category id.
In the wordpress admin there is a page set - the page uses an ad detail template for all urls that are www.site/ad/?
What I want to do now is put the ad title in the url so www.site/ad/adtitle/?id=1&cid=2
The issues with this is the page will not load as the permalink now does not correspond
perma link is www.site/ad/?
so cannot use www.site/ad/adtitle/? and still load the page with template.
I guess the question is can I make so that the page will load the url with the ad title in
I try to set permalinks to allow custom structure but no luck
Apologies about title didn't know what to call this
Fixed it with pods, which will allow you to use a page referencing a part of a url
http://blog.stefanxo.com/2014/02/how-to-create-a-pods-page/
Thanks
Im building a micro CMS. Using Mysql as RDMS, and Doctrine ORM for mapping.
I would like to have two types of pages. Static Page, and Blog Page.
Static page would have page_url, and page_content stored in database.
Blog page would have page_url, but no page_content. Blog would have Posts, Categories...
Lets say I have route like this:
/{pageurl}
This is page, with page url that can be home, or news, or blog...
That page can be either Static page, and then I would joust print page_content.
But it can also be Blog Page, and then I would print latest posts as content.
How should I relate these Static Page and Blog Page tables?
Is this inheritance, since both are pages, with their URL, but they have different content?
Should I use inheritance, so that both Static and Blog page extends Page that would have page_url? Or should I made another table page_types and there store information about available page types?
Generally, and I'm speaking with a MVC framework in mind, when you use routes you would have a specific pattern that you can match to different controllers/actions that could then map to different models and views.
For instance, a standard page URL would be in the format:
/{pageurl}
Whereas a page URL for a blog page would be:
/blog/{pageurl}
This would make it very easy to distinguish between the two and route accordingly. If it matches the pattern /blog/*, it's a blog! However, let's assume you don't want to take the easiest method - but instead decide to have all URLs follow the same pattern: /{pageurl}. Your actual list of URLs should all be stored in a single table in the database, let's say it's named site_pages. The relationship between your site_pages and blogs would be a column in blogs named page_id that is a foreign-key back to the site_pages table.
With this, you have a few ways you could determine what type of page you're looking at:
Add a new column to site_pages table, such as is_blog; it it's set, you have a blog!
Every time you look up the page in the database, also get a count() of blog entries for the specific page; if it's > 0, you have a blog page. Otherwise display it as static content.
Make an assumption that if page_content is empty, it's a blog.
Once you have established if the page is a static page or a blog page, you could load a corresponding Model - "Page" or "BlogPage". BlogPage can, and probably should, extend Page since they have the same purpose. The only difference between the two is that "Page" just loads the page's text-content and the View simply writes it out. The "BlogPage", on the other hand, would load the list of Categories, Posts, etc and the View would iterate through them and display them however you see fit.
I have a custom posttype in Wordpress 3. I would like to every post of this type to have the option to add a link. Basicly this will be a link that refers to another page/post whatever on the site.
There should be only one link for each post of this type. And i would then need to extract this link in my template files. Basicly im creating a post-type "Slideshow" And each slide-item should be connected to one page or post. So when you click a slide you will be taken to the defined page.
i know i can do this by using a custom-field. But then i would need to enter the whole url every time. I would like a feature similar to that of the wordpress WYSIWYG editor link button. So i can add a link to "existing content" easy.
Anyone know of any tutorials or similar on how to do this?
Thanks!
bit surprised no one has mentioned this plugin "Related Links"
Wordpress plugin Related Links
It adds a metabox to your edit forms. You can link to related content or put in external URL.
It allows links to posts, pages, media AND Custom post type - check its type on the Settings page of the plugin after install. It uses a similar search/browse facility to the normal WYSIWYG link insert feature!
What post-types does it apply to? If you are using on a custom post type, then check all the post-types you want to be able to link to - it still shows the box on this post-type itself.
It can accept multiple links, but one will work of course. Then you use the get_related_link() function in your template to output this and format as you like.
If you just want to make a link between the two posts, rather than insert it in your post content, I'd recommend the Posts 2 Posts plugin. It'll allow you to create links between posts without having to remember the full URL.
Edit:
I haven't used it, but I suspect the cardinality argument should help you limit the number of links - see the wiki.
And you can certainly access the connections in your templates - I have. Once you've registered your connection type, you can just call get_connected:
$venue_details = p2p_type( 'exhibition_to_venues' )->get_connected( $post->ID, array(
'posts_per_page' => -1,
'connected_orderby' => 'order',
'connected_order' => 'ASC' )
);
It seems to me that the best method to accomplish this would be to add a custom meta box to all posts and post types (see here:http://themefoundation.com/wordpress-meta-boxes-guide/). Within this meta box, you can simply query all posts that you would like to include in a dropdown. You can then select the post from that dropdown, obtain the ID of the post selected (use as data attribute in the option field), and then return the permalink for that ID. This will then allow you to simply select the post, rather than having to know the actual URL each time.
Another method would be to attach a piece of unique data to x post (most likely utilizing custom fields), and then also attach it to y post. In this way, you could use a function to automatically append the link to the displayed post. You would do this by querying the posts in the database, match the custom data, and if matched, display the link to that post. This would allow the entire thing to be automated, and you wouldn't even have to select anything. In my opinion, the title field should actually be sufficient for this, since both posts are different post types (you should be able to title them the same), and would likely make your query a bit easier/shorter since you would simply need to find the post that matches the title, and then link to the permalink.
I can elaborate on all of the above further, with code samples as well, but in my opinion, the question is slightly too vague to write a custom script example for this scenario.
I would hope the above would be sufficient to get you going.