How to use PHP in the header of a drupal view? - php

I have a drupal view, which displays a content type filtered by a url argmuent (e.g. category). Now i'd like to add a link in top of my view, which allows my users to add a new node. In the add form the argument field should be prepopulated with the value from the filter.
I think to archive this, i have to install the prepopulate module and add some php-code to the header section of my view, which generates the link.
Any suggestions how to manage this?

If you are wanting to add php code to get the arguments passed to views in the views header section, do the following:
Make sure that the PHP filter is turned on; this is a module that can be enabled
In the header section use the following code:
$view = views_get_current_view();
// [0] is first arg, [1] is second etc.
$argumentOutput = $view-> args[0];
Remember to have the input format set to PHP code. This will get you the argument passed to views.

It would be a lot easier to override the template for the view and insert the code/markup there. Check out the Theming infomation inside the view to find out what to call your template.

Related

WordPress replace page with a plugin

I am trying to make a plugin that will display custom page when a parameter is present in the URL. For example, let's say that the URL of a post is localhost/wp/2018/03/22/postname/, it displays post normally as expected. What I want to achieve, is when I specify URL as localhost/wp/2018/03/22/postname/?param=1, I want to display page that I will generate with the plugin. I made a function that works when the parameter is present and it echoes some example content, however, it merges with what WordPress normally displays. So when I put the parameter into URL I get a regular page with post and my content somewhere in the middle. I want to display only the page that i generate from scratch, from <html> to </html> with my plugin. How can I do that?
There is a filter in WordPress called template_include this filter is executed before any output is displayed on the WordPress generated page.
You can override the current template via this filter as soon as you find the parameter and value in the URL. E.g. below is a filter I was using in a project to override archive & single template for a certain CPT:
function em_templates($template) {
if(get_query_var('post_type') == 'xyz' ) {
return FMM__DIR__ . '/templates/archive-xyz.php';
}
return $template;
}
add_filter('template_include', 'em_templates', 1, 1);
You can adopt this logic for parameter and use this WordPress filter to take over the template process and display yours own.

Change header on specific page wordpress?

I want to change my header (all header : logo + menu) on specific pages of my wordpress.
EDIT : I didn't precise something important, in my mind, I woudl like to change the header on specific page AND tagged post.
Example : header test on page id 4, and post 3,4 & 5 wich are tag "test".
How can I manage that easily ?
I tried some plugins without success :
WP Display Header
Page Specific Menu Items
Menu Swapper
I have the idea to change it with an if statement, but I'm worried about maintaining this code in time (I'm thinking about WordPress or Theme updates).
Have you already do that ?
Thanks a lot,
<?php if(is_page(<page id>)) { ?>
--------- Header 1 -----
<?php } else { ?>
--------- Header 2 --------
<?php } ?>
Add this code according to your header sections in header.php of your theme.
When you use get_header() an optional parameter is the name of the header, you can see this in:
https://developer.wordpress.org/reference/functions/get_header/
You need to create, for instance, header-another.php and put the code in it, after this only need calls get_header('another').
Probabilly you need to create a custom template for the page that has a different header and call get_header('another') instead get_header() in it.
There are some bullet points you have to know about using different on different page.
first if you want to use different headers on different pages for example you want header(one) on productOne-page.php and header(two) on productTwo.php file.
For this First you have to create two header files namely header-one.php and header-two.php.
now when you call header(one) in your productOne-page.php file you call it like:- get_header('one');
and when you call header(two) in your productTwo-page.php file you call it like:- get_header('two');
and at last if you need to call default header in any file you can call just get_header();
summery: just call different header with their name after hyphen(-) in file name i.e. get_header('Custom Header Name');
I hope it will help you.:)
I'm using the plugin Page Menu (by Rohit Kumar) to change the menu and the Haninder Singh solution to change the header and it work perfect.

Broken SilverStripe Template

