im trying to make a custom hardcoded menu in a wordpress page-template. The site has 3 pages (1, 2, 3) and at page 1 i wish to loop trough all pages that has page1 as pagetemplate and 2 for 2 etc.
How can i loop trough and get the names of all pages so i can put them in a menu?
if for example you visit page 1 and the pages that has page1 as template (parent) are "visit us" and "read more" the menu would look like this:
-visit us
-read more
but if i go to page 2 the menu might look like this:
-funpark
-foobar
Now if I add a new page with "1" as parent and name the page "about" the new menu will now show
-visit us
-read more
-about
Cheers,
Emil
You can query for pages and their template value. The codex says:
The filename of a Page's assigned custom template is stored as the value of a Custom Field named '_wp_page_template' (in the wp_postmeta database table). (Custom fields starting with an underscore do not display in the Edit screen's Custom Fields module.)
This means you can build a page query like this:
$args = array(
'post_type' => 'page',
'meta_query' => array(
array(
'key' => '_wp_page_template',
'value' => '[Your template goes here]'
)
)
);
All you have to do is loop through the pages, get the permalink and build your menu.
Related
In my WordPress v5.7, currently I have two custom post_type: song, & poem.
I have a custom author.php template with author profile and latest 10 posts from both post_type with no pagination, as required by site design. Here is the author template at pastebin (https://pastebin.com/kCrYebcD).
If the author has more than 10 posts from both post_type, I want to show all posts from both post_type in archive.php template with pagination. Here is the archive template at pastebin (https://pastebin.com/twkWn5Bc).
In author.php I have this URL to redirect to all post archive page:
<?php
$author_page_link = esc_url(get_author_posts_url(false, ID->user_nicename));
echo 'All';
?>
This is how the URL constructs: http://www.local.site/author/one/?post_type[]=song&post_type[]=poem.
The above URL is redirecting back to author.php page. If I use only any one post type as below, the URL stays in the archive.php.
All
How can I show a single author's all posts from selective custom post_type in the archive.php?
This is how I have solved:
I have created a custom page template author-all-posts.php with page slug all. I have used the custom URL parameters as below:
http://www.local.site/all/?author_id=1
In my custom page template, I have the below code:
// get the author ID
$authorID = $_GET['author_id'];
Below the WP_Query to get all the posts from multiple post_types:
$all_args = array(
'author' => $authorID,
'post_type' => array('song', 'poem'),
'posts_per_page' => -1
);
query_posts($all_args);
I am working on add_rewrite_rule to red rid of the ugly URLs next.
I'm creating a Wordpress template from scratch for my company's website.
I don't know much about PHP but I know my old company has succeded in doing the operation I want to do but I don't find any solution on the Internet...
I would like to display in a parent page, several child page and the template page that they are linked to. That way, I could display the template page as a block that can be re-used in several other pages.
For example I created a template named home.php that will be the parent page, and a custom template introduction.php with one of my block structure. In wordpress I have created a parent page for my homepage, and a child page with my template introduction.php linked.
I would like to display in my parent page, the child page and the template content to have the structure + the content of the child page.
In each parent page, I'll have several child that can have the same custom template.
I find this solution better than creating a full static page and giving it the ID of the pages or putting my HTML directly in a page editor, but I don't know if this solution exists.
For now I have only find solutions to loop child page into the custom template and then display the template with "get_template_part" into the home.php, but it displays all my templates even if they aren't link with a child page and shows the content twice...
Thank you if you can help me with this problem and sorry for my broken English.
So you want a 'parent page' that displays all the 'child page contents' in the 'parent page' itself like blocks
add this in your parent template file,
`<parent page content here>
<?php
$args = array(
'post_parent' => $post->ID,
'post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'DESC'
);
$query = new WP_Query($args);
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
$template = get_post_meta( $post->ID, '_wp_page_template', true );
$template = pathinfo($template);
$template_parts = explode('-', $template['filename']);
get_template_part( $template_parts[0], $template_parts[1] );
endwhile;
endif;
?>`
This should get the child pages to the parent page, here i will be using the template file like this 'template-introduction.php' and i will be adding the 'introduction' as the template name.
This is assuming you have different templates for the child pages.
I have't tested this, in theory this should get you what you need.
I am making an e-commerce site with WordPress, Woocommerce, and BuddyPress. We allow members to upload images of their products. The product category name is the same as the member's username. So if exampleUser1 uploads a product image, that product is given the category 'exampleUser1'.
This is a temporary method that my team and I are using so members can see all of their products on one page (product-category page).
I want to create a dynamic URL that I can display via a button that links to the product-category page of the logged in user. I have researched how to do this using functions.php and passing variables to the URL but am having no luck getting anything to work.
Thank you for your help!
You should change the category to a constant value for all users like productImage or something. Then no need for a dynamic URL, the same URL will work for all users. Just create a page template and use WP_Query in the loop like this:
$author = get_current_user_id();
$args = array(
'author' => $author,
'post_status' => 'any',
'category_name' => 'productImage',
'post_type' => 'attachment'
);
$query = new WP_Query( $args );
i am developing a CMS site in wordpress ...
the final web page should some what look like :
Website
I want just one page (front page) and on that i want to display subpages titles in it. (About,Home,People)...
So when the subpage title is clicked it should expand and display the the page content in the panel,on same page and at same place.
any ideas regarding same?
Thanks in advance...:)
EDIT1
[accordions]
[accordion title="Accordion 1"]
Accordion 1 Content here
[/accordion]
[accordion title="Accordion 2"]
Accordion 2 Content here
[/accordion]
[accordion title="Accordion 3" last="last"]
Accordion 3 Content here
[/accordion]
[/accordions]
after puting the above code in the page content looks like this:
but on click of ACCORDION 1 its not getting expanded..
You can write your own template, which does a query (checks for a subpage), and adds its content between div tags.
When it's done, then it's time for JS/jQuery . Search for tab jQuery scripts, so you get some information.
Anyway, you will mostly need to use hide() / show() functions to reach something like that.
Also, did you made that template, and are you familiar with programming in WP?
<?php
$args = array(
'numberposts' => -1,
'post_parent' => $post->ID,
'post_type' => 'page',
'post_status' => 'publish',
'orderby' => 'menu_order,title',
'order' => 'ASC'
);
$my_pagelist = get_children($args);
?>
Above is code for displaying subpage content, below is the jQuery tutorial for tabs.
http://jqueryui.com/demos/tabs/
EDIT:
Also, check this site - http://www.learningjquery.com/2006/09/slicker-show-and-hide it will show you some info about show/hide functions. Also more information about WP get_children function is found here - http://codex.wordpress.org/Function_Reference/get_children
If I understand your question correctly, in the latest version of WordPress.
Login to your admin panel.
Click on the 'Settings' tab, then the 'Reading' sub-tab.
Change the 'Front page displays' option to 'A static page'.
On my WordPress site, I'm using a plugin called "Vera Meta Boxes" that lets me add custom meta boxes to pages (not referring to custom post types).
So now every page has meta box that says, "Show on Home page" with a checkbox, that when selected has the value of "yes".
Now on my homepage, I want to show the titles and featured image of any page that has "Show on Homepage" checked.
The vera meta box plugin says to use:
<?php get_post_custom_values('your_custom_field_key_here'); ?>
So I would use:
<?php get_post_custom_values('show_on_homepage'); ?>
But how do I do the rest? Conceptually, it would be something like:
LOOP Query Pages > if <?php get_post_custom_values('show_on_homepage'); ?> = yes show title and featured image and repeat loop until all pages with "show_on_homepage" are shown.
Well, I haven't tested this, but according to the docs you should be able to use this query:
array('post_type'=>'page', 'meta_query' => array( array('key' => 'show_on_homepage') ) )
Note that meta_query is an array of arrays
This is 3.1 code, the 3.0 version should look like this:
array('post_type'=>'page', 'meta_key' => 'show_on_homepage')