I can't properly load child posts of a post - php

I'm new to PHP and WordPress.
I'm using Types plugin for creating my custom types (more info at here).
Here is my code:
<?php
$args = array('post_type' => 'project-list');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) :
?>
<span><?php echo $post -> post_title; ?></span>
<ul class="project-list">
<?php
$argsChild = array('orderby' => 'post-order', 'order' => 'ASC');
$mypostsChild = types_child_posts('project', $argsChild);
foreach ( $mypostsChild as $postChild ) :
?>
<li>
<p class="project-cost"><?php echo $postChild -> post_title; ?></p>
<span class="project-name"><?php echo $postChild -> fields['project-name']; ?></span>
<hr />
<p class="project-type"><?php echo $postChild -> fields['project-type']; ?></p>
<hr />
<span class="project-location"><?php echo $postChild -> fields['project-location']; ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
What I expect:
I need to load the list of projects by their type. For example I need the final output to look like this.
Project Group Name-1
proj-1
proj-2
proj-3
Project Group Name-2
proj-1
proj-2
proj-3
I've two problems here:
1: I don't know where to put the parent post "ID", and this code is now rendering me all the projects from all the project lists. I need to load projects separately for every project list (like above).
Q: How can I do this?
2: I can't sort the posts by their post-order (which is a custom field type). Thus, I noticed that the following part of the code is not working proper.
$argsChild = array('orderby' => 'post-order', 'order' => 'ASC');
Q: How can I sort the child posts based on a custom field type, which is "post-order" in my case?
Thanks guys!

Posts haven't any parent or child. Posts are created under categories. You should create some categories and subcategory under category. i.e.
CATEGORY :Project Group Name-1
- SUBCATEGORY: pro 1
- SUBCATEGORY: pro 2
CAT:Project Group Name-2
- SUBCATEGORY: pro 1
- SUBCATEGORY: pro 2
Moreover, you have created custom post type. There are many ways to retrieve the terms of custom post types depending your spec. However, try the following code to make some sense:
<?php
//list terms in a given taxonomy
$taxonomy = 'project-list';
$tax_terms = get_terms($taxonomy);
?>
<ul>
<?php
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
}
?>
</ul>
Learn more about custom post type , terms and taxonomy. And read this link, you will get more codes what is related to your need. these will help you.
FYI: page has parent or child page

Related

php generate links to all wordpress categories

Im having a little problem. I want to display all categories connected to a wordpress post.
I found several codes and plugins, but all of them have problems. Some dont display correct anchor text, others dont link to correct url.
So i took the working parts from different places and put them together to the code below, and its working.
The problem is that it does not generate links to ALL connected categories, it only show link to 1 single category (first one that was connected to the post)
what is wrong with the code ? Why will it not show links to all connected categories. I have been working on it for 2 days now, and i give up.
<?php
$the_cat = get_the_category();
$category_name = $the_cat[0]->cat_name;
$category_link = get_category_link( $the_cat[0]->cat_ID );
?>
<?php
global $post;
$category = reset(get_the_category($post->ID));
$category_id = $category->cat_ID;
?>
<a class="button" href="<?php echo get_category_link( $category_id ); ?>"title=”<?php echo $category_name ?>” ><?php echo $category_name ?></a>
Please try this code
<?php
$category_args_query = array(
'orderby' => 'name',
'parent' => 0,
'hierarchical' => 1,
'number'=> 10
);
$categories = get_categories( $category_args_query );?>
<ul>
<?php
foreach ( $categories as $category )
{ ?>
<li><?php echo $category->name; ?> </li>
<?php
}
?>
</ul>
If you want to show the category that is related with the posts on post details page and archive page add this code in your child theme functions.php and call the function where you want to display it.
function postsCategories()
{
$categories_list = get_the_category_list( esc_html__( ', ', 'your-text-domain' ) );
$allowed_tags_before_after=array('span' => array('class'=>array()),'i'=>array('class'=>array()),'a'=>array('class'=>array(),'href'=>array(),'rel'=>array()));
if ( $categories_list )
{
printf(wp_kses(sprintf(__('<span class="blogmeta cat-links"> <i class="fa fa-folder-open"></i> %1$s </span>','your-text-domain'), $categories_list ),$allowed_tags_before_after));
}
}
Call in the template : <?php printf(__('%s','your-text-domain'),postsCategories()); ?>
Actually just <?php the_category(', ') ?> should be enough (within the WP while loop). The , determines what should go in-between, if there is more than one category.

