Only run wordpress query in one section - php

I currently have a setup where i have the following files being called in order.
in my header.php, i am calling <?php get_theme_part( 'front', 'current-front' ); ?>
The contents of that file are
<?php query_posts("showposts=1&cat=6"); ?>
<?php
/**
* The template for displaying list of recent posts on the front page.
*
* #package WordPress
* #subpackage xxx
* #since xxx 1.0
*/
if ( !have_posts() )
return;
?>
<div class="front-current-print">
<div class="current-print">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_theme_part( 'current-print' ); ?>
<?php endwhile; ?>
</div>
</div>
That file is calling current-print.php, which has this code:
<?php
?><article <?php post_class( 'compact bg-secondary' ); ?>>
<?php the_post_thumbnail( 'current-print' ); ?>
<h5 class="title">
<a class="inverse" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h5>
<div class="entry-summary"><?php the_excerpt(); ?></div>
<?php the_post_episode(); ?>
</article>
This code allows me to have a thumbnail for a specific post from a specific category.
This works fine on the front page, but it seems that for all inside pages and posts it will only display the category and the post. I've tried taking out <?php query_posts("showposts=1&cat=6"); ?> and the posts come through fine. Is there any way i can have that script only run in the header and not affect the rest of the site?

You need to use wp_reset_query();:
wp_reset_query() restores the $wp_query and global post data to the original main query. This function should be called after query_posts(), if you must use that function.
See: http://codex.wordpress.org/Function_Reference/wp_reset_query
In your case this would be:
<?php query_posts("showposts=1&cat=6"); ?>
<?php
/**
* The template for displaying list of recent posts on the front page.
*
* #package WordPress
* #subpackage xxx
* #since xxx 1.0
*/
if ( !have_posts() )
return;
?>
<div class="front-current-print">
<div class="current-print">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_theme_part( 'current-print' ); ?>
<?php endwhile; ?>
</div>
</div>
<?php wp_reset_query(); ?>

Related

Wordpress: Relationship field inside a block element

I am trying to create a custom block using ACF. I have followed their details here: https://www.advancedcustomfields.com/resources/blocks/
I am having an issue displaying the content from my relationship field though...
I have setup the custom post type
I have created the ACF custom field which links to the block
I have added content to my custom posts.
It displays 2 posts.. but none of the data.
Attached is my code and reviewing the ACF site I cannot work out what I am doing wrong. Please help.
<?php
/**
* Client Slider Template.
*
* #param array $block The block settings and attributes.
* #param string $content The block inner HTML (empty).
* #param bool $is_preview True during AJAX preview.
* #param (int|string) $post_id The post ID this block is saved to.
*/
?>
<?php
$featured_posts = get_field('clients');
if( $featured_posts ): ?>
<div class="owl-carousel owl-theme work-snippets">
<?php foreach( $featured_posts as $post ):
setup_postdata($post); ?>
<div>
<?php
$image = get_field('client_logo');
if( !empty( $image ) ): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
<p><strong><?php the_field( 'project_title' ); ?></strong></p>
<p><?php the_field( 'project_overview' ); ?></p>
</div>
<!-- Close Work Snippet -->
<?php endforeach; ?>
</div>
<!-- Work Snippets -->
<?php wp_reset_postdata(); ?>
<?php endif; ?>

Testimonials shortcode also showing in my site? How to avoid this issue

Hey Guys Extremely sorry for the silly question because I am newbie to wordpress. I just wanted to show user testimonials.
For that I Installed Easy Testimonials Plugin.
Then I creates a new page called Testimonials. In that I give the below shortcode
[testimonials theme="default_style" paginate="all" count="5" orderby="date" order="ASC" show_title="0" use_excerpt="0" show_thumbs="1" show_date="1" show_other="1" hide_view_more="1" output_schema_markup="1" show_rating="stars"]
Upto now all are working fine, But in my site its showing that shortcode that I have given in the testimonial page.
My admin panel
This is my page.php
<?php
/**
* The template for displaying all pages.
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages and that other
* 'pages' on your WordPress site will use a different template.
*
* #package OceanWP WordPress theme
*/
get_header(); ?>
<?php do_action( 'ocean_before_content_wrap' ); ?>
<div id="content-wrap" class="container clr">
<?php do_action( 'ocean_before_primary' ); ?>
<div id="primary" class="content-area clr">
<?php do_action( 'ocean_before_content' ); ?>
<div id="content" class="site-content clr">
<?php do_action( 'ocean_before_content_inner' ); ?>
<?php
// Elementor `single` location
if ( ! function_exists( 'elementor_theme_do_location' ) || ! elementor_theme_do_location( 'single' ) ) {
// Start loop
while ( have_posts() ) : the_post();
get_template_part( 'partials/page/layout' );
endwhile;
} ?>
<?php do_action( 'ocean_after_content_inner' ); ?>
</div><!-- #content -->
<?php do_action( 'ocean_after_content' ); ?>
</div><!-- #primary -->
<?php do_action( 'ocean_after_primary' ); ?>
<?php do_action( 'ocean_display_sidebar' ); ?>
</div><!-- #content-wrap -->
<?php do_action( 'ocean_after_content_wrap' ); ?>
<?php get_footer(); ?>
I suggest you to go to admin panel Testimonials->All Testimonials select edit testimonial and check description content there
in your page.php put below code anywhere and check it working or not
<?php echo do_shortcode('[testimonials theme="default_style" paginate="all" count="5" orderby="date" order="ASC" show_title="0" use_excerpt="0" show_thumbs="1" show_date="1" show_other="1" hide_view_more="1" output_schema_markup="1" show_rating="stars"]'); ?>

