Wordpress child page include feutured images - php

So this is my code it shows all the titles of the child pages but i also want all the feutured images of the child pages. And i have no clue how to accomplish this. I have incldue page.php and my function.php script. I know alot of people allready ask this type of quistion but i can't seem to figure it out.
Page.php
<?php
get_header();
if(have_posts()) :
while (have_posts()) : the_post(); ?>
<article class="post page">
<?php
if ( has_children() OR $post->post_parent > 0 ) { ?>
<nav class="site-nav children-links clearfix">
<span class="parent-link"><?php echo get_the_title(get_top_ancestor_id()); ?> </span>
<ul>
<?php
$args = array(
'child_of' => get_top_ancestor_id(),
'title_li' => ''
);
?>
<?php wp_list_pages($args); ?>
</ul>
</nav>
<?php } ?>
</article>
<?php endwhile;
else :
echo '<p> No content found</p>';
endif;
get_footer();
?>
Function.php
function get_top_ancestor_id() {
global $post;
if ($post->post_parent) {
$ancestors = array_reverse(get_post_ancestors($post->ID));
return $ancestors[0];
}
return $post->ID;
}
// Does page have children?
function has_children() {
global $post;
$pages = get_pages('child_of=' . $post->ID);
return count($pages);
}

So i found the answer finally for people that also strungle:
<?php
$args = array(
'child_of' => get_top_ancestor_id(),
'title_li' => ''
);
?>
<?php $our_pages = get_pages($args); ?>
<?php if (!empty($our_pages)): ?>
<?php foreach ($our_pages as $key => $page_item): ?>
<div class="col-*-* product-object">
<a class="product-article" href="<?php echo esc_url(get_permalink($page_item->ID)); ?>">
<div class="product-image" style="background: url(<?php echo get_the_post_thumbnail_url($page_item->ID,'product-image');?>); ">
<h2 class="product-h2"><?php echo $page_item->post_title ; ?></h2>
</div>
</a>
</div>
<?php endforeach ?>
<?php endif ?>
</article>
</div>
<?php

Related

PHP code not showing up on the page

So I have a php page in wordpress and when I add a part of the code it doesn't work. So to explain a bit more. I have a php template and most of it works, but when I add some additional code at the bottom that doesn't show up.
<?php get_header(); ?>
<div class="main_color container_wrap_first container_wrap">
<menu id="nav">
<ul><?php
$category_id = 2;
while( $category_id < 7 ) { ?>
<li>
<a href="<?php echo get_category_link( $category_id ); ?>">
<?php
$cat_id = $category_id;
echo get_cat_name( $cat_id );
$category_id++;
?>
</a>
</li>
<?php } ?>
</ul>
</menu>
<?php
$args = array(
'post_type' => 'projekti',
'posts_per_page' => -1,
'category' => '4',
);
$posts = get_posts($args);
if( $posts ):
$i = 1; ?>
<div class="custom-posts-grid">
<?php foreach($posts as $post): setup_postdata( $post ); ?>
<?php if( have_rows('logotipi') ): ?>
<?php while( have_rows('logotipi') ): the_row();
// vars
$image = get_sub_field('thumbnail_for_logotipi');
$content = get_sub_field('project_name');
$link = get_sub_field('url_logotipi');
$count = count($posts);
?>
<div class="post-grid-logotipi">
<a href="<?php echo $link; ?>">
<div class="post-title-hover"><?php echo $content ?></div>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" class="image-overlay-post" />
</a>
</div>
<?php
$i++;
if( $i == 6 ):
$i++;
?>
<div class="sodelujmo-post-grid"><div class="sodelujmo-post"><p class="sodelujmo-text">Vam je všeč,<br>kar vidite?<br>...<p><div class="button-bg"><a class="sodelujmo-link" href="../u3nek/sodelujmo/">Sodelujmo</a>
</div></div></div>
<?php
endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<!-- after this nothing I add can be shown -->
</div>
</div>
I have no idea what is wrong. So if anyone encountered anything similar before do let me know.

WP posts_per_page or showposts?

