Trying to get property of non object - php

I am trying to code a function for Wordpress Post navigation but i am getting
Trying to get property of non object
error in lines with $prevpost->ID and $nextpost->ID, the code is this:
if ( ! function_exists( 'mm_post_nav' ) ) :
/**
* Displays navigation to next/previous post when applicable.
*
* #since 1.0
*
* #return void
*/
function mm_post_nav() {
global $post;
// Don't print empty markup if there's nowhere to navigate.
$previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
$next = get_adjacent_post( false, '', false );
$prevpost = get_previous_post(true);
$prevThumbnail = get_the_post_thumbnail($prevpost->ID, array(44,44) );
$nextpost = get_next_post(true);
$nextThumbnail = get_the_post_thumbnail($nextpost->ID, array(44,44) );
if ( ! $next && ! $previous )
return;
?>
<nav class="mm-post-nav" role="navigation">
<h1 class="screen-reader-text"><?php _e( 'Post navigation', 'mm-cube' ); ?></h1>
<div class="mm-single-nav">
<div class="mm-prev-link">
<?php previous_post_link( '%link', _x( '<i class="icon-chevron-left"></i><span class="mm-prev-post-title"> %title</span>', 'Previous post link', 'mm-cube' ) ); ?>
</div>
<div class="mm-next-link">
<?php next_post_link( '%link', _x( '<span class="mm-next-post-title">%title</span><i class="icon-chevron-right"></i>' , 'Next post link', 'mm-cube' ) ); ?>
</div>
</div><!-- .nav-links -->
<div class="clear"></div>
</nav><!-- .navigation -->
<?php
}
endif;
Making a var_dump($prevpost) outputs this:
string(0) ""

