I want the CMS to have the different pages (e.g. "Careers", "Jobs", "Team") with each having its own template, but then to combine them into one big scrollable page (e.g. "Our Company") that would have a template. How would I do this?
I know there used to be a function get_page but that's been deprecated (and replaced with get_post which is not the same thing), but that doesn't retrieve the page's template.
I want both the page and the template so I can output both into the main page.
I also want it so if someone clicks in the navigation menu to go to "Jobs" or "Team", it will take them to that "Our Company" page, but with a querystring so I can scroll them to that part of the page
Is this possible?
First for main page template choose default template and write you global elements there. And in this template use get_template part to include pages
<!--custom query for pages-->
<?php
$args= array('post_type'=>'page');
$query= new WP_Query($args);
$query->while(have_posts()):query->the_post();
$temp_name= get_page_template_slug( $post->ID );
$temp_name_exp =explode('.',$temp_name);
get_template_part($temp_name_exp[0]);
endwhile;
endif;
?>
and in career, blog etc pages
<?php
/*
Template name: Career or blog or something else
*/
?>
<?php the_tiele();
the_content();
?>
for
"I also want it so if someone clicks in the navigation menu to go to "Jobs" or "Team", it will take them to that "Our Company" page, but with a querystring so I can scroll them to that part of the page"
assign each pages wrapper to page slug example <section class="<?php echo $post->post_name; ?>"> and write a function to redirect your view page link to http://yoursiteurl/#page-slug
EDIT
In order to get one page's content into another use the following function:
function show_post($path){
$post = get_page_by_path($path);
$content = apply_filters('the_content', $post->post_content);
echo $content;
}
and then create a template for the "Our company" page (like template-our_company.php) in which you will make a call to the function (e.g. <?php show_post('careers'); /* Shows the content of the "Careers" page using the slug. */ ?>).
So the template file should include something like this:
<?php
show_post('careers');
show_post('jobs');
show_post('team');
?>
For your 2nd question, you need to adjust the template-our_company.php file like this:
<?php
<div id="careers"></div>
show_post('careers');
<div id="jobs"></div>
show_post('jobs');
<div id="team"></div>
show_post('team');
?>
and then in the Menu dashboard, just adjust the navigation link to something like "/our-company/#careers" etc.
EDIT 2
In order to retrieve the content of pages with specified templates in another template, you can do the following:
Create the templates (files careers.php and jobs.php) and the posts that will be using those templates
/*
Template Name: Careers
*/
...
/*
Template Name: Jobs
*/
Then in the "parent" template, you can query the posts that have the above specified templates selected
untested code
$args = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_wp_page_template',
'value' => 'careers.php',
'compare' => '='
),
array(
'key' => '_wp_page_template',
'value' => 'jobs.php',
'compare' => '='
)
)
);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
the_content();
// or add anything else
endforeach;
wp_reset_postdata();
#user3418748's answer was a good start for me, but in my case I needed to load specific pages, and I found that just using get_template_part() by itself wasn't loading any content because I was doing it outside a loop. In order to get this to work you need to first set the global $post variable to the page/post you want to display. Here's the function I used (replace mytemplate with the name of your tempalte):
function mytemplate_show_page($path) {
global $post;
$post = get_page_by_path($path);
$tpl_slug = get_page_template_slug($post->ID);
$tpl_slug_exp = explode('.', $tpl_slug);
get_template_part($tpl_slug_exp[0]);
}
Related
I'm trying to make an option in theme for related posts
I want to display two different related posts ,one by tags and other by categories but I want to give the possibility to choose it from theme panel
My code in relatedposts.php
$relatedposts = get_option('theme_relatedoptions');
if (get_option('theme_relatedoptions') <> '') {
get_template_part( 'templates/related/'.$relatedposts.'', get_post_format() );
} else {
get_template_part( 'templates/related/tags', get_post_format() );
}
Code in Admin Functions
$options[] = array( "name" => __('Related Videos','framework'),
"id" => $shortname."_relatedoptions",
"type" => "radio",
"options" => array("tags", "category"),
"std" => "tags");
tags.php in related folder displays related posts by tags
and category.php displays the relatedposts by category but I dont know where so,
Im not sure here
get_template_part( 'templates/related/'.$relatedposts.'', get_post_format() );
Can anyone help me? thanks
You can't actually pass a $variable directly to a template called with get_template_part(); because the files are required: http://codex.wordpress.org/Function_Reference/get_template_part
To use a variable directly within get_template_part(); you need to declare the variable as global or pass a custom $arg to WP_Query.
That being said I don't see why you would need to pass a variable to get_template_part(); directly here. Could you simply read the variable (the category or tag in this case) and then pass that to WP_Query as an argument somewhat similar to this:
$query = new WP_Query( 'tag='$variable );
Agh this is frustrating. I've been searching for hours on how to do this.
I am creating a WP template that pulls in queries of a category on the page.
My question is how can I make the category in the array dynamically load, whether by slug or whatever, based entirely on what the page title is?
The fancy piece is that it will be three sections on the page via an additional category that also need to be loaded in the template, but those are not dynamic.
So if I can break this down:
Section 1 - TitleCat + Section1Cat
Then
Section 2 - TitleCat + Section2Cat
Then
Section 3 - TitleCat + Section3Cat
That way during content creation, my team can simply click two categories, and the website will automatically put the post excerpts where it needs to go!
It needs to be dynamic so I don't need to build a template for each of the 31 categories the posts are divided into.
EDIT: This is how I was approaching it using ACF. That array can be simply switched out for categories if that's the way to go. Same concept.
<?php $pagecat = $post->post_title; //this copies the page title the page title ?>
<?php
// args
$args = array(
'posts_per_page' => 4,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'page',
'value' => $pagecat,
'compare' => '='
),
array(
'key' => 'section',
'value' => 'Second String',
'compare' => '='
)
)
);
$the_query = new WP_Query( $args );
?>
On any custom / template page you can use this many times over, for example on home page you want to show samples of articles in many different categories:
Code Sample
<!-- Create query -->
<?php query_posts( array ( 'category_name' => 'category-slug-goes-here', posts_per_page' => 1 ) ); ?>
<?php // The Loop
while ( have_posts() ) : the_post();?>
// CODE THAT YOU WANT HERE
<?php endwhile; ?>
// IMPORTANT RESET QUERY
<?php // Reset Query
wp_reset_query(); ?>
Now you can do this OVER and OVER again as many times as you need on one template...
Now for displaying your SINGLE article for some category you can do this in a "Single.php" page use http://www.wordpress.org website for more references on this..
Here is the code for single.php page you need an IF statement and you can target your specific categories that you want to display a specific way...
<?php if(have_posts() && (in_category('category-name-here') || in_category('category-name-here-2') || in_category('category-name-here-3') )) : while(have_posts()) : the_post(); ?>
// NOW HERE use HTML to format the article for those categories.
Now you said you have 30+ different categories I assume you want different displays..
Now you will use that same IF statement over and over again and change the HTML format code in the middle. So you will have in the end ALL page layouts inside 1 page and you will target them by category-name
Best of luck!
Can I use:
<?php
if (function_exists( 'wpp_get_mostpopular' )) {
wpp_get_mostpopular('range=weekly&order_by=views&limit=8');
}
?>
Inside custom template (inside query posts) to make query with own template output:
<?php query_posts(????????); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="myclass">
<?php echo get_the_post_thumbnail( $post_id, 'thumbnail'); ?>
</div>
Please help me with this, so I don't have to edit plugin and plugin's css while I have my ready just to query posts by this plugin.
Thanks.
Plugin: http://wordpress.org/plugins/wordpress-popular-posts/
#alen imeko as far as i have understoody your requirment, why you need this plugin, you just need to show the most popular post where ever you need to, You just need to set the post to sticky and call the sticky post using the same query post method then it will automatically render your specified post at the respective place.
For this just go to post to make it sticky, at the top above the publish/update button you will see an option visibility click the edit part of it and you will the options will slide down check the checkbox having text Stick this post to the front page and after that you just need to perform query.
$args = array(
'post__in' => get_option('sticky_posts'),
'posts_per_page' => 20,
'orderby' => 'title',
'post_date' => 'DESC',
'cat' => your category id
);
query_post($args);
?>
Then you need to simply use the while loop for that the same way you do in wordpress.
In the website every product is a post, but when we add new products we want something like a newsletter, mostly like a post so in the sidebar of the home page you can see the new products or events of the month.
I'm using pages because I don't want to re-post a product on every new newsletter so I junt wanna display the posts inside the page.
In the products page I separate every product by category and sub-category but since I want to group specific post to publish them on the sidebar I think that pages was the best way to do it.
Right now I'm using this code:
<?php
$productos = new WP_Query(array(
'post__in'=> array(81, 83),
'orderby'=>'title',
'order'=>'ASC'
)
); if ($productos->have_posts()) : while ($productos->have_posts()) : $productos->the_post();
?>
It display the posts with the id of 81 and 83, I would like to show post by slug using 'name' as the codex says because is going to take some time to be checking the ids of the new post, instead of using the name of every new product but It doesn't work in array or I'm doing something wrong.
Now I will love to make something like this work
$names = get_post_meta($post->ID, "names", $single = true);
$productos = new WP_Query(array(
'name'=> array($names),
'orderby'=>'title',
'order'=>'ASC'
)
);
So every time I publish a new page I just write the slugs of the posts that I want to include in the page in a custom field, as you can see I'm not very good with php but I trying to learn and I search a lot for something that could work before asking in here.
I try the ggis inline post plugin and although it works I need the id for every post I want to include and I will need to edit the plugin because I want a different order in the output of the post thats why I don't like to depend to much on plugins.
Update:
So I'm now looking if I can make this using shortcodes, right now I have this:
function producto_func($atts) {
extract(shortcode_atts(array(
'nombre' => ''
), $atts));
global $post;
$pieza = get_page_by_title($nombre,OBJECT, 'post');
echo '<h1>'. $pieza->ID . '</h1>';
}
add_shortcode('producto', 'producto_func');
enter code here
So I just enter the shortcode [producto nombre="ff 244"] in the page and it show its ID, and I can add any number of shortcodes depending on the number of post I need.
But how can I show the entire content of the post.
Any idea?
I find I solution using Shortcodes.
So I put this on my functions.php page
function productos($atts, $content = null) {
extract(shortcode_atts(array(
"slug" => '',
"query" => ''
), $atts));
global $wp_query,$post;
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query(array(
'name'=> $slug,
));
if(!empty($slug)){
$query .= '&name='.$slug;
}
if(!empty($query)){
$query .= $query;
}
$wp_query->query($query);
ob_start();
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<h1><?php the_title(); ?></h1>
<div><?php the_content() ?></div>
<?php endwhile; ?>
<?php $wp_query = null; $wp_query = $temp;
$content = ob_get_contents();
ob_end_clean();
return $content;
}
add_shortcode("producto", "productos");
And in my page template I just write [producto slug="MY-SLUG"] and that way I can display multiple post just with the slugs. Hope someone find this useful.
From the Wordpress Codex:
Display post by slug:
$query = new WP_Query( 'name=about-my-life' );
Display page by slug:
$query = new WP_Query( 'pagename=contact' );
UPDATE
Try changing this:
'name'=> array($names),
To this:
'name'=> $names,
The 'name' - and 'pagename' - parameter does not take in an array. Only a string. A comma delimited list SHOULD give you what you need from within your Custom Fields titled "names", though I haven't tested this approach.
Also, thank you for using WP_Query instead of query_posts.
How do I include the page content of one or more page in another page?
ex. I have pageA, pageB and pageC and I want to include the contents of these pages in pageX
is there a wordpress function that loads the post of a specified page/post?
like show_post("pageA")??
There is not a show_post() function per se in WordPress core but it is extremely easy to write:
function show_post($path) {
$post = get_page_by_path($path);
$content = apply_filters('the_content', $post->post_content);
echo $content;
}
Note that this would be called with the page's path, i.e.:
<?php show_post('about'); // Shows the content of the "About" page. ?>
<?php show_post('products/widget1'); // Shows content of the "Products > Widget" page. ?>
Of course I probably wouldn't name a function as generically as show_post() in case WordPress core adds a same-named function in the future. Your choice though.
Also, and no slight meant to #kevtrout because I know he is very good, consider posting your WordPress questions on StackOverflow's sister site WordPress Answers in the future. There's a much higher percentage of WordPress enthusiasts answering questions over there.
I found this answer posted on the Wordpress forums. You add a little code to functions.php and then just use a shortcode whenever you like.
function get_post_page_content( $atts ) {
extract( shortcode_atts( array(
'id' => null,
'title' => false,
), $atts ) );
$the_query = new WP_Query( 'page_id='.$id );
while ( $the_query->have_posts() ) {
$the_query->the_post();
if($title == true){
the_title();
}
the_content();
}
wp_reset_postdata();
}
add_shortcode( 'my_content', 'get_post_page_content' );
For the shortcode,
[my_content id="Enter your page id number" title=Set this to true if you want to show title /]
Pages are just posts, with a post_type of 'page' in the database. You can show the content of multiple pages on another page by writing a post query in your pageX template that gets the posts you specify and output them in a Loop.
There are three ways to get post content from the database:
get_posts
query_posts
WP_Query
These links all point to the WordPress Codex. Get_posts and query_posts have an argument available, 'page_id', where you can specify the id of the page you'd like to retrieve and display.
You could install a Plugin "Improved Include Page". Once installed, you create page X and enter:
[include-page id="123"]
[include-page id="124"]
[include-page id="125"]
where these are the ID's of pages A, B and C respectively
<?php query_posts('p=43');
global $more;
//set $more to 0 in order to only get the first part of the post
$more = 0;
// the Loop
while (have_posts()) : the_post();
// the content of the post ?>
the_title();
the_content();
endwhile; ?>
This is obviously a portion of the post, I got the detail from the wordpress codex.
Interesting... I looked around for how to embed Wordpress content elsewhere (as in, on another website), and found some things...
www . shooflydesign.org/buzz/past/embedding_wordpress . html
Shows how to embed Wordpress content in another site with a php script; maybe just use the same thing to embed Wordpress into itself?
www . corvidworks . com/articles/wordpress-content-on-other-pages
Similar concept; embed Wordpress on another page; just try to use that tool in a new WP post
feeds themselves
My searching pulled up some suggestions to just use a feed to your own blog to embed into a post. There's nothing preventing that. You might want it automated and so restructuring the feed to look right might be problematic, but it's worth a shot depending on what you want to do.
Hope those are quasi-helpful. While they all are solutions for getting your WP content on some place other than WP... they might work for your given question and allow you to display A, B, and C on X.
There is a Wordpress function to display the content of a particular page inside another using query_posts() it is:
<?php query_posts("posts_per_page=1&post_type=page&post_id=134"); the_post(); ?>
You set the number of pages to be displayed to 1, post type is page instead of post and the page id
Kit Johnson's wordpress forum solution with creating a shortcode works, but adds the inserted page in the top of the new page, not where the shortcode was added. Close though, and may work for other people.
from the wordpress post, I pieced together this which inserts the page where the shortcode is put:
function get_post_page_content( $atts ) {
extract( shortcode_atts( array(
'id' => null,
'title' => false,
), $atts ) );
$output = "";
$the_query = new WP_Query( 'page_id='.$id );
while ( $the_query->have_posts() ) {
$the_query->the_post();
if($title == true){
$output .= get_the_title();
}
$output .= get_the_content();
}
wp_reset_postdata();
return $output;
}
Then, the shortcode bit works as expected. If you don't want the title, title=false does not work, you need to leave title off entirely.