Having some trouble with a wordpress blog. I am trying to show posts with specific categories based on the section of the site the user came from. Everything is working except the 'query_posts' inside of the the if/else statement. I have the following php:
<?php
/*
Template Name: Blog
*/
get_header(); ?>
<?php
// Find out if the user came to the blog from 'Experienced' or 'College' section of the site
$came_from = wp_get_referer();
// Show posts with categories based on where the user came from
if (strpos($came_from,'experienced') !== false) {
$text = 'test';
query_posts('cat=experienced-professionals');
// wp_reset_query();
} else {
$text = 'heyo';
query_posts('cat=college-students');
// wp_reset_query();
}
?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div style="background:red;width:100%;height:200px;"></div>
<div id="container">
<div id="content" role="main">
<h1 class="entry-title"><?php the_title(); ?></h1>
<p><?php echo $text; ?></p>
<?php print_r($came_from); ?>
</div><!-- #content -->
</div><!-- #container -->
<?php endwhile; ?>
<?php endif; ?><!--end the entire loop-->
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
I know my referer variable and if/else statement are working because the $text variable changes as it should based on the section of the site I get to the blog from. However no matter how I get to the blog, the page is showing all posts and ignoring the query_posts category inside the if/else statment. Can someone please help?
Nevermind figured this out...hope it helps someone else! Fixed it with this:
<?php
/*
Template Name: Blog
*/
get_header(); ?>
<?php
// Find out if the user came to the blog from 'Experienced' or 'College' section of the site
$came_from = wp_get_referer();
// Show posts with categories based on where the user came from
if (strpos($came_from,'experienced') !== false) {
$text = 'test';
$queryCategory = 'experienced-professionals';
// wp_reset_query();
} else {
$text = 'heyo';
// query_posts('cat=college-students');
$queryCategory = 'college-students';
// wp_reset_query();
}
?>
<?php query_posts( array ( 'category_name' => $queryCategory ) ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div style="background:red;width:100%;height:200px;"></div>
<div id="container">
<div id="content" role="main">
<h1 class="entry-title"><?php the_title(); ?></h1>
<p><?php echo $text; ?></p>
<p><?php echo $queryCategory; ?></p>
<?php print_r($came_from); ?>
</div><!-- #content -->
</div><!-- #container -->
<?php endwhile; ?>
<?php endif; ?><!--end the entire loop-->
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
Related
I'm designing a theme from scratch. It's an One page site and my CMS is WordPress.
My folder structure is :
front-page.php
page-about.php
page-work.php
page-contact.php
I can display all my pages on the front-page.php as section but I lose all my custom classes (and specific styles). Here the loop in my front-page.php :
<?php get_header(); ?>
<?php query_posts('post_type=page&order=ASC'); ?>
<?php
if (have_posts()):
while (have_posts()) : the_post(); ?>
<section id="page-<?php the_ID(); ?>">
<div class="container">
<h2><?php the_title(); ?></h2>
<article>
<p><?php the_content(); ?></p>
</article>
</div>
</section>
<?php endwhile;
endif; ?>
<?php get_footer(); ?>
How can I keep the style (therefore, their classes/id's) of the page-xxx.php as section in my front-page.php ?
Thank you
you can add a custom class to specific page id, example :
add_filter('body_class', 'your_body_class');
function your_body_class($classes) {
if (is_page(xxx))
$classes[] = 'new-class';
return $classes;
}
you can use this plugin Custom Body Class
I did this (in the front-page.php) :
<?php get_header(); ?>
<?php
$args = array(
'post_type' => 'page',
'order' => 'ASC'
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ( $query->have_posts()) {
$query->the_post();
$tn = get_page_template_slug($post_id);
$word = array("page-", ".php",' ');
$template = str_replace($word,'',$tn);
get_template_part('page', $template);
}
}
?>
<?php get_footer(); ?>
Look like it works, I have three sections with my three pages and their classes. But now, I have no content.
My page-about.php :
<?php get_header();
/* Template Name: About */ ?>
<section id="about" class="section section-about">
<div class="container">
<h2 class="title-2"><?php the_title(); ?></h2>
<div class="col-md-6 col-lg-12"></div>
<article class="article-about">
<?php
if (have_posts()):
while (have_posts()): the_post(); ?>
<p><?php the_content(); ?></p>
<?php endwhile;
endif;
?>
</article>
</div>
</section>
<?php get_footer(); ?>
Here is a copy of my index.php file, which i gather governs what is shown on the posts page. Im wanting the posts page to only show the posts from my "blog" category. Can someone help with the code changes that i need to make, to implement this?
Thanks heaps guys
Erin
<?php
$archive_page_layout = esc_attr(of_get_option('archive_page_layout'));
?>
<main id="main" class="site-main clearfix <?php echo esc_attr($archive_page_layout); ?>">
<?php if($archive_page_layout == 'both-sidebar'): ?>
<div id="primary-wrap" class="clearfix">
<?php endif; ?>
<div id="primary" class="content-area">
<?php if ( have_posts() ) : ?>
<?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' , 'blog' );
?>
<?php endwhile; ?>
<?php the_posts_navigation(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</div><!-- #primary -->
<?php
if($archive_page_layout == 'both-sidebar' || $archive_page_layout == 'left-sidebar'):
get_sidebar('left');
endif;
?>
<?php if($archive_page_layout == 'both-sidebar'): ?>
</div>
<?php endif; ?>
<?php
if($archive_page_layout == 'both-sidebar' || $archive_page_layout == 'right-sidebar'):
get_sidebar('right');
endif;
?>
</main><!-- #main -->
<?php get_footer(); ?>
you have to use argument parameters to specify a posts categorie/s please refer to
https://wordpress.stackexchange.com/questions/145125/display-content-from-a-specific-category for the code you will need.
:)
I am trying to insert meta slider to display above my posts in wordpress.
I correctly inserted the code in the theme index.php file above the loop. it works correctly, but it also displays on top of the side bar as well, and i would like it just above the posts.
is there any advice on how i can do this? where in the loop would i need to insert it?
here is what i have
<?php
/**
* The main template file
*
* #package SimpleMag
* #since SimpleMag 1.0
**/
get_header();
global $ti_option;
?>
<?php $archive_sidebar = get_field( 'page_sidebar', get_option('page_for_posts') ); ?>
<section id="content" role="main" class="clearfix animated">
<?php
echo do_shortcode("[metaslider id=437]");
?>
<?php if ( $ti_option['posts_page_title'] == 'full_width_title' ) : ?>
<header class="entry-header page-header">
<div class="wrapper title-with-sep page-title">
<h1 class="entry-title">
<?php
$posts_page_id = get_option( 'page_for_posts' );
echo get_the_title( $posts_page_id );
?>
</h1>
</div>
</header>
<?php endif; ?>
<div class="wrapper">
<?php
// Enable/Disable sidebar based on the field selection
if ( ! $archive_sidebar || $archive_sidebar == 'page_sidebar_on' ):
?>
<div class="grids">
<div class="grid-8 column-1">
<?php endif; ?>
<?php if ( $ti_option['posts_page_title'] == 'above_content_title' ) : ?>
<header class="entry-header page-header">
<div class="title-with-sep page-title">
<h1 class="entry-title">
<?php
$posts_page_id = get_option( 'page_for_posts' );
echo get_the_title( $posts_page_id );
?>
</h1>
</div>
</header>
<?php endif; ?>
<div class="grids <?php echo $ti_option['posts_page_layout']; ?> entries">
<?php
if ( have_posts() ) : while ( have_posts()) : the_post();
get_template_part( 'content', 'post' );
endwhile;
?>
</div>
<?php ti_pagination(); ?>
<?php else : ?>
<p class="message">
<?php _e( 'Sorry, no posts were found', 'themetext' ); ?>
</p>
<?php endif;?>
<?php
// Enable/Disable sidebar based on the field selection
if ( ! $archive_sidebar || $archive_sidebar == 'page_sidebar_on' ):
?>
</div><!-- .grid-8 -->
<?php get_sidebar(); ?>
</div><!-- .grids -->
<?php endif; ?>
</div><!-- .wrapper -->
</section><!-- #content -->
<?php get_footer(); ?>
As long as the sidebar is deactivated, your placement is fine. When the sidebar is active you should place it just below <div class="grid-8 column-1">. That's the beginning of the main area of your page, next to the sidebar. (I'm guessing the div is using something similar to bootstrap, taking up 8 of 12 available grids).
To prevent future updates of your theme to overwrite your customizations, if this is in fact a supported theme, you should also consider creating a child theme. It's real easy and is explained here: https://codex.wordpress.org/Child_Themes
I'm not a wordpress expert, so wondering if someone can help me. I created a custom page template for one page so I could aggregate and display posts from one category on that page. However, I created one post in that category so far, but it gets displayed twice on the page. The posts that get pulled in from the category get displayed in the "div id=main" div. Here is the page template code:
<?php /* Template Name: Deals Page */ ?>
<?php get_header(); ?>
</div>
//<?php while ( have_posts() ) : the_post(); ?>
<div id="title-bar">
<h1 class="bar-entry-title container"><?php the_title(); ?></h1>
</div>
<?php endwhile; ?>
<div class="container col-md-12"><!-- restart previous section-->
<div id="primary" class="content-area col-md-8">
<main id="main" class="site-main" role="main">
<?php query_posts('category_name=deals &posts_per_page=20'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title( '<h2>', '</h2>' ); ?>
<?php the_content(); ?>
//<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
<?php
// If comments are open or we have at least one comment, load up the comment template
if ( comments_open() || '0' != get_comments_number() )
comments_template();
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_sidebar('footer'); ?>
<?php get_footer(); ?>
Do you know why it is displaying the post twice? I can't see why.
You are running two loops. Your comment line is outside of the PHP on the 5th line.
Change this //<?php while ( have_posts() ) : the_post(); ?>
to this <?php // while ( have_posts() ) : the_post(); ?>
Also comment out the first <?php endwhile; ?> around line 9 or else you will get a PHP error
I'm having problem in showing posts in my index page in wordpress the title and contents doesnt show. Currently i'm modifying the index.php of twenty-twelve template to show the custom post by id, my goal is to show 3 posts in my index page, here's my code below:
<?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(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<br><br>
<?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><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
The code above is the default index.php of twenty-twelve template in wordpress. I replaced this code from:
<?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' ); ?>
To this, because i want to show only post 23 by id.
<?php $the_query = new WP_Query( 'page_id=23' ) ?>
<?php while ( $the_query->have_posts() ) : ?>
<?php $the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
So the whole code for my new index.php is:
<?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();
?>
<div id="primary" class="site-content">
<div id="content" role="main">
<br><br>
<?php if (have_posts()) : ?>
<?php $the_query = new WP_Query('page_id=23') ?>
<?php while ($the_query->have_posts()) : ?>
<?php $the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?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><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
That's all what i have guys. Can you help me? Is there anything wrong on showing the posts? Thanks in advance guys. :)
An approach if u dont want to use Wp_query
Try this:
<div id="primary" class="site-content">
<div id="content" role="main">
<br><br>
<?php if (have_posts()) : ?>
<?php $post = get_post(23);?>
<?php echo $post->post_title; ?>
<?php echo $post->post_content; ?>
<?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><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
You need to display post with id 23. Right?
<?php if (have_posts()) : ?>
<?php $the_query = new WP_Query('p=23') ?>
<?php while ($the_query->have_posts()) : ?>
<?php $the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
You are missing little like this :
$query = new WP_Query( 'p=7' );
ALternate you can try query_posts() for specific post in query like this:
<?php query_posts('p=23'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;?>
<?php get_footer(); ?>