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 );
Related
I'm having trouble setting up my Wordpress template. I'm not sure how to achieve the following. My website setup is the following:
I have a Custom Post Type "Chapter". A "Chapter" is parent to other CPT's. I have a few post types such as: reviews, interviews, blogposts, ...
On a chapter page I do a different WP_Query for every post type within this chapter.
page template single-chapter.php
Now I want to be able to click on "Blogpost (2)" and open an archive page of all blogpost within the current chapter. How can I achieve this? I assume I should create a template page "archive-blogpost.php". I already found that I can link to this page using:
<?php echo get_post_type_archive_link( 'blogpost' ); ?>
However I can't see how this page could know in what chapter i'm currently in?
I never used the WP Types plugin so far, but here is what you could do: add a chapter parameter to the archive link (with a rewrite rule for it), that you'll get in your blogposts archive template and display the posts conditionally to this parameter.
First we change the archive link to send the chapter slug:
<?php echo get_post_type_archive_link( 'blogpost' ) . '/' . $post->post_name; ?>
Then we define in functions.php this new rewrite tag:
function custom_rewrite_tag() {
add_rewrite_tag('%chapter%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
To make the URL looks good, we'll add a rewrite rule (so you don't have a url like /archive/?chapter=chapter-1, but /archive/chapter-1) - this still go to functions.php:
function custom_rewrite_rule($rules) {
add_rewrite_rule('^archive/([^/]+)/?$', 'index.php?post_type=blogposts&chapter=$matches[1]', 'top');
}
add_filter('init', 'custom_rewrite_rule', 10, 0);
You may have to change the URL / post type name depending of your configuration.
And last, you can get this query arg in your blogposts archive template with $wp_query->query_vars array:
$wp_query->query_vars['chapter']
Since I don't know much about WP Types, I'm not really sure about what follows, but it seems you could query the childs posts of the chapter with this:
if(isset($wp_query->query_vars['chapter']) && $chapter = get_page_by_path($wp_query->query_vars['chapter'])) {
$childargs = array(
'post_type' => 'blogposts',
'numberposts' => -1,
'meta_query' => array(array('
key' => '_wpcf_belongs_property_id', 'value' => $chapter->ID
))
);
$child_posts = get_posts($childargs);
} else {
// default template : display all posts
}
I used a WP types functions to achieve what I wanted:
$child_posts = types_child_posts("blogpost", array('post_id' => $_GET['wpv-pr-child-of']));
With this I can query all the child posts from the custom post type on a page.
I also use a session variable to keep track of the current chapter I'm in.
I want to have a static front page on my WordPress and this static one should display the latest 5 posts and below that up to 6 custom meta fields. Thats why I changed the reading settings from last posts to "Static page", because otherwise I would not have the possibility to access the custom meta fields.
How can I now include the latest 5 blogposts on my front-page and use the default post template/output.
I did it with this query:
$latest_blog_posts = new WP_Query( array( 'posts_per_page' => 5 ) );
if ( $latest_blog_posts->have_posts() ) : while ( $latest_blog_posts->have_posts() ) : $latest_blog_posts->the_post();
// Loop output goes here
endwhile; endif;
Do i now have to rewrite the code for the post here completly or can I just include the post template in some way?
Thanks!
You need to write this line to show latest posts:
<?php
wp_get_archives(array('type' => 'postbypost', 'limit' => 10, 'format' => 'html'));
?>
Check this link for more details
Instead of WP_Query you should try
wp_get_recent_posts( $args, $output );
Check more details here
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]);
}
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.