I'm working on a theme similar to getBootstrap.com page. The issue I'm facing is:
I'm trying to display specific content in the purple section by using the following:
<?php $recent = new WP_Query("page_id=39"); while($recent->have_posts()) : $recent->the_post();?>
<h1><?php the_title(); ?></h1>
<br>
<div class="col-md-4 col-md-offset-4">
<?php the_content(); ?>
<?php endwhile; ?>
The top content are brought from page ID 39 <-- this is a page template, as I will be creating multiple templates to display specific content at the top from other pages as well, if there is another way please help otherwise...
I also have this code after the top content which is the default content page (The default page content from the WordPress WYSIWYG editor).
<?php the_content(); ?>
But it is getting page ID 39 content.
I want to the page ID I'm in contents at the bottom (white space) beside the page ID 39 in the (purple space).
I hope my question is clear.
http://i.stack.imgur.com/6lspz.png
Solution Found:
<?php
$id=39;
$post = get_post($id);
$content = $post->post_content;
echo $content;
?>
Just Used this:
<?php
$id=39;
$post = get_post($id);
$content = $post->post_content;
echo $content;
?>
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 use a plugin called "Custom Content Type Manage"
this allows me to create a new content type which essentially mimics posts (all done in the name of 'user friendly')
This aside, I wrote the following:
<h2><?php single_cat_title( '', true ); ?></h2>
<p>
<?php
$page_id = '5536';
$page_data = get_page($page_id);
print apply_filters('the_content', $page_data->post_content);
?>
</p>
<p>
<?php
$category = get_the_category();
$category_id = $category[0]->cat_ID;
print category_description( $category_id );
?>
</p>
<div class="category-list">
<?php wp_nav_menu(); ?>
</div>
<ul class="leaders-container">
<?php if (have_posts()) : while (have_posts()) : the_post();?>
<li class="leader-container">
<?php
$image = get_custom_field('leader_image:to_image_array');
$url = get_custom_field('website_url:raw');
print "<img class='leader-image' src='".$image['0']."'>";
?>
<h2>
<?php the_title(); ?>
</h2>
<?php
print "</h2>";
the_content();
if($url != "#") {
print "<a class='website-button' href='".$url."' target='_blank'>Visit Website</a>";
}
?>
</li>
<?php endwhile; endif;?>
What this was doing, was getting the info from the category, and listing all the posts assigned to the category.
In this case, a post was labeled as a "Leader" and a category was their "congregation", so it was listing all the leaders assigned to the congregations.
This is an example of what it should look like
http://www.ubmsonline.org/?leader=rabbi-binyamin-sheldrake
However, this only works as it is a direct link from the posts in question
The category on the otherhand, which was working, and did list as many leaders assigned to the category, has now stopped working.
http://www.ubmsonline.org/category/ubms-leaders/
As you can see though, its pulling everything across correctly, category description, category title etc, but its just now not showing the posts.
fixed!
It was to do with a setting that changed on the "Custom Content Type Manager" plugin im using. After alot of research, i stumbled upon the exact problem on the "issues" section of the plugin's FAQ's page.
https://code.google.com/p/wordpress-custom-content-type-manager/issues/detail?id=594
Hopefully this might help others.
In wp-content/plugins/custom-content-type-manager/loader.php the filter mentioned below might be commented, so if you uncomment this add_filter this bug will fix.
// Enable archives for custom post types
add_filter('request', 'CCTM::request_filter');
Thanks again for every one's help.
Andrew
I am creating a wordpress theme from scratch and I think I'm having problems with my page.php file. When I go into the admin panel, add a new page and fill out the content I want then view the page I only see the title of the page while the rest is blank. For some reason the content of the page is not displaying.
The code in my page.php file is..
<?php /*
Template Name: Page Template
*/
get_header(); ?>
<div class="page">
<h1 class="entry-title"><?php the_title(); ?></h1>
<hr>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
</div>
<?php get_footer(); ?>
I only get to see up to the horizontal rule and then the footer but everything else in between is blank. Am I maybe missing something in my functions.php file?
Try to var_dump( get_template_part('content', 'page') ) or even var_dump(the_post()), you should get important debugging info...
I found the problem. It was a piece of code in my functions.php file.
function PLEASE_the_tags($content='') {
global $post;
$html.="<div class='tags'>\n";
$html.="<a href='#' rel='tag'>".$post_the_tags."</a>\n";
$html.="</div?\n";
$content.=$html;
return $content;
}
add_filter('the_content', 'get_the_tag_list');
After I removed this, the content started showing again. I have no idea how this caused the problem but thank you for everyone's help.