My customer has a Wordpress blog with a page named "Galleries" that serves as a kind of main page as he manually writes there links to other pages, both local and external.
The main menu for the blog has also a link to this Galleries page. Problem is that the menu item only is active for that page, but not for the linked local pages, unless you previously selected Galleries as their parent page.
So I was wondering if it will be possible (with a hook) to virtually force an ancestor to a page that meets certain conditions before the menu is rendered, avoiding the need to manually edit every page and make it a child to "Galleries"
You'd have to find a filter that works in the very early stage of assembling the menu data, or do some heavy array juggling once it's already build.
I'd suggest another approach:
Update the parent for all currently existing pages via a simple database query. wp_posts contains a column post_parent - so create a query that filters out all of your pages that satisfy the criteria you mentioned (how easy or hard that will be, depends on what your criteria are - might involve JOINs with other tables like wp_postmeta, wp_terms), and then update the parent for those. This is a one-time thing, so it can be done via phpMyAdmin or some database frontend like that. Keep in mind that since "pages" are ordinary posts internally, and stored in the same table, you will have to filter for only those posts that are pages in the first place.
Create a function that hooks into the post save/update actions, in there again check if the criteria are matched, and if so set the parent page for the post at that point in the PHP data structure, before it gets written away to the database.
Finally found how to do it:
$main_gallery_page= get_page_by_title('Galleries');
add_filter('nav_menu_css_class' , 'force_ancestor_nav_class' , 10 , 2);
function force_ancestor_nav_class($classes, $item){
global $main_gallery_page,$post;
//condition: could be any condition: by category, title, contents, ...
$make_ancestor=substr( $post->post_title, 0, 7 ) === "Gallery";
//only adds the class if the condition is met and not already present
if ($item->object_id==$main_gallery_page->ID && $make_ancestor && !in_array('current-page-ancestor',$classes)) {$classes[]='current-page-ancestor';}
return $classes;
}
Related
I have 1 custom table (not wp core table) where are specific products imported. I've set up custom wp page where these products are listed, custom filter created etc. In this step so far everything is OK - products are listed and filtering works, pagination works...
But what I want to extend further now - open each product page (like /product-listing-page/product-name-1) when clicking on product names in product list page... How can be this achieved "out of wp_posts" table?
Why I'm not using wp_posts table: web site have static pages and dynamic posts as news and product table is truncated and re-imported once in a day every night! So, can not really use wp_posts page when product list is dynamic and totally truncated before each import...
Any ideas? Maybe anybody already did such stuff?;-)
I don't know if you developed a plugin to handle all the custom table and code you created, but if you didn't, I strongly encourage you to do so.
Because by making your code as a plugin, you have the oportunity to use all the composants of wordpress, like the rewrite rules system, which, I think, could help you to achieve your goal.
I created a plugin some times ago for a website I have the charge and I needed a custom page to be displayed within the website (worpdress) url system, which is what you want to do if I correctly understand your question.
Unfortunately the plugin has evolved since and I didn't keep a backup of that code, but if I remember correctly how I did it back when, here is the rough approach I followed:
I create a plugin which handle :
custom db table(s)
custom php objects
...
AND (this is the intersting part) add rewrite rule to wordpress rewrite rules system as follow:
set rewrite tag (init action) (if needed)
add rewrite rule (init action)
intercept url and parse it to get the params i need in my custom
page (parse_request action)
The following functions may help you with that :
add_rewrite_tag
add_rewrite_rule
And you need to hook in :
init
parse_request
Hope it helps you start.
the solution was quite quick and easy! For my needs it was enough just to register global query var:
function register_new_query_var($vars) {
$vars[] = 'product_id';
return $vars;
add_filter('query_vars', 'register_new_query_var');
and then I changed theme template for page product-listing-page to work for product listing and individual products with using
if(!get_query_var('product_id')). So, if query empty - do listing stuff, if not empty - do individual product stuff!
For me this is quite enough and I do not need even seo friendly urls (because product list changes every day, so - every day will be new products, links, and there will be to much 404 errors)...
and I can use http://example.com/product-listing-page?product_id=1 and page displays with date which is from custom table! ;-)
I would like to know how I can display single posts for a specific page.
ex. page url
http://www.yourblog.com/services/single-service-post-title
I tried creating "single-service.php" but it redirects me to my static front page.
I have setup a page-services.php and that one works fine, but that page is designed to only show a summary of posts.
It's not entirely clear what you're looking for, since pages can't have single posts. They're two different things.
If you're looking for a way to create a template for a single custom post type, you can view an interactive version of the hierarchy here.
You have to name the file based on the id of the custom post type. So if the id of the post type is 'service', then 'single-service.php' would display the single post where post type equals service.
You can also place logic into your 'single.php' to say something like
if(get_post_type(get_the_ID()) == 'service'){
// DO SERVICE-SPECIFIC STUFF HERE
}
This is usually for single custom post types that don't have their own template, but it can also be used as a fallback if that's something you're worried about.
Then you can use an else for regular post stuff and put stuff common to both outside of the if/else.
I want to load all categories grids on one page and want all products to be available on scroll down. currently I need to click on each category and the page refreshes for each category. This is the site I am working on(ahmad.esy.es). I want this type of functionality upon clicking the category(http://www.just-eat.co.uk/restaurants-hertsplaice-en11/menu#2254). kindly suggest me what changes should I make in view files (product.tpl, category.tpl) or controller files(product.php, category.php). how can I load all the grids of categories in one page
I didn't look at the site you have posted above - if you cannot describe your issue by words or by submitting a code or image it should be improved.
From what I understand you just want an infinite scroll to display all the products you have in your eshop. It is not mentioned whether you want to preserve the default functionality (category tree with products attached to certain categories) or whether you want only this one listing - basically it does not matter - so I will describe in short how to add such new functionality.
The model
you do not need to touch any model - you can reuse the method ModelCatalogProduct::getProducts()
this will give you the possibility to sort or filter the products except you will have to omit the filter_category_id to load all the products
The controller
feel free to create a new controller (e.g. ControllerProductInfinite) or just to reuse one of the already existing - either ControllerProductCategory or ControllerProductProduct
all you need is basically the same as in ControllerProductCategory::index() except adding filter_category_id into a array passed when calling ModelCatalogProduct::getProducts() and a new template for rendering
The template
create a new template that will meet your expectations and infinite scroll requirements
stick to any infinite scroll plugin/implementation you like (may Google be with you)
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.
OK, another problem with Expression Engine. I know it pretty well but i'm sort-of just learning categories!
I've searched around for this answer but can only find the same thing. I need to have a next button on a title_permalink page that goes to the next entry in that category. I know how to manually specify a category but that won't work as it will only ever give me entries from that category! I need a way for ee to know what category the entry is in. My code is very simple at the moment, it's :
{exp:channel:next_entry}Next Project{/exp:channel:next_entry}
I could add category_id="2" but then what happens whenever the person goes into a category that isn't 2, they are all using the same view template.
My structure is like follows:
Category Selection page - list of categories using category_name tag
Project List page - links here using channel:entries tag
Project View page - linked here by a title_permalink tag
Thanks for any help!
The default ExpressionEngine Next/Previous Entry Linking Tag Pairs allow you to generate links to the next or previous entry, based on the date of the current entry.
However, the biggest limitation is that they not as robust when it comes to restricting or filtering entries with multiple categories.
If your needs are simple, you can use the following code to restrict the Previous/Next links to showing entries within the current entry's category:
{exp:channel:entries channel="channel_name"}
...
{exp:channel:prev_entry category="{categories}{category_id}|{/categories}"}
« Previous
{/exp:channel:prev_entry}
{exp:channel:next_entry category="{categories}{category_id}|{/categories}"}
Next »
{/exp:channel:next_entry}
...
{/exp:channel:entries}
You'll notice that the {exp:channel:prev_entry} and {exp:channel:next_entry} links are nested inside the {exp:channel:entries} tag and use the {categories} tag pair to restrict the Next/Previous links to the current entry's category.
Realize, however, that this code works best when entries are classified using a single category.
Otherwise, when using the {categories} tag pair with an entry having multiple categories, the Previous/Next links will default to the first category output ... which may be different than the next or previous entry's order of categories.
For a more robust solution, you'll find there are many third-party solutions on Devot-ee for entry linking.
Two of my favorite plugins for handling linked entries are Nearby Entries by Adam Khan and Entries List by Laisvunas.