Wordpress display all pages on one page - php

I need to display all the pages on the one page.
At the moment I am using this to bring in each page:
<?php
$page_id = 5; //id example
$page_data = get_page( $page_id );
$content = apply_filters('the_content', $page_data->post_content);
$title = $page_data->post_title;
echo $content;
?>
But if a new page is created it wont display it as wont have this code and its ID in place..
Is there a way to bring all the pages in automatically?
Any help will be appreciated, thanks

You can get all the pages of your blog by using get_pages();, do this in a loop like so:
$pages = get_pages();
foreach ($pages as $page_data) {
$content = apply_filters('the_content', $page_data->post_content);
$title = $page_data->post_title;
echo $content;
}

The accepted answer's returned array doesn't contain "post customs" (which I needed in my case). And also you may need to retrieve pages with specific attributes. Use args for more customized results:
<?php $args = array(
'sort_order' => 'asc',
'sort_column' => 'post_title',
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'meta_key' => '',
'meta_value' => '',
'authors' => '',
'child_of' => 0,
'parent' => -1,
'exclude_tree' => '',
'number' => '',
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages($args);
?>
And use them like this:
<?php
foreach ($pages as $page) {
$title = $page->post_title;
echo $title. "<br />";
} ?>
Read more here

Also, if you are using the Twenty Ten theme (I don't know how will it work in others) you can easily get all the pages formatted:
echo "<h1 class=\"entry-title\">".$title."</h1>"
."<div class=\"entry-content\">".$content."</div>";

Well you can get the contents of each page by this code as well.
$content = apply_filters( 'the_content', $content );
Still there is huge problem if you are using custom templates for different pages you can get the content but not the template files on your one page

Related

Wordpress Recent Posts/Projects

Hi I have created my own custom post type within Wordpress to contain projects that i can call via my theme files.
I am new to creating my own themes. I currently am using the following code in my single.php file to call in related articles based on the category of the blog post.
<?php
// Default arguments
$args = array(
'posts_per_page' => 3, // How many items to display
'post__not_in' => array( get_the_ID() ), // Exclude current post
'no_found_rows' => true, // We don't ned pagination so this speeds up the query
);
// Check for current post category and add tax_query to the query arguments
$cats = wp_get_post_terms( get_the_ID(), 'category' );
$cats_ids = array();
foreach( $cats as $wpex_related_cat ) {
$cats_ids[] = $wpex_related_cat->term_id;
}
if ( ! empty( $cats_ids ) ) {
$args['category__in'] = $cats_ids;
}
// Query posts
$wpex_query = new wp_query( $args );
// Loop through posts
foreach( $wpex_query->posts as $post ) : setup_postdata( $post ); ?>
<div class="col-md-4 related-post">
<?php the_post_thumbnail('large'); ?>
<?php the_title(); ?>
</div>
<?php
// End loop
endforeach;
// Reset post data
wp_reset_postdata(); ?>
In my new post type "projects" i would like to call in related projects. Which im assuming would be very similar code except i need to stop it looking for posts and instead look for my projects.
Here is my code for new post type:
// Projects
add_action( 'init', 'create_post_type' );
add_post_type_support( 'bw_projects', 'thumbnail' );
add_post_type_support( 'bw_projects', 'custom-fields' );
function create_post_type() {
register_post_type( 'bw_projects',
array(
'labels' => array(
'name' => __( 'Projects' ),
'singular_name' => __( 'Projects' )
),
'public' => true,
'has_archive' => true,
'taxonomies' => array( 'category','post_tag'),
)
);
}
What would i need to change in my first code snippet in order to look for bw_projects and not look for 'posts' anymore. I tried playing around and changing certain lines myself but i caused more issues and stopped the page loading. Is this even right i can use the same code, slightly altered or would i need something completely different?
Thanks in advance.
You can get any post type that you require using get_posts();
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'projects',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
Simply set the 'post_type' argument to that of you custom post type, to get only these posts. You can also set the number of post, and filter by category etc.
You can find more info in the codex.
Alternatively, if you wanted to keep something similar to your existing code you could try using 'pre_get_posts' to filter the query to just your projects. However you'd need to remember to add / remove this filter so it only operates on the queries that need it.
To display the posts you can use a simple foreach to churn them out. You#d obviously want to do some sort of styling to get the layout correct:
$args = array("posts_per_page" => 10, "orderby" => "comment_count");
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
echo "<h1>" . $post->post_title . "</h1><br>";
echo "<p>" . $post->post_content . "</p><br>";
}
Or a really concise way of doing all of the above would be something like:
$args = array("posts_per_page" => 5, "post_type" => "projects");
$posts_array = get_posts($args);
foreach($posts_array as $post)
{
echo "<h1>" . $post->post_title . "</h1><br>";
echo "<p>" . $post->post_content . "</p><br>";
}

How to render formatted post content in widget in Wordpress

I am new on wordpress and try to make a widget which will only show the last post of particuler category.
TO do this I have given some settings in admin to select category for which admin wants to display last post (post of last ID).
I am using the below code to in widget to display post content:
$query_arguments = array(
'posts_per_page' => (int) 1,
'post_type' => 'post',
'post_status' => 'publish',
'cat' => (int) $settings['category'],
'order' => 'DESC',
'orderby' => (($settings['display_by'] == 0) ? 'ID' : 'date')
);
$posts_query = new WP_Query( $query_arguments );
if ($posts_query->have_posts()) {
$posts = $posts_query->get_posts();
$post = $posts[0];
echo $post->post_content;
}
But the above code is showing content in one paragraph or you can say that without format. I have done lot of search and found that, I need to apply "the_content" filter to format the content. So I have done the same as below code:
if ($posts_query->have_posts()) {
$posts = $posts_query->get_posts();
$post = $posts[0];
$content = apply_filters('the_content', $post->post_content);
echo $content;
}
Now the above changes is returning the null string. I have google lot of things but everyone is saying that to use apply filter or use the_content() function. I have tried both solutions but nothing happening.
Can anyone please share the solution for this problem?
please check whether you are getting any text in $post->post_content
by echo $post->post_content
All you need to do is,
$query_arguments = array(
'posts_per_page' => (int) 1,
'post_type' => 'post',
'post_status' => 'publish',
'cat' => (int) $settings['category'],
'order' => 'DESC',
'orderby' => (($settings['display_by'] == 0) ? 'ID' : 'date')
);
$posts_query = new WP_Query( $query_arguments );
while ($posts_query->have_posts()) {
$posts_query->the_post();
echo get_the_content();
}

How to wrap a foreach loop item in a link?

I think this question is php rather than WordPress...
I have a block of code that outputs pages which use a specific WordPress page template:
$pages = get_pages(array(
'meta_key' => '_wp_page_template',
'meta_value' => 'page-product.php'
));
foreach($pages as $page){
echo $page->post_title.'<br />';
}
My question is how do I wrap the post_title in a link (the_permalink() to the page itself)?
You would do the following:
foreach ( $pages as $page ){
printf( '%s<br />', get_permalink( $page ), $page->post_title );
}
Try this..
get_page_link( $page->ID )
In your code..
$pages = get_pages(array(
'meta_key' => '_wp_page_template',
'meta_value' => 'page-product.php'
));
foreach($pages as $page){
echo '' . $page->post_title.'<br />';
}
For more information Wordpress always has lots of example and information on what your using.
In this case I used actual code provided by Wordpress themselves, thus will be best way to do this. Please see https://codex.wordpress.org/Function_Reference/get_pages
Try this snippet:
$pages = get_pages(array(
'meta_key' => '_wp_page_template',
'meta_value' => 'page-product.php'
));
foreach($pages as $page){
///Do not forget to escape the title
echo '' . esc_html($page->post_title) . '';
}
Thanks for the suggestions. bodi0 - I liked the esc_html, thanks. I finally figured it out slightly differently with this:
<?php
$studentpages = get_pages(array(
'meta_key' => '_wp_page_template',
'meta_value' => 'students.php'
));
foreach($studentpages as $studentpage){
$studentlinks = get_page_link($studentpage->ID);
echo ''.$studentpage->post_title.'' . '<br />';
}
?>

Get an array of child posts IDs, wordpress 3.0, php

Ok here's one for ya...
On a custom template I'm using this code to retrieve & display a list of child pages/posts
$args = array(
'depth' => 1,
'show_date' => '',
'date_format' => get_option('date_format'),
'child_of' => $post->ID,
'exclude' => '',
'include' => '',
'title_li' => '',
'echo' => 1,
'authors' => '',
'sort_column' => 'menu_order, post_title',
'link_before' => '',
'link_after' => '',
'walker' => '' );
wp_list_pages( $args );
This works great, I'm also wondering how I can access/create an array of child post ID's. My goal is to access some custom fields meta data through the get_post_meta() function of each child post using it's ID.
Thanks guys.
I guess I wasn't very clear with this one as it's the first time I've never recieved an answer from SO.
I managed to find the information I needed and will place it here for anyone else browsing with the same request.
ok - To get all child IDs..
$pages = get_pages('child_of=X');
foreach($pages as $child) {
// Now you have an object full of Children ID's that you can use for whatever
// E.G
echo $child->ID . "<br />";
}
If you want to build an array of post ids for later use you can do this:
$pageids = array();
$pages = get_pages('child_of=X');
foreach($pages as $page){
$pageids[] = $page->ID;
}
And you have a clean array of just page ids.
$children = get_posts('post_parent=SLUG_OF_PARENT_POST&post_status=publish');
foreach($children as $child)
{
echo '<br/>ID:'.$child->ID;
}
you can use other attributes (i.e. $child->post_content)...
if you need to define post_type, then add this argument too : &post_type=POST_TYPE_NAME
Another way to do this:
$my_page_id = 12345;
$child_query_args = array(
'post_parent' => $my_page_id,
'post_type' => 'page',
'posts_per_page' => -1,
'fields' => 'ids',
);
$child_query = new WP_Query($child_query_args);
if ( $child_query && $child_query->have_posts() && $child_query->posts ) {
// (Since fields=ids, $child_query->posts is just an array of IDs)
$child_ids = $child_query->posts;
foreach ( $child_ids as $child_id ) {
$whatever = get_post_meta( $child_id, 'whatever', true );
echo esc_html($whatever);
}
}

retrieve only sub-pages

I want to list all sub-pages only one level though of one particular page. I was reading Function Reference/get pages and thought that $pages = get_pages( array( 'child_of' => $post->ID, 'parent' => $post->ID)) ; will do the trick but it is not working. It lists all pages on the same level like the page I call that code from. If I omit parent option I will get all pages even with sub-pages that I want. But I want only sub-pages.
The whole function is like
function about_menu(){
if (is_page('about')){
$pages = get_pages( array( 'child_of' => $post->ID, 'parent' => $post->ID)) ;
foreach($pages as $page)
{
?>
<h2><?php echo $page->post_title ?></h2>
<?php
}
}
}
below are screen shots from wp admin and results. Mine is second one
Screen shot from WP admin http://img687.imageshack.us/img687/6139/e2568e8ec2684e7aa1bb3d1.png and the result http://img269.imageshack.us/img269/2365/329c5097c78f4d3186a177c.png
Check out wp_list_pages(). I think these settings will give you what you want.
<?php
$args = array(
'child_of' => $post->ID, // current page
'depth' => 1, // get children only, not grandchildren
'echo' => 1, // print immediately when we call wp_list_pages
'sort_column' => 'menu_order', // sort by the menu_order parameter
);
?>
<?php wp_list_pages( $args ); ?>
try adding
global $post;
right before you declare $pages.
$paginas = array();
$paginas_obj = get_pages('sort_column=post_parent,menu_order');
$paginas['false'] = "Seleciona pagina";
foreach ($paginas_obj as $pagina) {
$paginas[$pagina->ID] = $pagina->post_title;
}

Categories