New to PHP. Below is the code to show posts and pagination. I'm trying to get 10 posts per page to show and am confused on what code to write to do this. I tried changing Reading Settings to 10 blog posts, but when I save it, it overwrites back to one. So I figured the setting is being overwritten in php somewhere. I'm looking to overwrite that here. Please help.
I tried adding:
but not only does 10 posts show, so does a second category list below the posts.
<?php get_template_part('templates/page', 'header'); ?>
<?php if (!have_posts()) : ?>
<div class="alert">
<?php _e('Sorry, no results were found.', 'roots'); ?>
</div>
<?php get_search_form(); ?>
<?php endif; ?>
<?php $i = 0; ?>
<?php while (have_posts()) : the_post(); $i++; ?>
<article class="<?php $allClasses = get_post_class(); foreach ($allClasses as $class) { echo $class . " "; } if($i&1) { echo 'odd';} else {echo 'even';}; ?> block clearfix">
<?php get_template_part('templates/content-category', get_post_format()); ?>
</article>
<?php endwhile; ?>
<?php if ($wp_query->max_num_pages > 1) : ?>
<nav class="post-nav">
<ul class="pager">
<li class="previous"><?php next_posts_link(__('← Older posts', 'roots')); ?></li>
<li class="next"><?php previous_posts_link(__('Newer posts →', 'roots')); ?></li>
</ul>
</nav>
<?php endif; ?>
try adding this to your functions.php
function trance_posts_per_page( $query ) {
if (! is_main_query())
return;
$query->set( 'posts_per_page', 20 );
}
add_action( 'pre_get_posts', 'trance_posts_per_page' );
check the plugin WP-PageNavi if this doesn't help you
Got to work by removing this:
<?php $i = 0; ?>
<?php while (have_posts()) : the_post(); $i++; ?>
<article class="<?php $allClasses = get_post_class(); foreach ($allClasses as $class) { echo $class . " "; } if($i&1) { echo 'odd';} else {echo 'even';}; ?> block clearfix">
<?php get_template_part('templates/content-category', get_post_format()); ?>
</article>
<?php endwhile; ?>
and replacing it with this:
<?php query_posts( $query_string . '&posts_per_page=-10' );?>
<?php while (have_posts()) : the_post(); ?>
<article class="block clearfix">
<?php get_template_part('templates/contentcategory',
get_post_format()); ?>
</article>
<br />
<?php endwhile; ?>

Creating Conditional Script in PHP / Wordpress

