Page display the All page Content in wordpress - wordpress

I have display all page content in current page but not current page content to be display so bellow code give me all page content but how can i filter it ?
Thanks.
$pages = get_pages();
foreach ($pages as $page_data) {
$content = apply_filters('the_content', $page_data->post_content);
$title = $page_data->post_title;
echo $content;
}

Try this code: it will exclude the current page by taking its ID.
<?php
$args = array(
'post_type' => 'page',
'numberposts' => -1,
'sort_order' => 'ASC',
'sort_column' => 'post_title',
'post_status' => 'publish',
'exclude' => get_the_ID()
);
//$allpages = get_pages($args ); ?>
<?php wp_list_pages( $args ); ?>

Related

Render child page on parent page in Wordpress

How can I render a child page on the parent page in wordpress? I'am building a landing page website, and the idea is to use child pages to make landing page structure.
Now I'am using this code in my parent page template:
$args = array(
'post_type' => 'page',
'post_parent' => $post->ID,
'orderby' => 'rand',
'posts_per_page' => 1,
'no_found_rows' => true
);
$child = new WP_Query($args);
var_dump($child->posts);
But it just gives me an array, and I need fully rendered HTML of my child pages.
Thank you in advance)
Try this code here:
$args = array(
'hierarchical' => 0,
'child_of' => $post->ID,
'parent' => $post->ID,
'sort_column' => 'menu_order, ID',
);
$pages = get_pages( $args );
foreach ( $pages as $post ) : setup_postdata( $post );
// child page html content here
endforeach;
//reset to the main page
wp_reset_postdata();
Finally I've found the proper solution, inspired by Matt Browne's answer
functions.php
function eatwings_show_page($pageid)
{
global $post;
$post = get_page($pageid);
$tpl_slug = get_page_template_slug($post->ID);
$tpl_slug_exp = explode('.', $tpl_slug);
get_template_part($tpl_slug_exp[0]);
}
parent-page-template.php:
$args = array(
'post_type' => 'page',
'post_parent' => $post->ID,
'orderby' => 'menu_order, ID',
'posts_per_page' => 1,
'no_found_rows' => true
);
$child = new WP_Query($args);
foreach($child->posts as $childpage)
{
eatwings_show_page($childpage->ID);
}

Woocommerce single-product page - Show all variation images

For a special gallery i need to show all woocommerce variation images. On the woocommerce content-single-product.php i can access the variations but i cant get the image url out of it. How can i do that?
Inside my content-single-product.php overwrite:
<?php
$args = array(
'post_type' => 'product_variation',
'post_status' => array( 'private', 'publish' ),
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'asc',
'post_parent' => $post->ID
);
$variations = get_posts( $args );
echo "<pre>"; print_r($variations); echo "</pre>";
?>
You can do something like this.
$product = new WC_Product_Variable( $product_id );
// get the product variations
$product_variations = $product->get_available_variations();
if ( !empty( $product_variations ) ) {
foreach($product_variations as $product_variation) {
echo $product_variation['image_src'];
}
}

How to Hide post that have same category

