How to show wordpress CPT items in the single page - wordpress

I defined custome post type (Slug = portfolio).
After a Portfolio post publishes and I click on View post, page not found displays. This is my first problem.
I want when user clicks on a portfolio item it opens in an another page with more details of information. This is my another problem.
This is the code of template page for showing portfolio posts (the name of the page is: portfolio.php)
<?php
/* Template Name: Portfolio */
$arg = array('post_type'=>'portfolio');
$loop = new WP_Query($arg);
while($loop->have_posts()):
$loop->the_post();
?>
show post
<br>
<?php
endWhile;
?>
and i have single-portfolio.php with these codes:
<?php
$arg = array('post_type'=>'portfolio');
$loop = new WP_Query($arg);
while($loop->have_posts()):
$loop->the_post();
?>
show post
<br>
<?php
endWhile;
?>
please help me

The contents of your single-portfolio.php code could be something like this.
<?php
while ( have_posts() ) : the_post();
the_title();
the_content();
endwhile; // End of the loop.
?>

Related

Wordpress homepage with latest posts and Advanced Custom fields - How?

I'd like my homepage to display my latest posts which are portfolio projects of mine. Beneath these project thumbnails I've got my static content which I'm using the Repeater add-on from Advanced Custom Fields to grab. I cant get it all to work on the same page... either get the blog to work or the ACF stuff to work. Never both because one needs to be a designated static page and one needs to be a posts page. I don't understand how to tie it together and make it appear as one page.
I've experimented with the settings in the reading panel..
I've tried using front-page.php
I've read up on the WP hierarchy etc
I've tried using templates...
I've tried wp_reset_postdata(); which I read about elsewhere on Stack Overflow.
What settings must I use in the reading panel. Do I need to use a template file?
Here's the code I'm working with, I've split the code between templates and different files already, but just for ease of reading its all together here (maybe that's the right way to do it anyway, I wouldn't know..)
<!-- The posts/portfolio items -->
<?php get_header(); ?>
<div>
<?php if(have_posts()) : ?>
<ul>
<?php while ( have_posts() ) : the_post(); ?>
<li>
<!-- Permalink,title and post thumbnail here (omitted) -->
</li>
<?php endwhile; ?>
</ul>
<?php else: ?>
<h2>No Posts found</h2>
<?php endif; ?>
</div>
<!-- Now for the ACF Stuff -->
<?php if(get_field('care_list')): ?>
<?php while(has_sub_field('care_list')): ?>
<div class="grid_2 what-i-care-about gap">
<div class="important-img-container">
<?php the_sub_field('care_list_image'); ?>
</div>
<h3><?php the_sub_field('care_list_title'); ?></h3>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer(); ?>
Please help a frustrated learner! Thanks in advance.
It looks like you're going to need to add the post id of your 'home page' (the one with the ACF repeater on it) to the get_field() function like so:
<?php $post_id = **post_id_of_your_homepage_here**; ?>
<?php if(get_field('care_list', $post_id)): ?>
<?php while(has_sub_field('care_list')): ?>
<div class="grid_2 what-i-care-about gap">
<div class="important-img-container">
<?php the_sub_field('care_list_image'); ?>
</div>
<h3><?php the_sub_field('care_list_title'); ?></h3>
</div>
<?php endwhile; ?>
This is because the $post_id parameter defaults to the current post being brought up by wordpress, which means ACF is looking for the repeater on the last Portfolio item/post you are displaying. If you set the $post_id parameter to the ID of your homepage, ACF will instead look for the repeater on that page.
Source: http://www.advancedcustomfields.com/resources/functions/get_field/#parameters
If I'm understanding correctly, you have a bunch of posts and you want to display a list of them with title and post thumbnail on your homepage, and then display a custom field you've assigned to the homepage underneath the list of posts?
Step 1: Create a new page template by copying page.php, changing the name to homepage.php and adding this to the top:
<?php
/*
Template Name: Homepage
*/ ?>
Step 2: Crete a Wordpress page called "Homepage" and in the attributes module in the right sidebar of the page creation tool, select "Homepage" as your page template.
Step 3: In your reading settings, change the front page from posts page to "Homepage." Now your homepage is your page called "Homepage."
Step 4: Make something like this the full code on your new page template homepage.php. It will output your posts list followed by your page custom field:
<?php get_header(); ?>
<?php $the_query = new WP_Query( $args );
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_post_thumbnail(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php if(get_field('repeater_field_name')): ?>
<?php while(has_sub_field('repeater_field_name')): ?>
<?php the_sub_field('sub_field_1'); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer(); ?>

How to apply pagination for a custom WordPress query?

<?php $query = new WP_Query('posts_per_page=1&'); ?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
<?php the_title(); ?><br />
<?php echo paginate_links( $args ) ?>
<?php endwhile;?>
<?php wp_reset_postdata(); ?>
Here is the query I'm using, it should display the first post title then displays the pagination links, however it only shows the post title without the pagination links. What's wrong?
The paginate_links function should go outside the loop and you should use the query variable "paged"
Please see this link http://scribu.net/wordpress/wp-pagenavi/right-way-to-use-query_posts.html for more information on using this.

Wordpress page content not appearing

I have a small problem on a wordpress custom post type page, whereby the page content underneath seems to output the last piece of content from one of the custom post types. The code is below.
We wanted to move the page content to below the custom post type and so moved the get_template_part( 'content', 'page' ); function to below the custom post loop. When this function is above the loop, then the page content outputs properly, but above the custom post loop (i.e not as desired).
I know the fix could be fixed, but if so please could you point me in the right direction?
<div id = "feature_boxes_wrap2">
<?php query_posts(array(
'posts_per_page' => 3,
'post_type'=>'feature_box'
)
); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class = "feature_box2">
<?php echo get_the_post_thumbnail($id, array($image_width,$image_height)); ?>
<h2 class="feature2"><?php the_title();?></h2>
<?php $feature_content = get_the_content(); ?>
<?php echo substr($feature_content, 0, 100); ?>
<br>
Read more
</div><!-- #feature_box2 -->
<?php endwhile; // end of the loop. ?>
</div><!-- #feature_boxes_wrap2 -->
<?php
the_post();
get_template_part( 'content', 'page' ); ?>
Here, try this:
Add a wp_reset_query() before you call the_post(); [...] and that should do the trick.
For more information: http://codex.wordpress.org/Function_Reference/wp_reset_query

Make WordPress homepage a post category?

I am trying to set my WordPress homepage to a category but it only allows me to set it to either the latest posts or a static page.
Is it possible to set your homepage as a post category?
I am hoping that you know about how to set static page. So first create an empty .php file and name it whatever you like and put it along the other files (index.php, arhive.php etc).
and then enter following code
<?php
/*
* Template Name: Category based Homepage
*/
?>
<?php get_header(); ?>
<div class="main">
<?php
$cat_ID = '1'; //it should be your category ID, you can get the id of the category by going to categories and edit and then in url you can find the tag_ID.
$posts_to_show = '10'; // number of posts from the category you want to show on homepage
//query_posts("cat=$cat_ID&showposts=$posts_to_show");
$category_posts = new WP_Query("cat=$cat_ID&showposts=$posts_to_show");
//if (have_posts())
if ($category_posts->have_posts())
: $first = true;
?>
<ul class="post-list">
<?php
//while (have_posts()) : the_post();
while ($category_posts->have_posts()) : $category_posts->the_post();
if ($first)
$class = "first-in-row";
else
$class = "";
$first = !$first;
?>
<!-- Start: Post -->
<li <?php post_class($class); ?>>
<?php the_post_thumbnail(); ?>
<p class="categories"><?php the_category(", "); ?></p>
<h2><?php the_title(); ?> <?php edit_post_link(__('Edit', 'your_theme_text_domain'), '', ''); ?></h2>
<p class="post-meta"><span class="date"><?php the_time(get_option('date_format')) ?></span> <?php if (comments_open()) : ?>, <span class="comments"><?php comments_popup_link(_x('0', 'comments number', 'your_theme_text_domain'), _x('1', 'comments number', 'your_theme_text_domain'), _x('%', 'comments number', 'your_theme_text_domain')); ?></span> <?php endif; ?> <span class="author"><?php the_author() ?></span></p>
<?php the_excerpt(); ?>
<p class="more"><?php _e('Read More »» ', 'your_theme_text_domain'); ?></p>
<?php if (has_tag()): ?><p class="tags"><span><?php the_tags(""); ?></span></p><?php endif; ?>
</li>
<!-- End: Post -->
<?php endwhile; ?>
</ul>
<?php else : ?>
<h2 class="center"><?php _e('Not found', 'your_theme_text_domain'); ?></h2>
<p class="center"><?php _e('Sorry, but you are looking for something that isn\'t here.', 'your_theme_text_domain'); ?></p>
<?php
endif;
//wp_reset_query();
wp_reset_postdata();
?>
</div>
<?php get_sidebar(); //optional?>
<?php get_footer(); ?>
and replace $cat_ID and $posts_to_show to your liking. And I have used both query methods adjust it to your needs.
Hope it helps somebody who is looking for similar solution.
You can create a custom template that mimics a category page using get_posts and set a page using that template to home, but it won't be perfectly dynamic in the sense that you have to hard code the category slug or ID into that query. Assuming that you don't want to change that category often, that shouldn't be an issue. Alternatively, you could use wp_safe_redirect in a template to redirect to the category page - that would be if you want the user to be put directly on the real category page, URL and all.
I'm not sure what you mean by having your home page as a category, you mean that in your home page posts that will be displayed will be only from a certain category ?
You only need to perform a WP_Query before the loop;
$query = new WP_Query("cat=10, paged=".get_query_var('paged'));
Then use use the WP_Query object to perform the loop;
if($the_query->have_posts()):
while($the_query->have_posts()):
the_title();
the_content();
//Use all the loop function normally
endwhile;
endif;
The paged parameter is used to determine in which page you are, if you need paginantion.
Instead of using the category id, it is good to retrieve the id by the slug.
$home = get_category_by_slug('home-category-slug');
Then your query will be like this
$the_query = new WP_Query("cat=".$home->cat_ID.", paged=".get_query_var('paged'));
Yes this is possible Go to dashboard>>Setting>>Reading>>Static page Choose page from drop down and SAVE. On that page you can create your own stuff...

Linking to list of blog posts with wordpress?

I've made my front page a static page with wordpress. I would still like to be able to link to a listing of blog posts. How is this done?
create a new page template file called page-home.php
best way to do this is copy and paste the code from index.php into your new page...
inside this page create a new template call..
<?php
/**
Template Name: home
*/
?>
inside this page paste your index.php code..
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<?php the_content('<p>Read the rest of this page »</p>'); ?>
<?php wp_link_pages(array('before' => '<p>Pages: ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
</div>
<?php endwhile; endif; ?>
this will display your home page as usual, then you have to create a new call to the wordpress database to display your latest posts from which ever categories you want or show all posts from all categories..
<?php
//The Query
query_posts('posts_per_page=5&cat=YOUR_CATEGORY_HERE');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
..
endwhile; else:
..
endif;
//Reset Query
wp_reset_query();
?>
this will then loop through the posts and display your latest posts..
have a check through the wordpress query posts codex

Resources