How to List all Child pages in wordpress? - wordpress

I have a parent page A with with child pages X, Y, Z, D
I want to display link to all child pages on page X and Y
When you go to page X and Y, you should see a list of all child pages X, Y, Z, D
For some reason I get only the child page (page D)
I am using this function.
$args = array(
'sort_order' => 'asc',
'sort_column' => 'post_title',
//'hierarchical' => 1,
'exclude' => '',
'include' => '',
'meta_key' => '',
'meta_value' => '',
'authors' => '',
'child_of' => A,
'parent' => -1,
'exclude_tree' => '',
'number' => '',
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages($args);
if ( $pages)
{
foreach($pages as $page)
{
$page = '<ul> <li> ' .$page->post_title . ' </li></ul>';
}
return $page;
}
What am I doing wrong?

In the example, you are failing to close your if statement. Also you can use echo with your foreach instead of return. I cleaned up your code a little bit.
<ul>
<?php
$args = array(
'sort_order' => 'asc',
'child_of' => '1',
'post_type' => 'page',
'post_status' => 'publish'
);
$pages = get_pages($args);
if ($pages) {
foreach ($pages as $page) :
echo ' <li> ' . $page->post_title . ' </li>';
endforeach;
}
?>
</ul>

I fixed it and it worked
I modified my foreach
$links = '';
foreach($pages as $page) {
$links .= '<ul> <li> ' .$page->post_title . ' </li></ul>';
}
return $links;
Before I used
$links = instead of $links .=

Related

WordPress post title query is not working with special character

This title query doesn't work, in which the post title have a special character before the title. I check the post he title. For example : -y-
Here is the code the post single page template:
<?php
$get_s_value = $_GET['search'];
if ($get_s_value == 'true') {
$current_p_title = get_the_title();
$args = array (
'post_type' => 'trending',
'post_status' => 'publish',
"s" => $current_p_title,
);
query_posts($args);
if (have_posts()): the_post();
$sahifa_trending_count = get_post_meta(get_the_ID(), '_trending_count', 1);
$update_count = $sahifa_trending_count + 1;
$last_m_date = get_the_modified_date('Y-m-d');
$trending_c_date = date('Y-m-d');
if ($last_m_date = $trending_c_date) {
update_metadata('post', get_the_ID(), '_trending_count', $update_count);
} else {
update_metadata('post', get_the_ID(), '_trending_count', '1');
}
else:
$create_post = array (
'post_type' => 'trending',
'post_title' => get_the_title(),
'post_status' => 'publish',
'meta_input' => array (
'_trending_count' => '1',
),
);
wp_insert_post($create_post);
endif;
wp_reset_query();
}
Please try encoding the title.
$create_post = array (
'post_type' => 'trending',
'post_title' => utf8_encode(get_the_title()),
'post_status' => 'publish',
'meta_input' => array (
'_trending_count' => '1',
),
);

wp_list_categories depending of a selected tag

I would like to display a list of categories with the current count of posts in each category depending of a selected tag.
For example if the tag A (2 posts tagged) is selected the categories list would be:
- cat A (2)
-- cat Aa (1)
-- cat Ab (1)
And if the tag B (3 posts tagged) is selected:
- cat A (3)
-- cat Aa (1)
-- cat Ab (1)
-- cat Ac (1)
As there is no possibility to specify a tag in the arguments of wp_list_categories, did you have ideas about how to process?
Thanks
You have a bunch of arguments which wp_list_categories function could be applied to. One of them is show count which is a boolean 0 -> false, 1 -> true. Check the child_of parameter too.
$args = array(
'show_option_all' => '',
'orderby' => 'name',
'order' => 'ASC',
'style' => 'list',
'show_count' => 0,
'hide_empty' => 1,
'use_desc_for_title' => 1,
'child_of' => 0,
'feed' => '',
'feed_type' => '',
'feed_image' => '',
'exclude' => '',
'exclude_tree' => '',
'include' => '',
'hierarchical' => 1,
'title_li' => __( 'Categories' ),
'show_option_none' => __( '' ),
'number' => null,
'echo' => 1,
'depth' => 0,
'current_category' => 0,
'pad_counts' => 0,
'taxonomy' => 'category',
'walker' => null
);
wp_list_categories( $args );
https://codex.wordpress.org/Template_Tags/wp_list_categories
With the use of wp_list_categories, it is not possible to combine with post tag. So first filter posts based on tag id and then based on those posts ids, find categories like shown in below code :
<h2><?php _e('Categories by Tags'); ?></h2>
<form action="" method="post">
<?php wp_dropdown_categories('taxonomy=post_tag&show_option_none=Select tag&orderby=name&name=posttag&selected='.$_POST['posttag']); ?>
<input name="btnSubmit" id="btnSubmit" type="submit" value="View" />
</form>
<?php
if(isset($_POST['btnSubmit']))
{
$the_query = new WP_Query( array( 'tag_id' => $_POST['posttag'] ) );
$categories = array();
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$post_cat = get_the_terms($post->ID, 'category');
foreach ($post_cat as $key => $value)
{
if($value->parent==0)
{
$categories[$value->term_id]['main_category']=$value;
}
else
{
$categories[$value->parent]['sub_categories'][$value->term_id]=$value;
}
}
}
echo "<ul>";
foreach ($categories as $cat_key => $cat_value)
{
foreach ($cat_value as $c_key => $c_value)
{
if($c_key == "main_category")
{
echo "<li>".$c_value->name." (".$c_value->count.")";
}
if($c_key == "sub_categories")
{
echo "<ul>";
foreach ($c_value as $sc_key => $sc_value)
{
echo "<li>".$sc_value->name." (".$sc_value->count.")</li>";
}
echo "</ul></li>";
}
}
}
echo "</ul>";
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
}
?>
This should work
<?php
$termID = 25; //Need cat ID
$termName = 'category'; // Need tax name
$catMain = get_term_by( 'id', $termID, $termName );
$termchildren = get_term_children( $termID, $termName);
if (is_array($termchildren) ) {
$return.='<ul>';
$return.='<li>'.$catMain->name.'('.$catMain->count.')</li>';
$return .='<ul>';
foreach ($termchildren as $child) {
$childTerm = get_term_by( 'id', $child, $termName );
$return .='<li>'.$childTerm->name.'('.$childTerm->count.')</li>';
}
$return .= '</ul></ul>';
}
echo $return;
?>

