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
Related
I am currently using the cubic theme and ran into some trouble with template creation. I have multiple sections on my site and am trying to replicate the functionality of my home page onto another page where ideally the home page would display featured articles and another page for urban exploration would be able to display posts about urban exploration in the same format. I.e. Images with nicely displayed titles in rows of three link to posts I have created.
So far this is what my template looks like:
<?php /* Template Name: Page Directory */ ?>
<?php get_header(); ?>
<?php the_content(); ?>
<div id='primary' class='content-area'>
<main id='main' class='site-main' role='main'>
</main><!-- .site-main -->
<?php get_sidebar( 'content-bottom' ); ?>
</div><!-- .content-area -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
This allows me to display the header with the integrated side bar and footer, but I cannot figure out how to replicate cubic's home page functionality.
I imagine the PHP function would have to query for tags in order to place the post on the page, then look for a post's featured image and title to display it as a square image covering 1/3 of the page. Any help would be greatly appreciated.
Update:
I found you can use WP_Query to create a loop to pull information then display it using get_template_part as a means of formatting the post information. I can now display the images for my posts however the formatting is off because of the way content.php is written. [Do any users of cubic (a child theme in WordPress) know how the homepage is constructed and how I might be able to reference that file so I can recreate its format on a different page in WordPress?]
<?php /* Template Name: Page Directory */ ?>
<?php get_header(); ?>
<?php the_content(); ?>
<main id='main' class='site-main' role='main'>
<?php
$postid = new WP_Query( array( 'tag' => 'featured' ) );
if( $postid->have_posts() ):
while( $postid->have_posts() ): $postid->the_post(); ?>
<?php get_template_part('content',get_post_format()); ?>
<?php endwhile;
endif;
?>
</main><!-- .site-main -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
You need to add the loop which fetches the posts:
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
I realized my website (www.inunfuturoaprile.it) with a minimal Wordpress theme that does not include the category.php template. I have created the template by me: the goal was to get pages that display a list of the posts belonging to a specific category. Unfortunately, due to my scarce knowledge of php, the code does not seem to work properly: I only see one post per category (e.g. https://www.inunfuturoaprile.it/politica/).
Here's the category.php file code:
<?php
/**
* Template di Categoria
*/
get_header(); ?>
<div id="content">
<?php
// Check if there are any posts to display
if ( have_posts() ) : ?>
<?php
// Since this template will only be used for Design category
// we can add category title and description manually.
// or even add images or change the layout
?>
<h1><strong><?php single_cat_title(); ?></strong>: Elenco Articoli</h1>
<div class="entry">
<?php
// The Loop
while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?> // <small><?php the_time('j F Y') ?></small>
<?php endwhile; // End Loop
else: ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php endif; ?>
</div>
</div>
<?php get_footer(); ?>
Someone can help me? Thank you :)
I think your posts_per_page parameter is set to 1.
You can see other posts using pagination:
https://www.inunfuturoaprile.it/politica/page/2/
https://www.inunfuturoaprile.it/politica/page/3/
Check it on Settings->Reading page ( /wp-admin/options-reading.php), parameter "Blog pages show at most"
But it also can be set up somewhere in the theme code.
Changing it may affect your main page - since the main page shows only one post too.
So you can change it only on category.php page. Add this after get_header():
get_header();
global $wp_query;
$wp_query->set('posts_per_page', 10);
// or this - if you need no pagination at all:
// $wp_query->set('nopaging', true);
$wp_query->query($wp_query->query_vars); ?>
I'm trying to show on my wordpress theme a specific post on a page. In this case, I'm trying to show the post on the home page and I've tried a lot to at least show the title, but all that I get is the title of the page, not of the post itself.
I've tried the_title and get_the_title() but the magic didn't happen.
Here comes the code relevant to my problem.
home.php
<?php
if ( have_posts() ) :
/* Start the Loop */
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.
*/
// if ( has_post_format( 'video' )) {
// echo 'this is the video format';
// }
get_template_part( 'template-parts/content-chat', get_post_format('chat') );
endwhile;
else :
// get_template_part( 'template-parts/content', 'none' );
endif; ?>
And it calls the file (it is calling the right file, double checked)
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> >
<h2><?php the_title(); ?></h2>
<?php the_content();
echo get_post_format();
?>
</article><!-- #post-## -->
In summary, the questions are:
1) Why is it showing the title of the page?
2) How can I show properly this kind of post format in my page?
For your first question wp grabs the title of the home page based on query_posts() this is what makes wordpress so dynamic, you can actually check a page or category id and then select with query posts what to show on it. Since you do not have a query_posts in your code wp defaults to the current post id which is your home page.
For your second, I had a code snippet, but it is at home, you can look it up here
https://developer.wordpress.org/reference/functions/query_posts/
Ok, so here is the snippet. Sorry for the delay, I have been on vacation.
First you have to determine the ID of your home page, or you can use the is home page function. If you go the former route you simply go to the back end to your home page and hover over it with your mouse. the home page should show up in a link tooltip at the bottom of your page. It will be something like post=5 the number is what you need.
Next I would create a category for the post you want to display on the front page and get that ID from the category page in the back end. Again it will be something like tag_id=12
<?php
$currpage = $post -> ID;
if($currpage == 5) {
query_posts('showposts=1&cat=12');
}
while (have_posts()): the_post();
You can have multiple posts on one page this way, infact you can have your whole loop on that page then under it dynamically populate other posts under it like in this example:
http://testex-ndt.com/products/ect-products/ The top part is the page that is working a loop and the bottom (testimonials) are posts that are assigned to a specific category, the showposts here are set to 3 and will display excerpts of the latest three posts. This is one website I did in the past.
One other thing you may want to keep in mind is page templates, if you want one page to work one way and you want another type of page to work another you will have to look into templating.
here is the entire code for that page.
<?php
/*
Template Name:Main Product
*/
?>
<?php get_header(); ?><!-- BANNER is part of main homepage only -->
<?php if (have_posts()) : ?>
<?php while (have_posts()): the_post(); ?>
<?php $postName = get_the_title($post); ?>
<!-- title link dynamic -->
<?php edit_post_link('Edit this item','',' | '); ?>
<?php the_content('Continue Reading'); ?>
<?php posts_nav_link(); ?>
<?php endwhile; ?>
<?php else : ?>
<div class="w3-col text">
<h2>Not Found</h2>
<p><?php _e("Sorry, no posts or pages could be found. Why not search for what you were trying to find?"); ?></p>
<?php get_search_form(); ?>
</div>
<?php endif; ?>
<!-- insert testimonial here -->
<section class="w3-row cl testimonials">
<!-- make code for query based on the page, then get the top three testimonials in excerpt form This is commented out because I did it with a function I attached to the header I will show below
$currpage = $post -> ID;
if($currpage == xx){
$cat = x;
}
-->
<h2>Testimonials for <?php echo $postName; ?></h2>
<?php
$currpage = $post -> ID;
$cat = testimonial($currpage); //here is the function
query_posts('showposts=3&cat=' .$cat ); ?>
<?php while (have_posts()): the_post(); ?>
<div class="w3-col excerpt" style="width:30%;">
<?php the_excerpt(''); ?>
</div>
<?php endwhile;
wp_reset_query(); // DO THIS EVERYTIME YOU ALTER QUERY_POSTS
?>
</section>
<!-- /content float -->
</div>
<!-- /content -->
</div>
So the Function was set up like as a seperate php file so I could alter it modularly. I called the file category_help.php
<?php
/*This particular block of code is only for determining what category of testimonial is allowed in a post. */
function testimonial($myPageID){
//services (all cats)
$id = "-15,-14,-13";
//Products
//lfet
if($myPageID == 18) $id = 2;
//bfet
if($myPageID == 22) $id = 4;
//rfet
if($myPageID == 20) $id = 3;
//ect
if($myPageID == 24) $id = 5;
//ultrasound
if($myPageID == 26) $id = 8;
return $id;
}
?>
Then I called this in the top of header.php
<?php require 'category_help.php'; ?>
I'm intending to have a few different custom post types but I want them to have different layouts to that of the normal posts.
Normal posts have two different appearances themselves, one for the index page and one for when you click through to the permalink page.
For custom posts I want to do the same thing (two different layouts, both different from normal posts) but for some reason my code doesn't seem to be making a difference.
I've so far used custom post template plugin as well as tried to code in a post-[postype].php file, but both seemed ineffective.
For my single.php here's what the code is -
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<!--- post wrapper home/post --->
<?php if ( is_home()) { echo '<div class="fullposthome">' ; }
else { echo '<div class="fullpost">' ; }
?>
<?php if( get_post_meta($post->ID, 'imgtest', true) ) { ?> <!--- made following div appear if said custom field presetn ---->
<div class="testbox"><img src="<?php the_field('imgtest'); ?>" alt="" width="100%" height="auto"/></div> <!--- div with custom field inside --->
<?php } ?>
<?php if ( is_home()) { echo '<div class="contenttextboxhome">' ; }
else { echo '<div class="contenttextbox">' ; }
?>
<?php get_template_part( 'content', 'single' ); ?>
</div>
<?php temptheme1_content_nav( 'nav-below' ); ?>
<?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();
?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!--- post wrapper --->
</div><!-- #primary -->
For custom posts I've tried changing the line
for custom posts I tried as that is what I'm assuming the names are referencing [content/single.php]
- this is in underscore.me / _S framework mind you, I'm also going to try on Thematic framework but since _S is more bare bones it would be easier for me to build it how I want it.
So my question I guess is where am I going wrong with my coding or how do I use the Custom Post Template plug in properly?
If your custom post type is "products"
then
Archive file should be : archive-products.php
Taxonomy should be : taxonomy-product_category_slug.php
Single file should be : single-products.php
if you want different content template then
create file of like content-product.php
and in your single.php use get_template_part( 'content', 'product' );
For current situation if everything working fine then just create content file, customize it.
I've made my first theme and it works great for editing updating pages etc, but won't display any posts.
I have put the "loop" in a template page (copied from twentytwelve theme), as I only want the posts to appear on that page. I've set the blog posts to appear on this page (from the settings page), but still nothing will show.
Here is my code for the template page to display blog posts.
Any idea what's wrong?
<?php
/**
* Template Name: blog
*
* Full width page template with no sidebar.
*
* #package Myfirsttheme
* #subpackage Template
*/
get_header(); ?>
<?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 else : ?>
<article id="post-0" class="post no-results not-found">
<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_footer(); ?>
Posts will always appear in the index.php template, unless you change the "Front page displays" option in Settings=>Reading, see here for example: http://imgur.com/izwa5yw If you have this set to show the blog posts on a page (is in the image), then whatever page that is (blog) has to have the Default Template (in the page edit screen) set to the value you wrote in the Template Name: section of your file (in your case blog), as Tamil said.
Updated: You have to echo get_template_part() or it won't show up. You can use the_content() instead which is preferred. Any variables that start with the_ output themselves. The get_ variables don't output themselves.
<?php echo get_template_part(); ?>
<?php the_content() ?>