You may try something like following, because $prevpost and $nextpost could return empty/null if there is no matching posts are available.
$prevpost = get_previous_post(true);
if (!empty( $prevpost )) {
$prevThumbnail = get_the_post_thumbnail($prevpost->ID, array(44,44) );
}
$nextpost = get_next_post(true);
if (!empty( $nextpost )) {
$nextThumbnail = get_the_post_thumbnail($nextpost->ID, array(44,44) );
}
Return Values (get_previous_post and get_next_post)
Post object if successful.
Null if global $post is not set.
Empty string if no corresponding post exists.
Read more on Codex about get_previous_post and get_next_post to know how they work and what they return on any condition.
Update: Check the function's arguments, you have used (in both previous and next):
previous_post_link( $format, $link, $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' );
You should use:
previous_post_link( '%link', _x( '<i class="icon-chevron-left"></i><span class="mm-prev-post-title">%title</span>', FALSE, 'mm-cube' ) );
Use same order for other function. Check more on Codex about previous_post_link and next_post_link

Related

"Exclude current post" Widget wordpress (PHP help)

Hi :) I am working on my site with the "flexible post widget" to show the related contents or the content that I am interested in showing on the sidebar.
The widget works very well and I'm interested in continuing to use it, especially for the design and adaptability options with my site.
The problem? It does not include a function for "exclude the current post".
I was reviewing the code (I add it to this thread) and I think I could add some lines of code for this function (exclude the post I'm reading).
Unfortunately I'm new to programming and I'm lost with what and where to add it.
Could you help me?
<?php
/**
* Flexible Posts Widget: Default widget template
*
* #since 3.4.0
*
* This template was added to overcome some often-requested changes
* to the old default template (widget.php).
*/
// Block direct requests
if ( !defined('ABSPATH') )
die('-1');
echo $before_widget;
if ( ! empty( $title ) )
echo $before_title . $title . $after_title;
if ( $flexible_posts->have_posts() ):
function be_exclude_current_post( $args ) {
if( is_singular() && !isset( $args['post__in'] ) )
$args['post__not_in'] = array( get_the_ID() );
return $args;
}
add_filter( 'widget_posts_args', 'be_exclude_current_post' );
?>
<ul class="dpe-flexible-posts">
<?php while ( $flexible_posts->have_posts() ) : $flexible_posts->the_post(); global $post; ?>
<li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<a href="<?php echo the_permalink(); ?>">
<?php
if ( $thumbnail == true ) {
// If the post has a feature image, show it
if ( has_post_thumbnail() ) {
the_post_thumbnail( $thumbsize );
// Else if the post has a mime type that starts with "image/" then show the image directly.
} elseif ( 'image/' == substr( $post->post_mime_type, 0, 6 ) ) {
echo wp_get_attachment_image( $post->ID, $thumbsize );
}
}
?>
<div class="title"><?php the_title(); ?></div>
</a>
</li>
<hr>
<?php endwhile; ?>
</ul><!-- .dpe-flexible-posts -->
<?php
endif; // End have_posts()
echo $after_widget;
Add this to your theme's 'functions.php' and remove it from the widget template.
function be_exclude_current_post( $args ) {
if( is_singular() && !isset( $args['post__in'] ) )
$args['post__not_in'] = array( get_the_ID() );
return $args;
}
add_filter( 'dpe_fpw_args', 'be_exclude_current_post' );

Error only with php 7.1 how to solve the "Too few arguments to function get_query_var()" [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Please just le me say some things important
1) I'm a completely newbie to php coding, basically i don't know how to code, I'm able just to "understand" something looking at the code but in the most times I don't know how to solve an issue, I just try to change something in the code and try an try again.
2) I see there are other question-topic like this but no one of them seems to solve my issue because no one of them explains WHAT to change in the code.
I have a wordpress site and since my hosting provider updated to php 7.1 I receive this error during a checkout process
Fatal error: Uncaught ArgumentCountError: Too few arguments to function get_query_var(), 0 passed in /homepages/27/d717103608/htdocs/clickandbuilds/IlCrudoeIlCotto/wp-content/plugins/woocommerce_events/classes/woohelper.php on line 688 and at least 1 expected in /homepages/27/d717103608/htdocs/clickandbuilds/IlCrudoeIlCotto/wp-includes/query.php:26 Stack trace: #0 /homepages/27/d717103608/htdocs/clickandbuilds/IlCrudoeIlCotto/wp-content/plugins/woocommerce_events/classes/woohelper.php(688): get_query_var() #1 /homepages/27/d717103608/htdocs/clickandbuilds/IlCrudoeIlCotto/wp-includes/class-wp-hook.php(288): Woo_Helper->display_thank_you_text('Grazie. Il tuo ...') #2 /homepages/27/d717103608/htdocs/clickandbuilds/IlCrudoeIlCotto/wp-includes/plugin.php(203): WP_Hook->apply_filters('Grazie. Il tuo ...', Array) #3 /homepages/27/d717103608/htdocs/clickandbuilds/IlCrudoeIlCotto/wp-content/plugins/woocommerce/templates/checkout/thankyou.php(41): apply_filters('woocommerce_tha...', 'Grazie. Il tuo ...', Object(WC_Order)) #4 /homep in /homepages/27/d717103608/htdocs/clickandbuilds/IlCrudoeIlCotto/wp-includes/query.php on line 26
I know this is a well-know issue because in php 7.1 what was a "warning" has been promoted to be fatal error
see this:
https://www.bestwebframeworks.com/tutorials/php/153/solve-php-uncaught-argumentcounterror-too-few-arguments-to-function/
and this
http://php.net/manual/en/class.argumentcounterror.php
The problem is.. I completely don't know what to change in my php files to have this working again, and I dont know where to check for some error.
I understood that this problem belongs to a function that expect like 1 argument and receives nothing, but I dont know how to chek... This place is my last resource.
Then I put here my php files involved in the error, hope that someone will be so kind to help me. Thanks
At these URLs you can find the whole php files
woohelper.php
https://pastebin.com/EAwjg9Wm
class-wp-hook.php
https://pastebin.com/9K9nrk20
query.php
https://pastebin.com/Qy6aGmcX
plugin.php
https://pastebin.com/HutKXEuy
thankyou.php
https://pastebin.com/yDw8mCi8
From here I will just copy the parts that are listed in the error received.
Please also note.. if I revert my server php version prior to 7.1 (e.g. 7.0 or 6.9) everything works fine.
woohelper.php (error in line 688)
public function display_thank_you_text($thankYouText) {
global $woocommerce;
global $post;
$paged = get_query_var(); /*THIS IS LINE 688*/
$actualLink = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$segments = array_reverse(explode('/', $actualLink));
$orderID = $segments[1];
$order = new WC_Order($orderID);
$items = $order->get_items();
$products = array();
foreach($items as $item) {
$products[$item['product_id']] = $item['product_id'];
}
foreach($products as $key => $productID) {
$WooCommerceEventsThankYouText = get_post_meta($productID, 'WooCommerceEventsThankYouText', true);
if(!empty($WooCommerceEventsThankYouText)) {
echo $WooCommerceEventsThankYouText."<br/><br/>";
}
}
return $thankYouText;
}
query.php (error in line 26)
function get_query_var( $var, $default = '' ) { /* THIS IS LINE 26 */
global $wp_query;
return $wp_query->get( $var, $default );
}
wp-class-hook.php (error in line 288)
/**
* Calls the callback functions added to a filter hook.
*
* #since 4.7.0
*
* #param mixed $value The value to filter.
* #param array $args Arguments to pass to callbacks.
* #return mixed The filtered value after all hooked functions are applied to it.
*/
public function apply_filters( $value, $args ) {
if ( ! $this->callbacks ) {
return $value;
}
$nesting_level = $this->nesting_level++;
$this->iterations[ $nesting_level ] = array_keys( $this->callbacks );
$num_args = count( $args );
do {
$this->current_priority[ $nesting_level ] = $priority = current( $this->iterations[ $nesting_level ] );
foreach ( $this->callbacks[ $priority ] as $the_ ) {
if( ! $this->doing_action ) {
$args[ 0 ] = $value;
}
// Avoid the array_slice if possible.
if ( $the_['accepted_args'] == 0 ) {
$value = call_user_func_array( $the_['function'], array() );
} elseif ( $the_['accepted_args'] >= $num_args ) {
$value = call_user_func_array( $the_['function'], $args );
} else {
/** THIS IS LINE 288 */ $value = call_user_func_array( $the_['function'], array_slice( $args, 0, (int)$the_['accepted_args'] ) );
}
}
} while ( false !== next( $this->iterations[ $nesting_level ] ) );
unset( $this->iterations[ $nesting_level ] );
unset( $this->current_priority[ $nesting_level ] );
$this->nesting_level--;
return $value;
}
plugin.php (error in line 203)
function apply_filters( $tag, $value ) {
global $wp_filter, $wp_current_filter;
$args = array();
// Do 'all' actions first.
if ( isset($wp_filter['all']) ) {
$wp_current_filter[] = $tag;
$args = func_get_args();
_wp_call_all_hook($args);
}
if ( !isset($wp_filter[$tag]) ) {
if ( isset($wp_filter['all']) )
array_pop($wp_current_filter);
return $value;
}
if ( !isset($wp_filter['all']) )
$wp_current_filter[] = $tag;
if ( empty($args) )
$args = func_get_args();
// don't pass the tag name to WP_Hook
array_shift( $args );
/* THIS IS LINE 203 */ $filtered = $wp_filter[ $tag ]->apply_filters( $value, $args );
array_pop( $wp_current_filter );
return $filtered;
}
thankyou.php
<div class="woocommerce-order">
<?php if ( $order ) : ?>
<?php if ( $order->has_status( 'failed' ) ) : ?>
<p class="woocommerce-notice woocommerce-notice--error woocommerce-thankyou-order-failed"><?php _e( 'Unfortunately your order cannot be processed as the originating bank/merchant has declined your transaction. Please attempt your purchase again.', 'woocommerce' ); ?></p>
<p class="woocommerce-notice woocommerce-notice--error woocommerce-thankyou-order-failed-actions">
<?php _e( 'Pay', 'woocommerce' ) ?>
<?php if ( is_user_logged_in() ) : ?>
<?php _e( 'My account', 'woocommerce' ); ?>
<?php endif; ?>
</p>
<?php else : ?>
!-- this is line 41 -! <p class="woocommerce-notice woocommerce-notice--success woocommerce-thankyou-order-received"><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( 'Thank you. Your order has been received.', 'woocommerce' ), $order ); ?></p>
<ul class="woocommerce-order-overview woocommerce-thankyou-order-details order_details">
<li class="woocommerce-order-overview__order order">
<?php _e( 'Order number:', 'woocommerce' ); ?>
<strong><?php echo $order->get_order_number(); ?></strong>
</li>
<li class="woocommerce-order-overview__date date">
<?php _e( 'Date:', 'woocommerce' ); ?>
<strong><?php echo wc_format_datetime( $order->get_date_created() ); ?></strong>
</li>
<?php if ( is_user_logged_in() && $order->get_user_id() === get_current_user_id() && $order->get_billing_email() ) : ?>
<li class="woocommerce-order-overview__email email">
<?php _e( 'Email:', 'woocommerce' ); ?>
<strong><?php echo $order->get_billing_email(); ?></strong>
</li>
<?php endif; ?>
<li class="woocommerce-order-overview__total total">
<?php _e( 'Total:', 'woocommerce' ); ?>
<strong><?php echo $order->get_formatted_order_total(); ?></strong>
</li>
<?php if ( $order->get_payment_method_title() ) : ?>
<li class="woocommerce-order-overview__payment-method method">
<?php _e( 'Payment method:', 'woocommerce' ); ?>
<strong><?php echo wp_kses_post( $order->get_payment_method_title() ); ?></strong>
</li>
<?php endif; ?>
</ul>
<?php endif; ?>
<?php do_action( 'woocommerce_thankyou_' . $order->get_payment_method(), $order->get_id() ); ?>
<?php do_action( 'woocommerce_thankyou', $order->get_id() ); ?>
<?php else : ?>
<p class="woocommerce-notice woocommerce-notice--success woocommerce-thankyou-order-received"><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( 'Thank you. Your order has been received.', 'woocommerce' ), null ); ?></p>
<?php endif; ?>
</div>
From the documentation:
get_query_var( string $var, mixed $default = '' )
Retrieve variable in the WP_Query class.
Parameters $var (string) (Required) The variable key to
retrieve.
The first parameter in get_query_var() is required. On line 688, you're not passing the first required parameter.
Judging by your code you're looking for the 'paged' qvar, so try this for line 688 in woohelper.php:
$paged = get_query_var( 'paged' );