WP Custom Tabs System- Multiple Loops for Multiple Custom Post Types

I'm building a Wordpress site right now and I'm trying to create a tab structure on a single page where I can create a sort of sub-menu and display 4 different custom-post-type loops, depending on what menu item is selected. I've been able to get the nav-items block consistent with a foreach loop, and the content blocks are switching correctly, but right now, each content div is displaying all the posts of every custom-post-type that I have.
I've experimented with switch statements and if statements as well as the is_singular conditional tag, but nothing is working quite right thus far
I've put comments in the code to delineate for myself what each section is doing, thought they might help you get into my brain space.
<ul class="tab" role="tablist">
<?php
//All public posts that are not built into Wordpress
$argsnav = array(
'public' => true,
'_builtin' => false,
);
$output = 'objects'; // names or objects, note names is the default
$operator = 'and'; // 'and' or 'or'
//Get the Post Types of each post as an object
$post_types = get_post_types( $argsnav, $output, $operator );
//Remove Product from Array
unset( $post_types ['product'] );
//Iterate through each post type
foreach ($post_types as $post_type) {
//And print out a list item with the corresponding ID and text
echo '<li>
<a href="#' . $post_type->name . '" role="tab" data-toggle="tab">
'. $post_type->label .'
</a>
</li>';
}
echo '</ul>';
?>
//New section for post content/ This is the buggy portion
<div class="tab-content">
<?php
//Iterate through each post type
foreach($post_types as &$post_type) { ?>
<!--Create div with corresponding id-->
<div class="tab-pane" id="<?php echo "$post_type->name" ?>">
<!--Create new query for all posts of each type-->
<?php $loop = new WP_Query(
array(
'post_type' => array('standup', 'novels', 'short_stories', 'music'),
));
//Loop through and display each post's title
if ( $loop->have_posts()) :
while ( $loop->have_posts() ) :
$loop->the_post(); ?>
<h2><?php the_title(); ?></h2>
<!--Stop the loop-->
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php } wp_reset_postdata(); ?>
</div>
I think I know what needs to be done, but HOW is where I'm stuck. Any help is very much appreciated!
As I feel, you do not need to set all post_type in wp_query, please try this
<!--Create new query for all posts of each type-->
<?php $loop = new WP_Query(
array(
'post_type' => $post_type->name,
));
//Loop through and display each post's title

Showing Related Posts Manually (Wordpress)

We have a custom post type in my wordpress website under which we publish our reports. Here's a sample report page -
goo.gl/hqVFJ2
See the report name. The report name has a specific format which is "Region" Report Topic "Market"
For each topic we have 5 regional variants, which are as follow:
Global
North America
Europe
Asia Paciic
LAMEA
Our requirement is, when someone opens any one of the variant the other 4 should appear under the similar reports section. For that matter I used Contextual Related Posts plugin which is not able to meet the requirement 100% and in fact to the contrary making the site heavy. I was trying to figure out if there can be a way to achieve this with a custom code.
I found a code snippet but that shows similar posts as per the tags.
Here's the code of the report page:
<h1 itemprop="headline" class="entry-title"><?php the_title(); ?> </h1>
<?php if (get_post_meta($post->ID, 'cc_price', true) !== '') { ?><span
class="price_meta">
<span class="price_left"></span><span class="price_center"><?php
if (cc_get_option('cc_currency') != '') {
echo cc_get_option('cc_currency');
} else {
echo get_option('currency_symbol');
}
echo get_post_meta($post->ID, 'cc_price', true);
?></span><span class="price_right"></span></span> <?php } ?>
<ul class="post_meta">
<li class="estimate"><?php echo get_post_meta($post->ID,'publish_date_new',true); ?><time class="dt-published" datetime="<?php echo get_post_meta($post->ID,'publish_date_new',true); ?>"></time></li>
<li class="cate"><?php printf(IN . ' ' . '%s', $taxonomies); ?></li>
<li itemprop="author" class="author p-author h-card">By <?php echo get_post_meta($post->ID,'published_by_new',true); ?>
</li>
What you could do is add a custom field to specify the Unique Id of the report.
Then display all posts with that Unique Id (except the one the user is on)
You can use Advanced Custom Fields to create the custom field.
Then your hook would look something like
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'your_custom_post_type',
'meta_key' => 'report_unique_id',
'meta_value' => the_field('report_unique_id'),
'post__not_in' => get_the_ID(),
));
if($posts)
{
foreach($posts as $post)
{
/* Your related Post */
}
}
?>