I'm still pretty new to PHP, and I'm having trouble getting this to work. What I want to do, is make the slide linked (if link is available). Otherwise, print the post thumbnail without the
Here's my code so far:
<?php // START SLIDER ?>
<div class="slider">
<ul class="rslides">
<?php $args = array( 'posts_per_page' => 0, 'post_type' => 'slide'); $alert = new WP_Query( $args ); ?>
<?php if( $alert->have_posts() ) { while( $alert->have_posts() ) { $alert->the_post(); ?>
<li><?php the_post_thumbnail('full'); ?><div class="caption"><p class="captiontitle"><?php the_title(); ?></p><p class="caption"><?php the_content(); ?></p></div></li>
<?php } } ?>
</ul>
</div>
<?php wp_reset_query(); ?>
<?php // END SLIDER ?>
I've done this before using the WP Custom fields, but I'm not sure how to apply it to my custom post type (called slider). Here's what I did for my Custom Field script:
<?php $slider_url = get_post_meta($post->ID, 'Slider_URL', true);
if ($slider_url) { ?>
LINKED SLIDE HERE
<?php } else { ?>
UNLINKED SLIDE HERE
<?php } ?>
<?php endwhile; ?>
<?php endif; // have_posts() ?>
Here's what I tried (when combining the two), but there's an error somewhere:
<?php // START SLIDER ?>
<div class="slider">
<ul class="rslides">
<?php $args = array( 'posts_per_page' => 0, 'post_type' => 'slide'); $alert = new WP_Query( $args ); ?>
<?php if( $alert->have_posts() ) { while( $alert->have_posts() ) { $alert->the_post(); ?>
<?php $slide_url = get_post_meta($post->ID, 'Slide_URL', true);
if ($slide_url) { ?>
<li><?php the_post_thumbnail('full'); ?><div class="caption"><p class="captiontitle"><?php the_title(); ?></p><p class="caption"><?php the_content(); ?></p></div></li>
<?php } else { ?>
<li><?php the_post_thumbnail('full'); ?><div class="caption"><p class="captiontitle"><?php the_title(); ?></p><p class="caption"><?php the_content(); ?></p></div></li>
<?php } } ?>
<?php endwhile; ?>
<?php endif; // have_posts() ?>
</ul>
</div>
<?php wp_reset_query(); ?>
<?php // END SLIDER ?>
If I'm correct in thinking, you just want to check if the link is there, before outputting, otherwise, just show the image. Try the following:
<?php // START SLIDER ?>
<div class="slider">
<ul class="rslides">
<?php $args = array( 'posts_per_page' => 0, 'post_type' => 'slide'); $alert = new WP_Query( $args ); ?>
<?php if( $alert->have_posts() ) { while( $alert->have_posts() ) { $alert->the_post(); ?>
<!-- Get a link -->
<?php $theLink = get_post_meta($post->ID, "_location", true); ?>
<li>
<!-- Check for a link -->
<?php if($theLink != ''): ?>
<a href="<?php echo $theLink; ?>" title="More Info">
<?php endif; ?>
<?php the_post_thumbnail('full'); ?>
<div class="caption">
<p class="captiontitle">
<?php the_title(); ?>
</p>
<p class="caption">
<?php the_content(); ?>
</p>
</div>
<!-- Close the link -->
<?php if($theLink != ''): ?>
</a>
<?php endif; ?>
</li>
<?php } } ?>
</ul>
</div>
<?php wp_reset_query(); ?>
<?php // END SLIDER ?>

Wordpress tax_query and pagination issue

I am creating a wordpress template to query my custom post type posts by using tax_query. It works fine and I get the correct posts in page #1, but I see the same posts as seen at page #1 when I click page #2. What did i missing here? Thanks in advance.
<?php
get_header();
?>
<h1><?php
the_title();
?></h1> <hr> <br>
<div id="content" class="three_fourth <?php
echo of_get_option('blog_sidebar_pos');
?>">
<?php
?>
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'tax_query' => array(
array(
"taxonomy" => "wpdic-category",
"field" => "slug",
"terms" => "featured-charities",
"numberposts" => 5,
"paged" => $paged
)
)
);
$wp_query = new WP_Query($args);
?>
<?php
if ($wp_query->have_posts()):
while ($wp_query->have_posts()):
$wp_query->the_post();
?>
<article id="post-<?php
the_ID();
?>" <?php
post_class();
?>>
<header>
<h2><a href="<?php
the_permalink();
?>" title="<?php
the_title();
?>" rel="bookmark"><?php
the_title();
?></a></h2>
<?php
$post_meta = of_get_option('post_meta');
?>
<?php
if ($post_meta == 'true' || $post_meta == '')
{
?>
<div class="post-meta">
<div class="fleft"></div>
<div class="fright"></div>
</div><!--.post-meta-->
<?php
}
?>
</header>
<?php
$post_image_size = of_get_option('post_image_size');
?>
<?php
if ($post_image_size == '' || $post_image_size == 'normal')
{
?>
<?php
if (has_post_thumbnail())
{
echo '<a href="';
the_permalink();
echo '">';
echo '<div class="featured-thumbnail"><div class="img-wrap">';
the_post_thumbnail();
echo '</div></div>';
echo '</a>';
}
?>
<?php
}
else
{
?>
<?php
if (has_post_thumbnail())
{
echo '<a href="';
the_permalink();
echo '">';
echo '<div class="featured-thumbnail large"><div class="img-wrap"><div class="f-thumb-wrap">';
the_post_thumbnail('post-thumbnail-xl');
echo '</div></div></div>';
echo '</a>';
}
?>
<?php
}
?>
<div class="post-content">
<?php
$post_excerpt = of_get_option('post_excerpt');
?>
<?php
if ($post_excerpt == 'true' || $post_excerpt == '')
{
?>
<div class="excerpt"><?php
$excerpt = get_the_excerpt();
echo my_string_limit_words($excerpt, 52);
?></div>
<?php
}
?>
<div id="widget_my_contentwidget"><ul><li><a class="link" href="<?php
the_permalink();
?>"> read more</a></li></ul></div>
<hr>
</div>
</article>
<?php
endwhile;
else:
?>
<div class="no-results">
<p><strong>There has been an error.</strong></p>
<p>We apologize for any inconvenience, please <a href="<?php
bloginfo('url');
?>/" title="<?php
bloginfo('description');
?>">return to the home page</a> or use the search form below.</p>
<?php
get_search_form();
?> <!-- outputs the default Wordpress search form-->
</div><!--noResults-->
<?php
endif;
?>
<?php
if (function_exists('wp_pagenavi')):
?>
<?php
wp_pagenavi();
?>
<?php
else:
?>
<?php
if ($wp_query->max_num_pages > 1):
?>
<nav class="oldernewer">
<div class="older">
<?php
next_posts_link('« Older Entries');
?>
</div><!--.older-->
<div class="newer">
<?php
previous_posts_link('Newer Entries »');
?>
</div><!--.newer-->
</nav><!--.oldernewer-->
<?php
endif;
?>
<?php
endif;
?>
<!-- Page navigation -->
<?php
$wp_query = null;
$wp_query = $temp;
?>
<div id="footer">
<div class="clearfix">
<?php
if (!dynamic_sidebar('Footer Content')):
?>
<?php
endif;
?></div></div>
</div><!--#content-->
<?php
get_footer();
?>
wp_pagenavi() will work with the global $wp_query by default; if you want to paginate some custom query you need to pass it as a parameter, like:
wp_pagenavi( array( 'query' => $mycustomquery ) );
I have encountered the same problem before. For some reasons i cannot make WP query to work, but instead i used query_posts. Here is the code
<?php
/**
* #author MESMERiZE
* #copyright 2012
*/
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'tax_query' => array(
array(
"taxonomy" => "wpdic-category",
"field" => "slug",
"terms" => "featured-charities",
"numberposts" => 5,
"paged" => $paged
)
)
);
query_posts($args);
while(have_posts()): the_post();
endwhile;
?>
Then i added the wp pagenavi function after the endwhile keyword.
if (function_exists('wp_pagenavi')) {
wp_pagenavi();}