Wordpress categories hierarchy

I can't seem to find out why this code doesn't output the categories in hierarchy:
<ul>
<?php
$args = array(
'show_option_all' => '',
'container' => false,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'use_desc_for_title' => 0,
'child_of' => 0,
'hierarchical' => 1,
'number' => null,
'echo' => 1,
'depth' => -1,
'taxonomy' => 'category'
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '<li>' . $category->name . '(' . $category->count . ')</li>';
}
?>
</ul>
Instead all the list items are outputting as parents like this...
<ul>
<li>Media(1)</li>
<li>Movies(1)</li>
<li>TV Shows(1)</li>
<li>Uncategorised(1)</li>
</ul>
...but they should be like this...
<ul>
<li>Media(1)
<ul>
<li>Movies(1)</li>
<li>TV Shows(1)</li>
</ul>
</li>
<li>Uncategorised(1)</li>
</ul>
As you can see 'hierarchical' is set to 1, but it doesn't work as expected.
PS: I can't use the standard wp_list_categories method (http://codex.wordpress.org/Template_Tags/wp_list_categories) because I will need to be able to customise the markup in the list.
Any suggestions will be helpful.
You can use following code:
<ul>
<?php
$args = array(
'show_option_all' => '',
'container' => false,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'use_desc_for_title' => 0,
'child_of' => 0,
'hierarchical' => 1,
'number' => null,
'echo' => 1,
'depth' => -1,
'taxonomy' => 'category'
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
if($category->parent==0)
echo '<li>' . $category->name . '(' . $category->count . ')</li>';
else
echo '<ul>' . $category->name . '(' . $category->count . ')</li></ul>';
}
?>
</ul>
UPDATE
$args = array(
'hide_empty' => 0,
'echo' => 1,
'taxonomy' => 'category',
'hierarchical' =>1,
'show_count' => 1,
);
function add_class_wp_list_categories($wp_list_categories) {
$pattern = '/<li class="/is';
$replacement = '<li class="first ';
return preg_replace($pattern, $replacement, $wp_list_categories);
}
add_filter('wp_list_categories','add_class_wp_list_categories');
echo wp_list_categories( $args );
Use 'orderby' => 'parent'
$args = array(
'taxonomy' => 'your_tax',
'orderby' => 'parent'
);
$cats = get_categories($args);

