Wordpress loop goes into infinite loop when adjust - php

I am using KLEO theme for my work.
I am trying to filter posts by category when the date archive is called.
I did this by modifying the core files of the theme. However when the code is run it goes into infinite loop and definitely doesn't produce the results required.
e.g. if I go to localhost/wordpress/2014/06?category=events it should show posts on that date that are only from this category. However it goes into the infinite loop.
The modified code is here:
<?php
/**
* The template for displaying Archive pages
*
* Used to display archive-type pages if nothing more specific matches a query.
* For example, puts together date-based pages if no date.php file exists.
*
* If you'd like to further customize these archive views, you may create a
* new template file for each specific one. For example, Twenty Fourteen
* already has tag.php for Tag archives, category.php for Category archives,
* and author.php for Author archives.
*
* #link http://codex.wordpress.org/Template_Hierarchy
*
* #package WordPress
* #subpackage Kleo
* #since Kleo 1.0
*/
get_header(); ?>
<?php
//Specific class for post listing */
$blog_type = sq_option('blog_type','masonry');
$template_classes = $blog_type . '-listing';
if ($blog_type == 'standard' && sq_option('blog_meta_status', 1) == 1) { $template_classes .= ' with-meta'; }
add_filter('kleo_main_template_classes', create_function('$cls','$cls .=" posts-listing '.$template_classes.'"; return $cls;'));
?>
<?php get_template_part('page-parts/general-title-section'); ?>
<?php get_template_part('page-parts/general-before-wrap'); ?>
<?php if ( have_posts() ) : ?>
<?php
if ($blog_type == 'masonry') {
echo '<div class="row">'
.'<div class="grid-posts kleo-isotope masonry">';
}
?>
<?php
function the_loop($the_query){
// Start the Loop.
while ( $the_query ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
if ($blog_type == 'masonry') {
get_template_part( 'page-parts/post-content-masonry');
}
else {
get_template_part( 'content', get_post_format() );
}
endwhile;
}
if( is_date() ){
if(isset($_GET["category"])){
$category_slug = trim($_GET["category"]);
$args=array('category_name = '.$category_slug.'');
$the_query = new WP_Query( $args );
$lol =$the_query->have_posts();
the_loop($lol);
}
}
else{
$the_query=have_posts();
the_loop($the_query);
}
?>
<?php
if ($blog_type == 'masonry') {
echo '</div>'
.'</div>';
}
?>
<?php
// page navigation.
kleo_pagination();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
<?php get_template_part('page-parts/general-after-wrap'); ?>
<?php get_footer(); ?>
Please help!!!

Related

i can not access the post in custom template of my wordpress plugin

i added following code to use custom template for my recipes single page
function override_single_template( $single_template ){
global $post;
if ($post->post_type == "recipes"){
$single_template = plugins_url('/recipe-single-page-template.php',__FILE__);
}
return $single_template;
}
add_filter( 'single_template', 'override_single_template',10);
and in my template i added following code
<?php
/*
Template Name: recipe-single-page-template
Template Post Type: recipes
*/
require_once("../../../wp-load.php");
?>
<?php get_header(); ?>
<?php echo $post->ID?>
<?php get_footer(); ?>
but i do not access the post and echo out post id will cuses the following error
Trying to get property of non-object
var dump $post outputs null
NULL
and following code will print out my custom template address
$current_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo $current_url;
top code result:
https://charter.test/wp-content/plugins/recipe-plugin/templates/single-recipes.php
now whate should i do?
Are you trying to redirect a custom post type called recipes to a custom template? You don't need to override, by default wordpress has a built in dynamic content display system.
In your case, upon displaying a recipe it will search first for single-recipe.php, if not found, fallback on single.php, if not found, fallback on 404.php and finally on index.php
You just have to create a file called single-recipe.php.
Your second problem is that there is no loop displayed in your file, you have to tell to wordpress that if a post exist, it should retrieve it and present it to you. For that we use the loop system.
Your single-recipe.php file should look like something like this:
<?php
/**
* Get theme header
*/
get_header();
/**
* Start loop
*/
if ( have_posts() ):
while ( have_posts() ):
the_post();
/**
* If we find a post, output the tilte
*/
echo the_title().'<br/>';
/**
* End loop
*/
endwhile;
endif;
/**
* Get theme footer
*/
get_footer(); ?>

wordpress - get post that is assigned to the template

I want to get the post that is assigned to this template and display the content,
currently, I can only display the post content if I put the post id manually. I only want to display the content if the post is assigned to this template and display the content inside woocommerce page
<?php
/**
* Template Name: Sign up Page
*
* #package WordPress
* #subpackage Twenty_Fourteen
* #since Twenty Fourteen 1.0
*/
echo 'this is the signup page';
$post = get_post(2);
$output = apply_filters( 'the_content', $post->post_content );
echo $output;
Updated:
You could try to use get_page_template_slug() function to check, if the current page uses specific template, something like:
while ( have_posts() ) : the_post();
$current_template = get_page_template_slug( get_the_ID() );
if ($current_template == 'signup.php' ) {
the_content();
}
endwhile;

Shortcode not working over index.php only

I have download some plug-in to render youtube search inside my site, the shortcode working perfect at pages/posts but when I use <?php echo do_shortcode('[soundcloud_wpress]');?> over my index.php, it's doesn't show anything.
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.
* e.g., it puts together the home page when no home.php file exists.
*
* Learn more: {#link https://codex.wordpress.org/Template_Hierarchy}
*
* #package WordPress
* #subpackage Twenty_Fifteen
* #since Twenty Fifteen 1.0
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<div class="row searchdiv-wrapper">
<div class="col-sm-6 youtube-search">
<?php echo do_shortcode('[youtube_wpress]');?>
</div>
<div class="col-sm-6 soundcloud-search">
<?php echo do_shortcode('[soundcloud_wpress]');?>
</div>
</div>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php get_footer(); ?>
The plugin add shortcode:
$GLOBALS['ygp_youtube_wpress'] = get_option('ygp_youtube_wpress');
$GLOBALS['ygp_youtube_wpress']['plugin_code'] = 'youtube_wpress';
$GLOBALS['ygp_youtube_wpress']['item_name'] = 'youtube_wpress';
//error_reporting(E_WARNING);
require_once dirname( __FILE__ ).'/include/vbox/include/webzone.php';
require_once dirname( __FILE__ ).'/activation.php';
$a1=new Youtube_wpress_activation();
if($a1->verify_activation()) {
require_once dirname( __FILE__ ).'/youtube_widget.php';
}
if(is_admin()) {
require_once dirname( __FILE__ ).'/admin/options.php';
}
class Youtube_wpress {
function Youtube_wpress() {
if(is_admin()) {
register_activation_hook(__FILE__, array(__CLASS__, 'on_plugin_activation'));
//Settings link
add_filter( 'plugin_action_links', array(__CLASS__, 'plugin_action_links'), 10, 2);
}
//Shortcodes
add_shortcode('youtube_wpress', array(__CLASS__, 'youtube_wpress_shortcode'));
}
function add_scripts_wp_footer() {
}
I have added only the first top code from the plug-in class .
Any idea why echo shortcode not working only inside index.php ?
Thanks and sorry for beginner questions.
Here are some notes on how you should use the do_shortcode() function:
<?php
add_filter( 'the_content', 'do_shortcode', 11 ); // From shortcodes.php
// Use shortcode in a PHP file (outside the post editor).
echo do_shortcode( '' );
// In case there is opening and closing shortcode.
echo do_shortcode( '[iscorrect]' . $text_to_be_wrapped_in_shortcode . '[/iscorrect]' );
// Enable the use of shortcodes in text widgets.
add_filter( 'widget_text', 'do_shortcode' );
// Use shortcodes in form like Landing Page Template.
echo do_shortcode( '[contact-form-7 id="91" title="quote"]' );
// Store the short code in a variable.
$var = do_shortcode( '' );
echo $var;
?>
There is an exception to the built-in embed shortcode available with WordPress. In order to use this shortcode with do_shortcode(), you can do the following:
<?php
$embedurl = 'http://yourembeddableurl.com';
if ( ! empty( $embedurl ) ) {
$var = apply_filters( 'the_content', "" );
echo $var;
}
?>

WP how do I give different style to the content displayed in category.php

How do I use the same 'content' displayed on Index.php and content.php but with a completely different style class assigned to it when it is displayed in category.php? How can I specify it?
category.php
<section class="main">
<div class="mod"><h5><?php printf( __( 'Recent %s \ ', 'twentytwelve' ), single_cat_title( '', false ) ); ?></h5></div>
<ul id="franchise_list" class="tout_list">
<?php if ( have_posts() ) : ?>
<?php
/* Start the Loop */
while ( have_posts() ) : the_post();
/* Include the post format-specific template for the content. If you want to
* this in a child theme then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format(), '<a class="franchise">' . single_cat_title() );
endwhile;
twentytwelve_content_nav( 'nav-below' );
?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
<?php wpbeginner_numeric_posts_nav(); ?>
First of all, you are using get_template_part() wrong. You cannot add class to it. Look at the docs. This is the correct syntax
get_template_part( $slug, $name );
If you need a custom content file for a category, you can simply copy content.php, rename it content-category.php, add your custom classes and change what you need to change, save it and then in category.php change this line
get_template_part( 'content', get_post_format(), '<a class="franchise">' . single_cat_title() );
to this
get_template_part( 'content', 'category' );

Wordpress PHP - categories

I think my previous question was over complicated and, to be honest, was confusing me, nevermind the people trying to answer.
I currently have to pages, both with one category of posts assigned to them, however both post pages are using the same content.php and content-single.php, but i was both pages to use different iterations of these pages for cosmetic reasons.
As an example, visit http://dev.n8geeks.com/blog/ and click on the first blog post. It displays a thumbnail, which is cool and is what i want. However, now on the "videos" page as seen here; http://dev.n8geeks.com/videos/ (once there, click on the post) it also shows the thumbnail box (but no thumbnails will be attached on this posts page category).
This is why i need to user different iterations of content.php and content-single.php, but i simply don't know how. It would also be great if the "videos" page had the same formatting as the "blog" page, but again, i don't know how to achieve this.
The code i'm using for the current "videos" page is as below.
<?php get_header(); ?>
<div id="content">
<div id="main">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; else: endif; ?>
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1 class="entry-title"><a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a></h1>
<p><?php the_content(); ?>
<?php endwhile; else: endif; ?>
</div>
</div>
<?php get_footer(); ?>
Thanks in advance, i really appreciate any help like you wouldn't believe - it's 4:33am and i'm going insane trying to find a fix for this.
Regards
Still extremely confusing, haha, but from the sounds of it you want different templates to show up based on which category the post is in when you're viewing a single post?
If so, you could try setting this as you single.php:
<?php get_header(); ?>
<?php
if ( have_posts() ) { the_post(); rewind_posts(); }
if ( in_category(1) || in_category(2) ) {
include(TEMPLATEPATH . '/single-cat1-2.php');
}
else {
include(TEMPLATEPATH . '/single-default.php');
}
?>
<?php get_footer(); ?>
(from http://wordpress.org/support/topic/alternate-single-post-template-for-specific-categories)
And create the files 'single-cat1-2.php' and 'single-default.php', just add to the if statement checking to see if the post is in a certain category (or categories) and load the correct template. You can use ID, name, and their slug as selectors for the in_category function as well, read more here.
EDIT:
Kay, well you do need to learn plugin programming to really do this. I've begun a quick plugin for you to help you out. It works, just isn't perfect. You could definitely use a different system, like tying the categories in the category menu, but I didn't feel like playing with the Settings API for that page.
So make a new directory in your plugins directory, call it PostCatTheme, make a new file in there called index.php and put this in it:
<?php
/*
* Plugin Name: Post Category Templates
*/
//Replace __FILE__ with whatever the real path is because of symbolic link
/**
* Allows declarations of which categories a single-post template is assigned to
*/
class WordpressPostCatTheme
{
private $pluginDir, $templates;
function __construct ()
{
$this->pluginDir = dirname(__FILE__);
add_action("init", array($this, "load"));
add_filter('single_template', array($this, 'get_post_template'));
}
public function WPCT_deactivate ()
{
delete_option("PostCatTheme_templates");
}
public function load ()
{
register_deactivation_hook( __FILE__, array(&$this, 'WPCT_deactivate') );
$this->templates = get_option("PostCatTheme_templates");
if ($this->templates === FALSE)
{
$this->templates = $this->get_post_templates();
update_option("PostCatTheme_templates", $this->templates);
}
}
// This function scans the template files of the active theme,
// and returns an array of [category] => {file}.php]
public function get_post_templates()
{
$themes = get_themes();
$theme = get_current_theme();
$templates = $themes[$theme]['Template Files'];
$post_templates = array();
$base = array(trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()));
foreach ((array)$templates as $template)
{
$template = WP_CONTENT_DIR . str_replace(WP_CONTENT_DIR, '', $template);
$basename = str_replace($base, '', $template);
// don't allow template files in subdirectories
if (false !== strpos($basename, '/'))
continue;
$template_data = implode('', file( $template ));
$categories = '';
if (preg_match( '|Categories (.*)$|mi', $template_data, $categories))
$categories = _cleanup_header_comment($categories[1]);
//The categories are split by a | (pipe), if there aren't any pipes, assume it's just
//one category, otherwise split at the pipe
if (empty($categories))
continue;
if (strpos($categories, "|") === FALSE)
$categories = array($categories);
else
$categories = explode("|", $categories);
foreach ($categories as $category)
{
if (!empty($category))
{
if (isset($post_templates[$category]))
throw new Exception("Error, category assigned to more than one template");
if(basename($template) != basename(__FILE__))
$post_templates[trim($category)] = $basename;
}
}
}
//file_put_contents($this->pluginDir . "/log", json_encode($post_templates));
return $post_templates;
}
// Filter the single template value, and replace it with
// the template chosen by the user, if they chose one.
function get_post_template($template)
{
global $post;
$cats = wp_get_post_categories($post->ID);
//Go through each category, until one hits
foreach ($cats as $c)
{
$templateP = $this->templates[$c];
if(!empty($templateP) && file_exists(TEMPLATEPATH . "/{$templateP}"))
{
$template = TEMPLATEPATH . "/{$templateP}";
break;
}
}
return $template;
}
}
if (!isset($PostCatThemePlugin))
$PostCatThemePlugin = new WordpressPostCatTheme;
?>
After that, in your custom single.php template, add the code Categories: 1|2 in the header section (where Template Name is). Whenever you change or add these, make sure to deactivate and reactivate the plugin, to refresh the cache that this information is stored in.
To get the ID of a category, edit a category and in the URL, the number after tag_ID= is the category's ID.
Hope that helps some,
Max

Categories