Loops for cat links not looping for wordpress site

I have this code and all the vars pull in but the latest news is not showing up. any ideas?
<div class="content">
<?php get_sidebar('field'); ?>
<?php
global $current_user;
get_currentuserinfo();
$user_info = get_userdata($current_user->ID);
if ( have_posts() && $user_info->user_level != 0) : while ( have_posts() ) : the_post(); ?>
<?php // get custom fields
$wt_email = get_post_meta($post->ID, 'wt_email', true);
$wt_feed = get_post_meta($post->ID, 'wt_website', true);
$wt_facebook = get_post_meta($post->ID, 'wt_facebook', true);
$wt_twitter = get_post_meta($post->ID, 'wt_twitter', true);
$wt_linkedin = get_post_meta($post->ID, 'wt_linkedin', true);
?>
<div class="entry">
<h1><?php the_title(); ?></h1>
<div class="body">
<?php the_content(); ?>
</div>
<div class="share">
<div class="links">
<h3>Links</h3>
<ul>
<?php if($wt_twitter) { ?><li>Twitter</li><?php } ?>
<?php if($wt_facebook) { ?><li>Facebook</li><?php } ?>
<?php if($wt_email) { ?><li>Email</li><?php } ?>
<?php if($wt_linkedin) { ?><li>Website</li><?php } ?>
</ul>
</div>
<?php endwhile; // End the loop. Whew. ?>
<div class="news">
<h3>Latest News</h3>
<ul>
<?php
// The Query
$loop = new WP_Query( array(
'category_name' => $wt_feed,
'order' => 'ASC',
'posts_per_page'=> 5
) );
// The Loop
if( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();
echo '<li><a href="' . the_permalink() . '">';
the_title();
echo '</a></li>';
endwhile;
endif;
// Reset Query
wp_reset_query();
?>
</ul>
<?php
$args=array(
'category_name' => $wt_feed,
'type' => 'post'
);
$categories=get_categories($args);
if($wt_feed) { ?>Subscribe<?php } ?>
</div>
</div>
</div>
<?php else : ?>
<div class="entry">
<h1>Listing Private</h1>
<div class="body">
<p>You need to have a Member account view the details of this list. Request an account membership.</p>
</div>
</div>
<?php endif; ?>
<div class="clearfix"></div>
</div>
Whilst not an exact answer, have you tried adding:
error_reporting(E_ALL);
ini_set('display_errors', '1');
At the top of the page, to display any errors that might be occurring?

Categories