Drupal - Template for Custom Page - drupal

I've got a custom page I created programmatically using hook_menu. I can have it return content without a problem. However, I want it to appear within the content region of my page.tpl.php template so that blocks can be incorporated in the page. Same way that Views allows you to give a view a custom url while still appearing in the page.tpl.php template.
How do I do this for my page? By default, I just get a with only my content.
Thanks,
Howie

In your page callback function, if you just print the content of your page without returning anything, it won't be included within your page.tpl.php. If you return a string containing the contents of your page, it will go into the content region, i.e.
function mymodule_page_callback () {
$content = "Hello, world inside the content region";
return $content;
}

Related

Buddypress register page pulling wrong template

I have created a register page and tried to set a template for it by selecting the template from the dropdown in the right column ie: Page attributes->template (dropdown). When I view the register page the form is displaying correctly but it isn't using the template I set for the rest of the content. I can tell that it is instead using page.php from this snippet I used where it should be using template-other.php
function show_template() {
global $template;
print_r($template);
}
add_action('wp_footer', 'show_template');
The funny thing is that if I change the URL to a capital ie: from example.com/register to example.com/Register then it uses the right template but the buddy press form doesn't show up. But if I use example.com/register then the form shows up but it doesn't use the template I set. Any suggestions would be greatly appreciated.

Two pages with same content

How to create two pages with same content?
For example, the first page should be with a header menu and the second page should be without a header menu.
The page of my theme has an options without header
Or I can use two different templates of page, but the content of pages should be the same.
You can read the documentation here.
Create a file, called page-mytemplate.php under your theme directory.
In that file, start with this:
<?php
/*
Template Name: My Custom Page
*/
Create a page, and set the template to My Custom Page.
At here you can do whatever you want.
For the other page, repeat the steps, but just call the other template something different name.
EDIT
When you want to show the same content with different templates, just simple get the content of the page:
$post = get_post($postId);
echo $post->post_title ."<br />";
echo $post->post_content;
You can wrap this with your two template page. See the documentation here

Add dynamic content before the loop in Wordpress

I'm developing a content slider for Wordpress and I would know how is possible to put this content slider (as html content) into the body before the loop.
I tried to filter the content with add_filter('the_content', 'functionName), but I get the content slider before each post.
If you use add_filter('the_content'), your function will be called everytime the content of a post is output, whether a single post or a series of posts in a loop. If you need to "hook" before any post content is output in a page, the only dynamic parts of all WP themes you can reach are get_header() or get_sidebar() (or event get_footer). So your best luck would be not to use a filter with the content, but an action, with get_header, like this :
add_action('get_header', 'your_function'); // Add priority & param args if necessary
The problem is that this gets executed before header.php is called, and usually, the body tag is opened in header.php...
That is if you cannot modify the theme itself. Otherwise, you can easily add an action in the theme itself and execute it where you want.
Another technique would be to add your html content after document is ready, through JavaScript which you can output in the footer.

Multiple thumbnails in WordPress based on page template?

Is there a way to add multiple thumbnails in a page based on page template or page ID
instead of post type (post, page)?
You can add a conditional statement to test for a specific page id:
if(is_page('<your page id>')){
// Do your thing
}
Similarly, you can test for a specific page template like so:
if(is_page_template('<page template file name>']){
// Go on wit' you bad self
}
Make sure that when you use is_page_template() that you feed it the file name of the template, e.g., is_page_template('two-columns.php'), as opposed to the name of the template, e.g., is_page_template('Two Columns').

WordPress - how to insert html code selectively in posts

I want to display custom html code when a post is rendered (so not when is inserted into the database).
I currently do this with add_filter('the_content', 'my_custom_method'). The only problem is that I want this do be displayed only inside the post (when is viewed in its own page), not when all posts are rendered .
I banged my head against the wall, but couldn't find any method to tell me if i'm currently inside an individual post or not (this has to work for every url rewriting possible, so i can't rely on url)
Is there such a method? I believe it should be, but i can't find it. Thanks.
the function for checking if the post is in its own page is is_single()
add_filter('the_content', 'my_custom_method');
function my_custom_method(){
if(is_single()){
//code for your custom html code
}
}
the function is_single() checks if the page being rendered is a single page or not.
The easiest way to do this would be to modify your templates. Wordpress template sets should have a file named single.php (inside wp-content/themes/<theme name>). This is the page that gets rendered when you are viewing the page for a single post.
You could edit this file and insert whatever you needed to for the posts there.

Resources