Wordpress - Show custom fields of children on parent page - wordpress

I'm using the Advanced custom field plugin and having trouble showing custom fields of children in a loop. I tried this:
<?php <br /> $pages = get_pages(array('child_of' => $post->ID));
foreach($pages as $page)
{
setup_postdata($page);
$fields = get_fields(); print_r($fields);
}
wp_reset_query();
?>
When I put print_r($page) in it says 1111 so that bit is working (as there's 4 children items).
That code just prints the custom field names of the parent rather than the children, how can I show the children's custom fields?

It should've been post instead of page, doh!
http://support.plugins.elliotcondon.com/discussion/43/show-page-children-or-subpages-with-custom-fields

Have you tried this?
get_pages(array('child_of' => $post->ID, 'parent' => $post->ID, 'hierarchical' => 0))

Related

wp_list_categories() with count only for bottom level taxonomies

wp_list_categories() does such a nice job of listing my custom taxonomy based on parent, and even adding the count of posts inside that taxonomy as I want.
However, I need to get rid of the permalink and the count on taxonomies that are parent taxonomies, while keeping the parent category visible only by name.
So here's what I have:
ULTIMATE PARENT TAXONOMY LINK (2)
IRRELEVANT PARENT LINK (3)
ACTUALLY RELEVANT CATEGORY LINK (5)
ACTUALLY RELEVANT CATEGORY LINK (1)
ACTUALLY RELEVANT CATEGORY LINK (2)
IRRELEVANT PARENT LINK (3)
ACTUALLY RELEVANT CATEGORY LINK (7)
ACTUALLY RELEVANT CATEGORY LINK (1)
ACTUALLY RELEVANT CATEGORY LINK (2)
What I need:
ULTIMATE PARENT TAXONOMY
IRRELEVANT PARENT
ACTUALLY RELEVANT CATEGORY LINK (5)
ACTUALLY RELEVANT CATEGORY LINK (1)
ACTUALLY RELEVANT CATEGORY LINK (2)
IRRELEVANT PARENT
ACTUALLY RELEVANT CATEGORY LINK (7)
ACTUALLY RELEVANT CATEGORY LINK (1)
ACTUALLY RELEVANT CATEGORY LINK (2)
I could just call several wp_list_categories() for each "irrelevant parent", but I'm sure that will cause massive problems later on when the client wants to expland the categories.
Here's how I'm doing it right now, just a basic function usage:
<?php wp_list_categories(
array(
'taxonomy' => 'trukkikategoriat', // my custom taxonomy
'orderby' => 'name'
'show_count' => 1,
'child_of' => 54,
'title_li' => '',
)
); ?>
What kind of modifications would achieving my needs require?
Not to stroke my own chain or anything, but turns out answers were plentiful. My search words were simply terrible.
When I ran into the fact that you can query terms with WP_term_query it all became so simple. Full resolved answer for those who might Google the same thing with "bad" search words:
<?php
$cat_mastername = 'trukkikategoriat'; // The category or custom taxonomy you wish to query.
$args = array(
'taxonomy' => $cat_mastername,
'orderby' => 'name',
'order' => 'ASC',
'hierarchical' => true,
'hide_empty' => true,
'child_of' => '54', // If you wish to specify a further taxonomy or taxonomies, you can do that here. Remove this if you want to query the whole taxonomy.
);
$the_query = new WP_Term_Query($args);
$categories = $the_query->get_terms(); // single out the categories of your query result.
if ($categories){
foreach($categories as $category){ // loop everything and poke around.
$ancestors = get_ancestors( $category->term_id, $cat_mastername );
$category->ancestors = $ancestors;
$category->depth = count( $ancestors );
$cat_children = get_term_children($category->term_id, $cat_mastername ); // This is the real gold. Get_term_children finds everything we need.
if($category->depth === 1 && !empty($cat_children)) //We find the taxonomies that match our depth requirement: not the highest, but the second highest. Change the number 1 to different if you want deeper or shallower structure :?>
<a class="trucklist-link main-level" href="#toggle-sub-menu">
<?php
echo $category->name; // This code simply builds a blank link for the parent that is irrelevant beyond it's name.
?>
</a>
<div class="trucklist-sub-level">
<?php foreach($cat_children as $cat_child){ // Now that we found a taxonomy that matches our depth requirement, let's loop that taxonomys's children!
$this_cat = get_term($cat_child); ?>
<a class="trucklist-link sub-level" href="<?php echo get_term_link($cat_child);?>">
<?php
echo $this_cat->name;
echo ' ('.$this_cat->count.')';
?>
</a>
<?php } ?>
</div>
<?php endif;
}
}
?>
I hope this will help someone later!

Wordpress Display Child page data: Title, featured image and custom field

I'm displaying the child pages of the current page. With the featured image, title pulling through. I also want to pull through a custom field I've added. I'm outputting this at html so it looks like each child page has its own box, with the featured image filling the box, the title on top of the image and the the custom field value sitting under the title.
But I can't get the custom field to display properly, I just get the value 1, not sure if I've converted the array properly?
Could you help me out to output the custom field 'PageTag'
https://i.stack.imgur.com/vzCBU.png
https://i.stack.imgur.com/BwfuV.png
Wordpress template code
<div class="childPages">
<?php
$subs = new WP_Query(
array(
'post_parent' => $post->ID,
'post_type' => 'page',
'meta_key' => '_thumbnail_id'
)
);
if( $subs->have_posts() ) :
while( $subs->have_posts() ) :
$subs->the_post();
$pageTag = get_post_meta($post->ID, '_PageTag' , true);
echo '<div class="childPageBlock"> '.get_the_post_thumbnail().'<div class="childPageBlockText"><h3>'.get_the_title().'</h3><span>'.print_r( $pageTag ).'</span></div></div>';
endwhile;
endif;
wp_reset_postdata(); ?>
</div>
Removing the print_r() and leaving its as
'.$pageTag.'
Worked!

sectioning content within a wordpress page

Building a wordpress site that needs to organize content by stream - e.g Mechanical, Electrical etc. Also, each page needs sections like News, Articles events etc. If you pick one page (say Mechanical) it has to have the following sections
News
Articles (category:articles)
Events (category:events)
The other streams will have the same sections as well
Is there a plugin for achieving or would I be better off building a template page for each vertical and writing php code? Shown code for a page with a single section.
<?php
$args = array(
'posts_per_page' => 1,
'category_name' => 'news',
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args );
$the_query = new WP_Query($args);
//EXAMPLE NEWS SECTION
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_content();
echo $content;
}
} else {
// no posts found
}
?>
In my opinion you could just write a plugin which does that filtering.
Said plugin would have some kind on shortcode that would take a parameter (category for instance) and would return or echo all the posts associated with that category.
Shortcode registration :
add_action('init','register_shortcode');
function register_shortcode(){
add_shortcode('shortcode_name', 'shortcode_function');
}
Shortcode function:
function shortcode_function($attr){
$attr = shortcode_atts(array('category' => 'default'),$attr); //Setting defaults parameters
$args = array(
'category_name' => $attr['category'],
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//echo other html related stuff
the_title();
the_content();
}
}
}
Usage
[shortcode_name category='news']
[shortcode_name] //category defaults at 'default'
In your case a page could be something like that
<div>
News : <br/>
[shortcode_name category='news']
</div>
<div>
Articles : <br/>
[shortcode_name category='articles']
</div>
<div>
Events : <br/>
[shortcode_name category='events']
</div>
I don't think there is one right way to do this... it depends on the scenario... How many posts, how often do sections or "main streams" get added / change, do you need other stuff on the pages etc.
One pretty simple and easily maintained solution would be to:
1. Add mechanical, electrical, etc. as categories alongside news, articles and events
2. Modify (or add) category.php to repeat the loop three times, and only display the posts that belong to each section (other category):
//Loop your sections
foreach(array('news', 'articles', 'events') as $category) {
echo '<h2>' . $category . '</h2>';
//Loop posts
while ( have_posts() ) {
the_post();
//Check if post is in section (by slug!)
if ( in_category($category) ) {
the_title();
the_content();
}
}
//Start the loop over
rewind_posts();
}
Now just assign each post to one (or more) "parents", fx mechanical, and also to one (and only one) of news, articles or events...
If you go to the archive page of mechanical you get three sections
if you go to news you get all news (but also two empty sections, so you should of course check for that).
Note that this could be done using tags or even a custom taxonomy if you wanted, you'd just need to edit a different theme file - see the template hierarchy.
Beware though that this will not work very nicely with pagination, so if that is a concern you will need to deal with that.