is there someone who knows how to hide post that have same category?
I have Post 1, Post 2, Post 3 that have same category. Can i just show the latest post ( post 1) at my homepage, and hide other post ( post 2 and post 3 )
One way of doing that is to have three separate wp_queries.
//Query 1
$args1 = array(
'cat' => '1',
'nopaging' => true,
'posts_per_page' => '1',
); $query_1 = new WP_Query( $args1 );
if ( $query_1->have_posts() ) {
while ( $query_1->have_posts() ) {
$query_1->the_post();
// put content here
}
} else {
// no posts found
}
wp_reset_postdata();
//Query 2
$args2 = array(
'cat' => '2',
'nopaging' => true,
'posts_per_page' => '1',
); $query_2 = new WP_Query( $args2 );
if ( $query_2->have_posts() ) {
while ( $query_2->have_posts() ) {
$query_2->the_post();
// put content here
}
} else {
// no posts found
}
wp_reset_postdata();
//Query 3
$args3 = array(
'cat' => '3',
'nopaging' => true,
'posts_per_page' => '1',
); $query_3 = new WP_Query( $args3 );
if ( $query_3->have_posts() ) {
while ( $query_3->have_posts() ) {
$query_3->the_post();
// put content here
}
} else {
// no posts found
}
wp_reset_postdata();
<?php
$args = array(
'numberposts' => 1,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true
);
// the query
$the_query = new WP_Query( $args );?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Add your category ID to 'category'.
reefer these links for more information.
https://codex.wordpress.org/Class_Reference/WP_Query
https://codex.wordpress.org/Function_Reference/wp_get_recent_posts
You can achieve this by alter the query of home page throught pre_get_posts hook.
Here is the code:
function txt_domain_get_distinct_post_id()
{
$post_ids = [];
//getting all non empty category
$categories = get_terms('category', array(
'orderby' => 'count',
'hide_empty' => 0,
));
foreach ($categories as $category)
{
$args = array(
'posts_per_page' => -1,
'category' => $category->term_id,
'exclude' => $post_ids,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
//getting post from a specific category
$postlist = get_posts($args);
foreach ($postlist as $post)
{
if (!in_array($post->ID, $post_ids))
{
//this will only select one post form each category
$post_ids[] = $post->ID;
break;
}
}
}
return $post_ids;
}
add_filter( 'pre_get_posts', 'txt_domain_filter_get_posts' );
function txt_domain_filter_get_posts($query)
{
if (is_home() && $query->is_main_query())
{
$post_ids = txt_domain_get_distinct_post_id();
$query->set('post__in', $post_ids);
}
return $query;
}
Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
Reference:
is_home()
get_terms
get_posts

how to get related posts list in home page

how can i get list of latest posts with their relative posts by tags?
example:
Latest post post title 1
related post
related post
Latest post post title 2
related post
related post
use this in Index.php
<?php
$args = array(
'numberposts' => 100,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' =>,
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true );
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
<ul>
<?php
foreach( $recent_posts as $recent )
{
echo '<li>'.$recent["post_title"];
$tags = wp_get_post_tags($recent["ID"]);
if ($tags)
{
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($recent["ID"]),
'posts_per_page'=>100,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() )
{
echo '<ul>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php
endwhile;
echo '</ul>';
}
wp_reset_query();
}
echo '</li>';
}
?>
</ul>
you can do something like this
your main loop contain recent post so during each loop use following function to get it's tag
$tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
then you can use another loop of using those tags
$query = new WP_Query( 'tag_id='.$tag_ids );
now $query has content you want.

Wordpress - Loop with images from post to

I made an Wordpress theme, with pages and posts.
The loop of posts show me a short brief of post and a Continue reading link.
I like this, but how can I make the theme show in the post brief of the loop image(s) attached to post at beginning, if any.
Thank you!
You can get your attached images by using:
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => 1,
'orderby' => 'menu_order',
'order' => 'ASC',
'post_parent' => $post->ID
);
$images = get_posts($args);
and display it like this:
echo wp_get_attachment_image($images[0]->ID, $size='attached-image');
This for getting all attachement images with your post.
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ($attachments) {
foreach ( $attachments as $post ) {
$img = wp_get_attachment_image_src($post->ID, 'medium');
$fullsize = wp_get_attachment_image_src($post->ID, 'full');
}
}
You should add in your loop:
<?php
if(has_post_thumbnail()) {
$theimage = wp_get_attachment_image_src( get_post_thumbnail_id ( $post->ID ), 'thumbnail' );
}
?>
<img class="img_class" src="<?php echo $theimage[0]; ?>" />
Where "thumbnail" correspond to the size you want to show.
Remember that there is also a WordPress specific site in StackExchange

Resources