Make Target _Blank With This Attacment PHP

this php below will print like this http://example.com/wp-content/uploads/2013/01/imagename.jpg with anchor text 'DOWNLOAD'
<?php
if ( $attachments = get_children( array(
'post_type' => 'attachment',
'post_mime_type'=>'image',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID
)));
foreach ($attachments as $attachment) {
echo wp_get_attachment_link( $attachment->ID, '' , false, true, 'Download');
}
?>
1.when user clicking this link, how to target in _blank or open in new tab.
2.is possible this short code combine with Javascript to make Force download link? look like bellow.
if ( $attachments = get_posts( array(
'post_type' => 'attachment',
'post_mime_type'=>'image',
'numberposts' => -1,
'post_status' => 'any',
'post_parent' => $post->ID,
) ) );
foreach ( $attachments as $attachment ) {
echo '<a href="javascript:void(0);"
onclick="document.execCommand(\'SaveAs\', true, \'' . get_permalink( $attachment->ID ) . '\');">
Download This Wallpaper</a>';
}
This is what I meant below in my response.
array(
'post_type' => 'attachment',
'post_mime_type'=>'image',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID ,
'target' => 'target="_blank"';
)
See if it works this way.
amek teh chages and try this
foreach ( $attachments as $attachment ) {
echo '<a href="javascript:void(0);"
onclick="document.execCommand(\'SaveAs\', true, \'' . get_permalink( $attachment->ID ) . '\');" target="blank">
Download This Wallpaper</a>';
}
hope this will helpt you

Wordpress list pages from parent

I need help listing all pages when on a third level (grandchild) Page.
E.g I have Page 1(grandparent) Page 2(Parent) Page 3(Child)
I need to show all these pages listed the same on all three pages such as:
Page1
Page2
Page3
I have successfully shown the right list of pages on page 1 and 2. (see below)
Can anyone please help
function widget($args, $instance) {
global $post;
extract( $args );
if($post->post_parent == 0) {
$title = ''.$post->post_title.'';
$id_to_query = $post->ID;
}
elseif($post->ancestors) {
$page = get_page($post->ancestors[0]);
$title = ''.$page->post_title.'';
$id_to_query = $post->ancestors[0];
} else {
$page = get_page($post->post_parent);
$title = ''.$page->post_title.'';
$id_to_query = $page->ID;
}
$children = get_pages('post_type='.get_post_type().'&child_of='.$id_to_query);
if(empty($children) || is_page( array(17,125) ) ) return; // excludes contact us etc...
wp_reset_query();
$widget_title = $title;
echo $before_widget;
echo $before_title . $widget_title . $after_title; ?>
<ul>
<?php wp_list_pages('title_li=&post_type='.get_post_type().'&child_of='.$id_to_query); ?>
</ul>
<?php
echo $after_widget;
wp_reset_postdata();
Try getting the top ancestor id with $topid = get_top_ancestor($postid); then using the arguement 'child_of' => $topid and be mindful of your depth
Full example:
<?php
$postid = get_the_ID();
$topid = get_top_ancestor($postid);
$args = array(
'depth' => 2,
'show_date' => '',
'date_format' => get_option('date_format'),
'child_of' => $topid,
'exclude' => '',
'include' => '',
'echo' => 1,
'authors' => '',
'sort_order' => 'ASC',
'link_before' => '',
'link_after' => '',
'walker' => '',
'post_type' => 'page',
'post_status' => 'publish'
); ?>
<ul><?php wp_list_pages( $args ); ?></ul>
Something for you to work on [Untested]:
function widget($args, $instance) {
global $post;
$postid = get_the_ID();
$topid = get_top_ancestor($postid);
$args = array(
'depth' => 2,
'show_date' => '',
'date_format' => get_option('date_format'),
'child_of' => $topid,
'exclude' => '',
'include' => '',
'echo' => 1,
'authors' => '',
'sort_order' => 'ASC',
'link_before' => '',
'link_after' => '',
'walker' => '',
'post_type' => 'page',
'post_status' => 'publish'
);
// if(empty($children) || is_page( array(17,125) ) ) return; // excludes contact us etc...
// wp_reset_query();
$widget_title = $title;
echo $before_widget;
echo $before_title . $widget_title . $after_title; ?>
<ul>
<?php wp_list_pages( $args ); ?>
</ul>
<?php
echo $after_widget;
wp_reset_postdata();
}

Resources