WordPress trigger standard post list rendering

Is there a way to programmatically trigger the standard displaying post list from a custom WP_Query, just like a "category" menu item does?
To clarify:
i'm not looking for plugins like List category posts that do the work but with a custom built-in template..
I need the wp's(theme's) standard post list rendering loop to be triggered !
Thanks!
Put this code where ever you need to render list of post from category "example_category_slug", (or any other taxonomy).
$wpq = array ('taxonomy'=>'category','term'=>'example_category_slug');
$myquery = new WP_Query ($wpq);
$article_count = $myquery->post_count;
echo "<ul>";
if ($article_count){
while ($myquery->have_posts()) : $myquery->the_post();
echo "<li>".$post->post_title."</li>";
endwhile;
}
echo "</ul>";
or use simpler query, like $myquery = new WP_Query ('cat= 11, 9'); for cat ids, or any other wp_query
But while choosing where to put it, consider Template Hierarchy as a set of rules which template file is right to use.
If you need a list of categoires insted post use wp_list_categories
<?php
$args = array(
'show_option_all' => '',
'orderby' => 'name',
'order' => 'ASC',
'style' => 'list'
);
wp_list_categories($args);
?>
ful ref. in wp codex
Here is a way to do it..
inside a shortcode's function, or in a function that act ajax style, put a code like that:
global $wp_query;
$cat = $_GET['categoria'];
$wp_query->init();
$wp_query->query(array(
'posts_per_page' => 4,
'category_name' => $cat
));
get_template_part( 'blog', 'columns' );
wp_reset_query();
die();
the get_template_part arguments are theme dependent and can be desumed from php template names from theme's root directory.
in this case wp will call [theme's_dir]/blog-columns.php..
i reccomend the lecture #Michal reccomended me

Wordpress - Custom fields & Class Identifiers

In Wordpress - how do you assign a class to custom fields? I am attempting to target multiple custom fields with custom CSS, but seem unable to give them separate classes.
This is probably not the BEST solution, but is worth a shot, I'd do a loop per field so your get_posts would look like this:
$args = array(
'meta_key' => 'custom_attribute',
'meta_value' => 'value1',
);
$value1 = get_posts($args);
$args = array(
'meta_key' => 'custom_attribute',
'meta_value' => 'value2',
);
$value2 = get_posts($args);
So that will give you 2 arrays, each one pulling different posts, from that you can do a foreach going through each array (value1 and value2) and adding classes as needed.
Thanks for your help.
I've since discovered a great bit of syntax that accomplishes the job.
<?php echo '<li class="address">' . get_post_meta($post->ID, 'Address', true) . '</li>'; ?>
This pulls in one single custom field at a time. In the example above, my custom field title was 'Address'. The will wrap the content from that field - thus accomplishing the task of wrapping each custom field in it's own class.

Resources