How to substring element inside html

I need to shorten the title in the previous and next buttons in a WP theme.
Here's the code:
<?php
/* ----------------------------------------------------------------------------------
Display navigation to next/previous pages when applicable.
---------------------------------------------------------------------------------- */
if ( ! function_exists( 'thinkup_input_nav' ) ) :
function thinkup_input_nav( $nav_id ) {
global $wp_query, $post;
// Don't print empty markup on single pages if there's nowhere to navigate.
if ( is_single() ) {
$previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
$next = get_adjacent_post( false, '', false );
if ( ! $next && ! $previous )
return;
}
// Don't print empty markup in archives if there's only one page.
if ( $wp_query->max_num_pages < 2 && ( is_home() || is_archive() || is_search() ) )
return;
?>
<nav role="navigation" id="<?php echo $nav_id; ?>">
<?php if ( is_single() ) : ?>
<?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-icon"><i class="icon-angle-left icon-large"></i></span><span class="meta-nav">%title</span>' ); ?>
<?php next_post_link( '<div class="nav-next">%link</div>', '<span class="meta-nav">%title</span><span class="meta-icon"><i class="icon-angle-right icon-large"></i></span>' ); ?>
<?php elseif ( $wp_query->max_num_pages > 1 && ( is_home() || is_archive() || is_search() ) ) : // navigation links for home, archive, and search pages ?>
<?php if ( get_next_posts_link() ) : ?>
<div class="nav-previous"><?php next_posts_link( __( 'Older posts', 'lan-thinkupthemes') ); ?></div>
<?php endif; ?>
<?php if ( get_previous_posts_link() ) : ?>
<div class="nav-next"><?php previous_posts_link( __( 'Newer posts', 'lan-thinkupthemes') ); ?></div>
<?php endif; ?>
<?php endif; ?>
</nav><!-- #<?php echo $nav_id; ?> -->
<?php
}
endif;
/* ----------------------------------------------------------------------------------
Display navigation to next/previous image when applicable.
---------------------------------------------------------------------------------- */
if ( ! function_exists( 'thinkup_input_imagesnav' ) ) :
function thinkup_input_imagesnav() {
global $wp_query, $post;
?>
<nav role="navigation" id="nav-below">
<div class="nav-previous"><?php previous_image_link( 'false', '<div class="nav-previous"><span class="meta-icon"><i class="icon-angle-left icon-large"></i></span><span class="meta-nav">' . __( 'Previous', 'lan-thinkupthemes') . ' </span></div>' ); ?></div>
<div class="nav-next"><?php next_image_link( 'false', '<span class="meta-nav">' . __( 'Next', 'lan-thinkupthemes') . '</span><span class="meta-icon"><i class="icon-angle-right icon-large"></i></span>' ); ?></div>
</nav><!-- #image-navigation -->
<?php
}
endif;
/* Display comments at bottom of post, page and project pages. */
function thinkup_input_allowcomments() {
if ( comments_open() || '0' != get_comments_number() )
comments_template( '/comments.php', true );
}
/* ----------------------------------------------------------------------------------
Returns true if a blog has more than 1 category.
---------------------------------------------------------------------------------- */
function thinkup_input_categorizedblog() {
if ( false === ( $all_the_cool_cats = get_transient( 'all_the_cool_cats' ) ) ) {
// Create an array of all the categories that are attached to posts
$all_the_cool_cats = get_categories( array(
'hide_empty' => 1,
) );
// Count the number of categories that are attached to the posts
$all_the_cool_cats = count( $all_the_cool_cats );
set_transient( 'all_the_cool_cats', $all_the_cool_cats );
}
if ( '1' != $all_the_cool_cats ) {
return true;
} else {
return false;
}
}
/* Flush out the transients used in thinkup_input_categorizedblog. */
function thinkup_input_transient_flusher() {
delete_transient( 'all_the_cool_cats' );
}
add_action( 'edit_category', 'thinkup_input_transient_flusher' );
add_action( 'save_post', 'thinkup_input_transient_flusher' );
What I tried was to add substring to this line:
<?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-icon"><i class="icon-angle-left icon-large"></i></span><span class="meta-nav">%title</span>' ); ?>
to:
<?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-icon"><i class="icon-angle-left icon-large"></i></span><span class="meta-nav"><?php echo substr(%title,0,10)?></span>' ); ?>
but upon reloading the page it displayed an empty text instead.
What am I doing wrong?
You can override the previous_post_link() function in your functions.php file of your theme as like this :
function previous_post_link($format='« %link', $link='%title', $in_same_cat = false, $excluded_categories = '',$limit = -1) {
if ( is_attachment() )
$post = & get_post($GLOBALS['post']->post_parent);
else
$post = get_previous_post($in_same_cat, $excluded_categories);
if ( !$post )
return;
$title = apply_filters('the_title', $post->post_title, $post);
if ($limit>-1) {$title = substr($title,0,$limit).'…';}
$string = '<a href="'.get_permalink($post->ID).'">';
$link = str_replace('%title', $title, $link);
$link = $pre . $string . $link . '</a>';
$format = str_replace('%link', $link, $format);
echo $format;
And then you can call the function with the additional argument to define your desired trim length:
<li><?php previous_post_link('%link','&laquo %title',FALSE,'',20); ?></li>
Hope it helps you.
Taken from https://wordpress.org/support/topic/using-substr-to-limit-characters-in-post-title

Insert featured image into post content

I am using a wordpress theme for my website and I'm trying to alter the php so that the featured image is displayed within the post content instead of before or after (the top of the post or the bottom)
Here's the original code:
<?php if ( ( has_post_thumbnail( $post_id ) || '' != get_post_meta( $post_id, 'Thumbnail', true ) ) && 'on' == et_get_option( 'origin_thumbnails' ) ) { ?>
<div class="post-thumbnail">
<?php
if ( has_post_thumbnail( $post_id ) ) the_post_thumbnail( 'full' );
else printf( '<img src="%1$s" alt="%2$s" />', esc_attr( get_post_meta( $post_id, 'Thumbnail', true ) ), the_title_attribute( array( 'echo' => 0 ) ) );
?>
</div> <!-- end .post-thumbnail -->
<?php } ?>
<?php the_content(); ?>
Here's how I tweaked it in my child theme:
<?php the_content(); ?>
<?php if ( ( has_post_thumbnail( $post_id ) || '' != get_post_meta( $post_id, 'Thumbnail', true ) ) && 'on' == et_get_option( 'origin_thumbnails' ) ) { ?>
<div class="post-thumbnail">
<?php
if ( has_post_thumbnail( $post_id ) ) the_post_thumbnail( 'full' );
else printf( '<img src="%1$s" alt="%2$s" />', esc_attr( get_post_meta( $post_id, 'Thumbnail', true ) ), the_title_attribute( array( 'echo' => 0 ) ) );
?>
</div>
I'm not very fluent in php and all that accomplished was moving the featured image to the bottom of the post :-/. I posted to my theme's forum and was given the following code which didn't seem to do anything.
add_filter( 'the_content', insert_featured_image, 20 );
function insert_featured_image( $content ) {
$content = preg_replace( "/<\/p>/", "</p>" . get_the_post_thumbnail($post->ID, 'post-single'), $content, 1 );
return $content;
}
Can anyone point me in the right direction?
TIA!
What is the reasoning behind attempting to move the featured image into the content? The Wordpress codex gives the following as an example:
// check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
the_content();
Basically, the_post_thumbnail(); will display the image that was selected as the featured image while the_content(); is simply displaying what was inputted into the wysiwyg for that post or page.
If you're attempting to do this so that it matches a design or layout, you might want to look at applying addition markup and styles to this area. If you require text before and after the featured image you should look into custom fields.

code arrangement inside wordpress loop

This is the actual code for the loop:
<div id="content" class="span9" role="main">
<?php if ( have_posts() ){
// Do we have any posts in the databse that match our query?
?>
<?php
get_template_part( 'inc/post-format/single', get_post_format() );
$enable_rtl = of_get_option('enable_rtl', false);
if(!of_get_option('disable_pagination')){
if($enable_rtl){
$next_post = get_adjacent_post( false, '', true );
$prev_post = get_adjacent_post( false, '', false );
}else{
$next_post = get_adjacent_post( false, '', false );
$prev_post = get_adjacent_post( false, '', true );
}
?>
<?php
}
// show related posts by tag
if(!of_get_option('disable_related_posts')){
get_template_part( 'inc/related-posts' );
}
endwhile; // OK, let's stop the post loop once we've displayed it
// If comments are open or we have at least one comment, load up the default comment template provided by Wordpress
if ( comments_open() )
comments_template( '', true );
}else{ // Well, if there are no posts to display and loop through, let's apologize to the reader (also your 404 error) ?>
<article class="post error">
<h1 class="404"><?php _e('Page not found', 'outbox'); ?></h1>
</article>
<?php } // OK, I think that takes care of both scenarios (having a post or not having a post to show) ?>
</div><!-- #content .site-content -->
This is the snippet which I want to add:
// Parent CPT
if ( 'cpt-parent' == wpse121567_get_cpt_hierarchy() ) {
// Normal loop here
if ( have_posts() ) : while ( have_posts() ) : the_post();
// Normal loop markup
endwhile; endif;
}
// Child CPT
else if ( 'cpt-child' == wpse121567_get_cpt_hierarchy() ) {
// Globalize post object
global $post;
// Output Parent CPT title and content
$parent = get_post( $post->post_parent );
echo '<h1>' . $parent->post_title . '</h1>';
echo '<div>' . apply_filters( 'the_content', $parent->post_content ) . '</div>';
// Fetch parent CPT comments
$parent_cpt_comments = get_comments( array(
'post_id' => $post->post_parent,
'status' => 'approve'
) );
// Loop through parent CPT comments
foreach ( $parent_cpt_comments as $comment ) {
// Output comment list markup here
}
}
// Grandchild CPT
else if ( 'cpt-grandchild' == wpse121567_get_cpt_hierarchy() ) {
// Comment Stats code goes here
}
This is how I added it inside my actual code:
<div id="content" class="span9" role="main">
<?php if ( 'cpt-parent' == wpse121567_get_cpt_hierarchy() ) {
// Normal loop here
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'inc/post-format/single', get_post_format() );
endwhile; endif;
}
// Child CPT
else if ( 'cpt-child' == wpse121567_get_cpt_hierarchy() ) {
// Globalize post object
global $post;
// Output Parent CPT title and content
$parent = get_post( $post->post_parent );
echo '<h1>' . $parent->post_title . '</h1>';
echo '<div>' . apply_filters( 'the_content', $parent->post_content ) . '</div>';
// Fetch parent CPT comments
$parent_cpt_comments = get_comments( array(
'post_id' => $post->post_parent,
'status' => 'approve'
) );
// Loop through parent CPT comments
foreach ( $parent_cpt_comments as $comment ) {
// Output comment list markup here
}
}
// Grandchild CPT
else if ( 'cpt-grandchild' == wpse121567_get_cpt_hierarchy() ) {
// Comment Stats code goes here
}
?>
<?php
$enable_rtl = of_get_option('enable_rtl', false);
if(!of_get_option('disable_pagination')){
if($enable_rtl){
$next_post = get_adjacent_post( false, '', true );
$prev_post = get_adjacent_post( false, '', false );
}else{
$next_post = get_adjacent_post( false, '', false );
$prev_post = get_adjacent_post( false, '', true );
}
?>
<?php
}
// show related posts by tag
if(!of_get_option('disable_related_posts')){
get_template_part( 'inc/related-posts' );
}
endwhile; // OK, let's stop the post loop once we've displayed it
// If comments are open or we have at least one comment, load up the default comment template provided by Wordpress
if ( comments_open() )
comments_template( '', true );
}else{ // Well, if there are no posts to display and loop through, let's apologize to the reader (also your 404 error) ?>
<article class="post error">
<h1 class="404"><?php _e('Page not found', 'outbox'); ?></h1>
</article>
<?php } // OK, I think that takes care of both scenarios (having a post or not having a post to show) ?>
</div><!-- #content .site-content -->
And I'm getting this error: P**arse error: syntax error, unexpected T_ENDWHILE** in this line: endwhile;
I'm sure is something wrong on how I added the code but it's very confusing for me because the actual code has a lot of php tags around...
Any suggestion? Thanks!
i have changed ,try below code
<div id="content" class="span9" role="main">
<?php if ( 'cpt-parent' == wpse121567_get_cpt_hierarchy() ) {
// Normal loop here
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'inc/post-format/single', get_post_format() );
endwhile; endif;
}
// Child CPT
else if ( 'cpt-child' == wpse121567_get_cpt_hierarchy() ) {
// Globalize post object
global $post;
// Output Parent CPT title and content
$parent = get_post( $post->post_parent );
echo '<h1>' . $parent->post_title . '</h1>';
echo '<div>' . apply_filters( 'the_content', $parent->post_content ) . '</div>';
// Fetch parent CPT comments
$parent_cpt_comments = get_comments( array(
'post_id' => $post->post_parent,
'status' => 'approve'
) );
// Loop through parent CPT comments
foreach ( $parent_cpt_comments as $comment ) {
// Output comment list markup here
}
}
// Grandchild CPT
else if ( 'cpt-grandchild' == wpse121567_get_cpt_hierarchy() ) {
// Comment Stats code goes here
?>
<?php
$enable_rtl = of_get_option('enable_rtl', false);
if(!of_get_option('disable_pagination')){
if($enable_rtl){
$next_post = get_adjacent_post( false, '', true );
$prev_post = get_adjacent_post( false, '', false );
}else{
$next_post = get_adjacent_post( false, '', false );
$prev_post = get_adjacent_post( false, '', true );
}
?>
<?php
}
// show related posts by tag
if(!of_get_option('disable_related_posts')){
get_template_part( 'inc/related-posts' );
}
// If comments are open or we have at least one comment, load up the default comment template provided by Wordpress
if ( comments_open() )
comments_template( '', true );
}else{ // Well, if there are no posts to display and loop through, let's apologize to the reader (also your 404 error) ?>
<article class="post error">
<h1 class="404"><?php _e('Page not found', 'outbox'); ?></h1>
</article>
<?php } // OK, I think that takes care of both scenarios (having a post or not having a post to show) ?>

Categories