Why does wp_query make my website loop endlessly? - wordpress

I have a custom post type named " actualites ".
I want to wp_query the post data ( title and content for now ).
Here's the code I used :
<div class="row">
<div class="col-sm-8 blog-main">
<?php
// the query
$query = new WP_Query( array( 'post_type' => 'actualites' ) ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$the_query->the_post();
echo '<div class="blog-post">';
echo '<h2 class="blog-post-title">' . get_the_title() . '</h2>';
echo '<p class="the-content">' . the_content() . '</p>';
echo '</div>';
endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php esc_html_e( 'Désolé, aucun post ne correspond à votre requête.' ); ?></p>
<?php endif; ?>
</div>
<!-- /.blog-main -->
<div class="col-sm-3 col-sm-offset-1 blog-sidebar">
<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
<ul id="sidebar">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</ul>
<?php endif; ?>
</div><!-- /.blog-sidebar -->
For some reason this makes my website loop endlessly. Nothing appears, not even the header or the footer. Why does this happen ? It's 100% the wp_query part.
I tried the standard query and had an "expected statement " that probably was the cause of my issue. I'm trying the alternate wp query code now, and I don't have any clue as to what doesn't work. Any help ? Thanks !

Your Code should be as follows. In your code $the_query variable is not defined. I think that's why it's not working. Also turn on WP DEBUG MODE when you are developing. So you will be able to see errors and warnings.
<?php
// the query
$query = new WP_Query( array( 'post_type' => 'actualites' ) ); ?>
<?php if ( $query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $query->have_posts() ) : $query->the_post();
echo '<div class="blog-post">';
echo '<h2 class="blog-post-title">' . get_the_title() . '</h2>';
echo '<p class="the-content">' . the_content() . '</p>';
echo '</div>';
endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php esc_html_e( 'Désolé, aucun post ne correspond à votre requête.' ); ?></p>
<?php endif; ?>

Related

How do I use get_content WordPress function?

I am not able to fetch content while using the the_content WordPress function.
<!-- Start the Loop. -->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ( in_category( '3' ) ) : ?>
<div class="post-cat-three">
<?php else : ?>
<div class="post">
<?php endif; ?>
<!-- Display the Title as a link to the Post's permalink. -->
<h2><?php the_title(); ?></h2>
<!-- Display the date (November 16th, 2009 format) and a link to other posts by this posts author. -->
<small><?php the_time('F jS, Y'); ?> by <?php the_author_posts_link(); ?></small>
<!-- Display the Post's content in a div box. -->
<div class="entry">
<?php the_content(); ?>
</div>
<!-- Display a comma separated list of the Post's Categories. -->
<p class="postmetadata"><?php _e( 'Posted in' ); ?> <?php the_category( ', ' ); ?></p>
</div> <!-- closes the first div box -->
<!-- Stop The Loop (but note the "else:" - see next line). -->
<?php endwhile; else : ?>
<!-- The very first "if" tested to see if there were any Posts to -->
<!-- display. This "else" part tells what do if there weren't any. -->
<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<!-- REALLY stop The Loop. -->
<?php endif; ?>
I have really tried but get empty description result kindly help
Here a example fetch description from post in Wordpress
<?php
$categories = get_the_category( $id );
if( $categories ){
// Assumes you just want the first category
print 'You’re in the ' . $categories[ 0 ]->name . ' category';
}
?>
<ul>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$attachments = get_posts( array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null
) );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
?>
<li><?php echo wp_get_attachment_image( $attachment->ID, 'full' ); ?>
<p><?php echo apply_filters( 'the_title', $attachment->post_title ); ?></p>
</li>
<?php
}
}
endwhile; endif; ?>
</ul>
<p><?php echo apply_filters( 'the_title', $attachment->post_title ); ?></p> <!-- do you need change post_title for description -->

How to link a post to its category in WordPress

I want to add a link when my post gets click on. The link has to go to the category of the post. I've got this far but now I'm stuck. Can anyone show me how I can do this?
<?php
$args = array(
'category_name' => 'portriats',
'posts_per_page' => 1
);
$qry = new WP_Query($args);
if ( $qry->have_posts() ) :
while ( $qry->have_posts() ) : $qry->the_post();
$postcat = get_the_category( $post->ID );
?>
<div class="hometile">
<a href="<?php get_category_link( $postcat) ?>">
==> **I need to get the category of the post and then let PHP print the link of the categorey in the href**
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php
endwhile;
endif;
?>
It will display primary category of post with link
<?php $qry = new WP_Query($args ); ?>
<?php if ( $qry->have_posts() ) : ?>
<?php while ( $qry->have_posts() ) : $qry->the_post(); $postcat = get_the_category();?>
<div class="hometile">
<a href="<?php echo get_category_link( $postcat[0]->term_id ); ?>">
<?php echo $postcat[0]->name; ?>
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>

WP_Query Not working or not displaying post content