Wordpress excerpt's Read More takes me to the same excerpt again

My problem is the "Read on" button that is shown under every excerpt, is redirecting me to the same excerpt, too, instead of taking me to the full post. I have a complete suspect of my code, it's probably 100% different to those codes I've seen around on tutorials for adding Excerpt, for example I don't require using the < ?php the_content(); ?> to display the post, or < ?php the_excerpt(); ?> for the excerpt. I'm no longer able to reach the developer who did the work so I'm just lost. To my best knowledge, you will need the Index.php to look at, so here you go.
Index.php:
<?php
/**
* The main template file
*
* This is the most generic template file in a WordPress theme
* and one of the two required files for a theme (the other being style.css).
* It is used to display a page when nothing more specific matches a query.
* For example, it puts together the home page when no home.php file exists.
*
* #link http://codex.wordpress.org/Template_Hierarchy
*
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
get_header(); ?>
<article data-menu-block="news" class="homepage_news dark_bg">
<div class="inner_wrapper row">
<div class="left_column">
<h5>LATEST NEWS <span class="red">\</span></h5>
<div class="news_list">
<ul>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentytwelve_content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<?php if ( current_user_can( 'edit_posts' ) ) :
// Show a different message to a logged-in user who can add posts.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'No posts to display', 'twentytwelve' ); ?></h1>
</header>
<div class="entry-content">
<p><?php printf( __( 'Ready to publish your first post? Get started here.', 'twentytwelve' ), admin_url( 'post-new.php' ) ); ?></p>
</div><!-- .entry-content -->
<?php else :
// Show the default message to everyone else.
?>
<header class="entry-header">
<h1 class="entry-title"><?php _e( 'Nothing Found', 'twentytwelve' ); ?></h1>
</header>
<div class="entry-content">
<p><?php _e( 'Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
<?php endif; // end current_user_can() check ?>
</article><!-- #post-0 -->
<?php endif; // end have_posts() check ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Take a look on content.php file in your theme folder.
<?php get_template_part( 'content', get_post_format() ); ?>
The above part load the external php file which is located in your theme folder. Most probably, there are more files exists like content-page, content-tags etc. depands of your developer.

Pagination not working in the main index.php

So my link to show the older posts is not displaying on my main blog page http://surechamp.com/blog/ it's displaying fine in my archive.php so I'm not sure why it's not working with the index.php. I'm using underscores as my base theme to work off of. This is my code:
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
?>
<?php endwhile; ?>
<?php _s_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
I typically run paging navigation outside the loop and test if there will be a previous or next link shown:
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<?php if($wp_query->get('paged') || $wp_query->get('paged') > 1): ?>
<a class="loadPrev" href="<?php previous_posts(); ?>">prev</a>
<?php endif; ?>
<?php if ($next_url = next_posts($wp_query->max_num_pages, false)): ?>
<a class="loadNext" href="<?php echo $next_url; ?>">next</a>
<?php endif;?>
<?php endif; ?>

Wordpress PHP posts using category as query

PHP noob here - all round web noob tbh. Anyhoo been trying to get this going for a while now.
The site is http://talkativebroadcasting.co.uk
Basically have a page of posts called "Posts" - thats working fine
Have a "Talkative Blog" page where I only want posts with category of "BLOG" posted - works fine-ish
Have a sub page of "BTCC" called interviews where I only want pages of category "BTCC" or any subcategory of BTCC post posted - again ok-ish
Current code in Page.php is
<?php
/**
* The template for displaying all pages.
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages
* and that other 'pages' on your WordPress site will use a
* different template.
*
* #package WordPress
* #subpackage Twenty_Twelve
* #since Twenty Twelve 1.0
*/
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php comments_template( '', true ); ?>
<?php
if (is_page('talkative-blog')) {
query_posts('category_name=BLOG');
} elseif (is_page(17)) {
query_posts('category_name=BTCC');
}
?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Main issues still present:
Featured images are no longer present, neither are tags etc.
When say 10 posts are added you cannot go older/newer as like on the "Posts" page
In fact it would be perfect if it operated the same as the "Posts" page which will eventually be hidden and just operate behind the scenes
Thanks
In order to only display posts from a single category (or subcategory), you do not need to write any code on page.php file of wordpress. If you want to modify the way posts or list of posts work, on twenty twelve theme, check the file category.php.
In order to display the posts of the wanted category you only have to call the category's link. For example, in order to show all the posts on BTCC category, just use this url: http://talkativebroadcasting.co.uk/category/BTCC/
In the same way, you can show all categories you need.
your define code must be use before while loop:
<?php
if (is_page('talkative-blog')) {
query_posts('category_name=BLOG');
} elseif (is_page(17)) {
query_posts('category_name=BTCC');
}
?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'thumbnail');
?>
<?php if ( has_post_thumbnail() ) { ?>
<img src="<?php echo $image_url[0]; ?>" alt="<?php the_title(); ?>" />
<?php } ?>
<?php endwhile; // end of the loop. ?>
http://codex.wordpress.org/Function_Reference/query_posts

Categories