Excluding a Category Name (Not Posts) From A WordPress Template

I'm currently working with a template (Point from MyThemeShop) in WordPress that has been functioning fairly well for me. I've already made some customizations but am having one issue.
The theme pulls the most recent posts and displays them in a "Featured Posts" area at the top of the page. I wanted more control over the posts displayed here so I set up a category (Homepage Featured Posts) to better define which posts should show up there. Works well, however, under this area where the recent post feed is . . . the thumbnail previews pull and display the category name in alphabetical order. So, if I have a featured post categorized under "Review" also, the preview thumbnail currently says "Homepage Featured Post" instead of "Review". I would like to exclude "Homepage Featured Post" from showing up in these thumbnails (and one other category I've also defined, "Trending Articles").
In the Index Template the code that seems to pull the category names is this:
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="nofollow" id="featured-thumbnail">
<?php echo '<div class="featured-thumbnail">'; the_post_thumbnail('featured',array('title' => '')); echo '</div>'; ?>
<div class="featured-cat"><?php $category = get_the_category(); echo $category[0]->cat_name;?></div>
<?php if (function_exists('wp_review_show_total')) wp_review_show_total(true, 'latestPost-review-wrapper'); ?>
</a>
I've tried an exclusion method that sort of worked . . . but ended up not pulling ANY categories instead, using this method on line 4:
<div class="featured-cat"><?php $category = get_the_category(); echo $category[0]->cat_name !--'Homepage Featured Post';?></div>
Do I need to use specific category ID's? I am unsure how to accomplish this. Any help is much appreciated.
<?php
foreach((get_the_category()) as $cat) {
if ( $cat->cat_name!=='Homepage Featured Post' || $cat->cat_name!=='Trending Articles' ) {
echo $cat->cat_name . ' ';
}
}
?>
Can you use above code.

Wordpress Menu for current Custom Post Type

I am looking to automatically create a Wordpress Menu for the 'current' Custom Post Type. I found a useful snippet that outputs the current custom post type here:-
$post_type = get_post_type( $post->ID );
echo $post_type;
But am struggling to translate this (or alternate method) into a dynamically created menu for the current custom post type - list all posts in the custom post type. I can't do this on an individual custom post type basis as I'm using a master template to display a series of custom post types.
Thanks
Glennyboy
Here's a super simple way to do it. just create a loop that lists all the page titles wrapped in a link.
<?php
$obj = get_post_type_object(get_post_type($post->ID));
echo '<h2>' . $obj->labels->name . '</h2>';
?>
<ul>
<?php
$query = new WP_Query(array('post_type' => get_post_type($post->ID), 'posts_per_page' => -1, 'order' => 'DESC', 'orderby' => 'date',));
while ( $query->have_posts() ) : $query->the_post();
?>
<li><?php the_title(); ?></li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
This will loop through all the current post's post type and create an unordered list with all the posts titles, wrapped in links to the posts. You could obviously change the HTML to suit your needs.

Categories