For some reason, after doing a ?flush=all, a certain page type is not able to locate it's default template. I figure out that it's not loading it's template file after appending showtemplate=1 to the URL. The dumped raw template shows nothing for the default template.
Pastebin: http://pastebin.com/uMLefAsP
I wish someone could point me to the right direction here, for I have no idea where to start debugging.
Thanks, Jan.
Firstly, and I have to ask: Is there actually a PageType called "CommunityExtensionPage"? PageTypes need to be named the same as the required template, for the template to be picked-up automatically.
You seem to have two template files "CommunityExtensionPage.ss". One at "templates/CommunityExtensionPage.ss" and one at "templates/Layout/CommunityExtensionPage.ss"
It would be useful to see the contents of both files. SilverStripe will look for "CommunityExtensionPage.ss" in the top-level of the "templates" dir before looking in "templates/Layout".
If "templates/CommunityExtensionPage.ss" is found, it will also try and look for "templates/Layout/CommunityExtensionPage.ss" and render this into the $Layout template variable. Otherwise, it'll use the default "templates/Page.ss" and request "templates/Layout/CommunityExtensionPage.ss"
Does your "templates/CommunityExtensionPage.ss" template contain a reference to $Layout? If not, then the contents of "templates/Layout/CommunityExtensionPage.ss" will not be rendered.

Custom Taxonomy Term page in Drupal 7

I'm trying to make a custom Taxonomy Term page in Drupal 7. I've created a page--taxonomy.tpl.php file in my templates folder. The file only prints out a message. I now try to force the template file by adding
function template_preprocess_page($variables) {
if (arg(0) == 'taxonomy') {
$variables['template_file'] = 'page--taxonomy-tpl';
}
}
in my template.php, but it won't work. Can you help me? And if I get the custom page working, how do I fetch the nodes with this term (in page--taxonomy.tpl.php)? Thanks in advance.
Try using this in your template.php:
function template_preprocess_page(&$variables) {
if (arg(0) == 'taxonomy') {
$variables['theme_hook_suggestions'][] = 'page__taxonomy';
}
}
You need to pass $variables by reference, so add a & before it
template_file has changed to theme_hook_suggestions in Drupal 7
You don't need the -tpl in the template suggestion unless you want it to be a part of the filename like "page--taxonomy-tpl.tpl.php" which I don't think is what you want.
For more information, check out template_preprocess_page(), theme_get_suggestions() and Working with template suggestions
Not sure if this would meet your requirements, but one of default D7 views - Taxonomy term - emulates Drupal core's handling of taxonomy/term pages. You could just enable it (it would automatically replace Drupal's core taxonomy URLs), and then do whatever you want with it, keeping original page structure, all blocks etc, using Views' page templates (see "Theming information" in "Advanced") and all other bells and whistles...
Since you are using Drupal 7, you could also create a file name "taxnomy-term.tpl.php" and edit according to your needs.
See taxonomy-term.tpl.php
Full control over the taxonomy term page can be obtained using hook_menu_alter() . See https://drupal.stackexchange.com/questions/48420/theming-and-overriding-taxonomy-term-vocabulary-page/111194#111194

Drupal View (Page) vs Taxonomy

I have the following problem:
I use taxonomys (tx) as tags. They can be added when the node is created. So I don't know how many tx I have or what ID they have.
The path of the tx is like the following:
/foo/element1
/foo/element2
/foo/element3
...
The secound element is the tx.
Now I want to use a view (page) to handle the tx-path:
/foo/%
The problem is, when I open a path like the one on top I see the theme of the node-taxonomy.tpl.php but not the style I set in the view.
Whenever I open a path in the form (/foo/not-a-tx) I can see the output of the view.
Could someone give me a hint how to get out the view output but not the tx-output?
Thanks
Sebastian
I solved the problem with this way:
I use a view block (not a page)
I added a new output area in my ,info file
I use this way to show only the vocab
I show the block in the new area online bei foo/*
It works Okay for me.
Thx to every one.
Do you want to get rid of the taxonomy pages completely?
If so, you can use a hook_menu_alter() and unset the taxonomy page.
EX.
hook_menu_alter(&$items) {
unset($items['taxonomy/term/%taxonomy_term']);
}
You'd have to look at the $items array to pinpoint the name of the registered menu path, but I think this is it.
This will remove the taxonomy page for all vocabularies however.
Actually you need to make a view to override the internal drupal path of the taxonomy term page: taxonomy/term/% (where % is the taxonomy id) and not the aliased path, which in your case is foo/%
[Optional but saves work: There is already an example view that is bundled with Drupal that implements the taxonomy view. Go to Views > List and you will see the the view is greyed out and it is called
Default Node view: taxonomy_term (default)
All you need to do is enable it and modify it to your needs]
Don't worry about the aliases. You can define your URL pattern at /admin/build/path/pathauto (make sure pathauto module is enabled. You can download it at http://drupal.org/project/pathauto ). In your case the pattern would be foo/[cat] where [cat] is a token for category. Make sure you enter this pattern under Taxonomy Term paths in the pathauto automated alias settings.

Categories