I'm quite new to WordPress Development been following some online tuts and read bits and bobs of documentation but my WP_Query still displays nothing and when I add an else to my loop the else condition displays. I have created the page I'm trying to pull from the database what am I doing wrong here's the code:
<section id="home">
<?php
$query = new WP_Query( array( 'pagename' => 'home' ) );
if ($query->have_post() ) {
while ($query->have_posts() ){
$query->the_post();
echo '<div class="entry-content">';
the_content();
echo '</div>';
}
}
wp_reset_postdata();
?>
</section>
simply change
$query->have_post()
to
$query->have_posts()
use the below code for display content
$args=array(
'post_type' => 'post'
);
$the_query = null;
// the query
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content"><?php the_content(); ?></div>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Content gets replaced after adding "get_template_part"

I am trying to add a Slider on my home page using a template from themeforest but I don't get any support there.
I am adding the following code to my header:
<?php get_template_part ( 'includes/featured/featured-call'); ?>
and this code calls featured-call.php and from there another files is called, flexislider.php that looks like this:
<section>
<div class="spaced-wrap clearfix">
<div class="flexslider-container clearfix">
<div class="flexslider-loader">
<div class="flexslider">
<ul class="slides">
<?php
$captioncodes="";
$count=0;
query_posts( array( 'post_type' => 'mtheme_featured', 'showposts' => -1, 'orderby' => 'menu_order', 'order' => 'ASC') );
?>
<?php if( have_posts() ) : while( have_posts() ) : the_post(); ?>
<?php
$image_id = get_post_thumbnail_id(($post->ID), 'full');
$image_url = wp_get_attachment_image_src($image_id,'full');
$image_url = $image_url[0];
$custom = get_post_custom(get_the_ID());
$featured_description="";
$featured_link="";
if ( isset($custom["featured_bigtitle"][0]) ) $featured_bigtitle=$custom["featured_bigtitle"][0];
if ( isset($custom["featured_description"][0]) ) { $featured_description=$custom["featured_description"][0]; }
if ( isset($custom["featured_link"][0]) && $custom["featured_link"][0]<>"" ) {
$featured_link=$custom["featured_link"][0];
} else {
$featured_link = get_post_permalink();
}
//$textblock=$featured_description;
$title=get_the_title();
$text=$featured_description;
$permalink = $featured_link;
$count++;
?>
<li>
<a href="<?php echo $permalink; ?>">
<img src="<?php echo $image_url; ?>" alt="<?php the_title(); ?>" />
</a>
<?php
$titlecode ='<div class="flex-title">' .$title . '</div>';
$captioncodes ='<div class="flex-caption">' . $text . '</div>';
$bigtitle='<div class="flex-bigtitle">'.$featured_bigtitle.'</div>';
echo '<div class="flex-caption-wrap">';
echo $titlecode;
echo $captioncodes;
echo $bigtitle;
echo '</div>';
?>
</li>
<?php
endwhile; endif;
?>
</ul>
</div>
</div>
</div>
</div>
The problem I have is that once this works, it loads the sliders as posts to the home page and instead of the page I had selected (Home). The page loads fine if I delete the "get_template_part" from header.php, otherwise the sliders come as posts and I don't see the page I selected from reading on wordpress.
My website is http://van-london.com/
I made it!
All I needed was this code after the "get_template_part"
<?php wp_reset_query(); ?>
Done! :)

Wordpress - display certain categories on homepage

I have been trying to alter my current code, but it makes no sense to me (it doesn't do what I was told it does). All I want to do is only display posts with category 13 on my homepage and I want to be able to add multiple categories to a post.
Post 1 (Category 13 and 1) = displayed on homepage
Post 2 (Category 13 and 4 and 5) = displayed on homepage
Post 3 (Category 6 and 1) = not visible on homepage
Post 4 (Category 2) = not visible on homepage
This is my current code to only show category 13 on my homepage, if another category is added to the post it won't be displayed at all.
<?php get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php
if (is_home()) {
query_posts("cat=-6,-4,-1,-11");
}
?>
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'cat' => 13,
'posts_per_page' => 5,
'paged' => $paged);
query_posts($args);
?>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Geen berichten beschikbaar', 'twentyeleven' ); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php _e( 'Helaas, er zijn nog geen gearchiveerde berichten in deze categorie. ', 'twentyeleven' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php endif; ?>
You're looking way too hard at this code. You don't need to exclude anything, you just need to include cat 13, which will include everything that is in cat 13; even if it is in other categories as well. Just run a regular WP_Query() like so:
<?php get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php
if (is_home()) {
// The Query
$the_query = new WP_Query("cat=13, paged=".get_query_var('paged'));
}
?>
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php /* Start the Loop */ ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Geen berichten beschikbaar', 'twentyeleven' ); ?></h1>
</header><!-- .entry-header -->
<div class="entry-content">
<p><?php _e( 'Helaas, er zijn nog geen gearchiveerde berichten in deze categorie. ', 'twentyeleven' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</article><!-- #post-0 -->
<?php endif; ?>
(had to add a few charachters or I could not edit, sorry :P)
Try this...
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=6&cat=13'.'&paged='.$paged);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
Your stuff goes here...
<?php endwhile; ?>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
Try this code
<?php
global $post;
if (is_home()) {
$args = array( 'numberposts' => 5, 'category' => 13 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content( 'Continue reading <span class="meta-nav">»</span>); ?>
</div>
<?php endforeach;
}
?>
This will display the title and content. If you want additional information like date, author, category, tag etc, you may have to use other template tags. Also if you want to style these differently, then you have to use appropriate CSS.

Resources